Codebase list libmojolicious-plugin-openapi-perl / b5bbcf7
Merge pull request #212 from elcamlost/fix-collectionFormat Fix bug with arrays in query and formData with collectionFormat Jan Henning Thorsen authored 3 years ago GitHub committed 3 years ago
2 changed file(s) with 122 addition(s) and 3 deletion(s). Raw diff Collapse all Expand all
4040 formData => sub {
4141 my $name = shift;
4242 my $value = $req->body_params->every_param($name);
43 return $evaluated[@evaluated] = {exists => 1, value => $value} if @$value;
43 my $n = @$value;
44 return $evaluated[@evaluated] = {exists => 1, value => $n > 1 ? $value : $value->[0]}
45 if $n > 0;
4446
4547 $value = $req->upload($name);
4648 return $evaluated[@evaluated] = {exists => !!$value, value => $value && $value->size};
5961 return $evaluated[@evaluated] = {exists => 1, value => $req->url->query->to_hash}
6062 unless my $name = shift;
6163 my $value = $req->url->query->every_param($name);
62 return $evaluated[@evaluated] = {exists => !!@$value, value => $value};
64 my $n = @$value;
65 return $evaluated[@evaluated] = {exists => !!$n, value => $n > 1 ? $value : $value->[0]};
6366 },
6467 };
6568 }
99 },
1010 'getPets';
1111
12 post '/pets' => sub {
13 my $c = shift->openapi->valid_input or return;
14 $c->render(openapi => $c->validation->output);
15 },
16 'postPets';
17
1218 get '/pets/:id' => sub {
1319 my $c = shift->openapi->valid_input or return;
1420 $c->render(openapi => $c->validation->output);
1521 },
1622 'getPetsById';
1723
18
1924 plugin OpenAPI => {url => 'data://main/discriminator.json'};
2025
2126 my $t = Test::Mojo->new;
3439
3540 # In path
3641 $t->get_ok('/api/pets/ilm,a,r,i')->status_is(200)->json_is('/id', [qw(ilm a r i)]);
42
43 # In query
44 $t->post_ok('/api/pets?idq=ilm,a,r,i')->status_is(200)->json_is('/idq', [qw(ilm a r i)]);
45 $t->post_ok("/api/pets?idq-tsv=ilm\ta\tr\ti")->status_is(200)->json_is('/idq-tsv', [qw(ilm a r i)]);
46 $t->post_ok('/api/pets?idq-ssv=ilm a r i')->status_is(200)->json_is('/idq-ssv', [qw(ilm a r i)]);
47 $t->post_ok('/api/pets?idq-pipes=ilm|a|r|i')->status_is(200)
48 ->json_is('/idq-pipes', [qw(ilm a r i)]);
49
50 # In formData
51 $t->post_ok('/api/pets' => form => {idf => 'ilm,a,r,i'})->status_is(200)
52 ->json_is('/idf', [qw(ilm a r i)]);
53 $t->post_ok('/api/pets' => form => {'idf-tsv' => "ilm\ta\tr\ti"})->status_is(200)
54 ->json_is('/idf-tsv', [qw(ilm a r i)]);
55 $t->post_ok('/api/pets' => form => {'idf-ssv' => 'ilm a r i'})->status_is(200)
56 ->json_is('/idf-ssv', [qw(ilm a r i)]);
57 $t->post_ok('/api/pets' => form => {'idf-pipes' => 'ilm|a|r|i', 'a' => 'b'})->status_is(200)
58 ->json_is('/idf-pipes', [qw(ilm a r i)]);
59 $t->post_ok('/api/pets' => {'Content-Type' => 'application/x-www-form-urlencoded'} =>
60 'idf-multi=ilm&idf-multi=a')->status_is(200)->json_is('/idf-multi', [qw(ilm a)]);
3761
3862 done_testing;
3963
102126 "schema": { "type": "object" }
103127 }
104128 }
129 },
130 "post" : {
131 "operationId" : "postPets",
132 "parameters" : [
133 {
134 "name":"idq",
135 "in":"query",
136 "type":"array",
137 "collectionFormat":"csv",
138 "items":{"type":"string"},
139 "minItems":0,
140 "required":false
141 },
142 {
143 "name":"idq-tsv",
144 "in":"query",
145 "type":"array",
146 "collectionFormat":"tsv",
147 "items":{"type":"string"},
148 "minItems":0,
149 "required":false
150 },
151 {
152 "name":"idq-ssv",
153 "in":"query",
154 "type":"array",
155 "collectionFormat":"ssv",
156 "items":{"type":"string"},
157 "minItems":0,
158 "required":false
159 },
160 {
161 "name":"idq-pipes",
162 "in":"query",
163 "type":"array",
164 "collectionFormat":"pipes",
165 "items":{"type":"string"},
166 "minItems":0,
167 "required":false
168 },
169 {
170 "name":"idf",
171 "in":"formData",
172 "type":"array",
173 "collectionFormat":"csv",
174 "items":{"type":"string"},
175 "minItems":0,
176 "required":false
177 },
178 {
179 "name":"idf-tsv",
180 "in":"formData",
181 "type":"array",
182 "collectionFormat":"tsv",
183 "items":{"type":"string"},
184 "minItems":0,
185 "required":false
186 },
187 {
188 "name":"idf-ssv",
189 "in":"formData",
190 "type":"array",
191 "collectionFormat":"ssv",
192 "items":{"type":"string"},
193 "minItems":0,
194 "required":false
195 },
196 {
197 "name":"idf-pipes",
198 "in":"formData",
199 "type":"array",
200 "collectionFormat":"pipes",
201 "items":{"type":"string"},
202 "minItems":0,
203 "required":false
204 },
205 {
206 "name":"idf-multi",
207 "in":"formData",
208 "type":"array",
209 "collectionFormat":"multi",
210 "items":{"type":"string"},
211 "minItems":0,
212 "required":false
213 }
214 ],
215 "responses" : {
216 "200": {
217 "description": "pet response",
218 "schema": { "type": "object" }
219 }
220 }
105221 }
106222 }
107223 }