@@ -131,3 +131,194 @@ func TestNullOnlyParameterRejected(t *testing.T) {
131131 a .Error (err )
132132 a .Contains (err .Error (), "combination" )
133133}
134+
135+ func explodePtr (v bool ) * bool { return & v }
136+
137+ func stringArraySchema () * ogen.Schema {
138+ return & ogen.Schema {
139+ Type : "array" ,
140+ Items : & ogen.Items {Item : & ogen.Schema {Type : "string" }},
141+ }
142+ }
143+
144+ func TestParameterExplodeDefault (t * testing.T ) {
145+ object := & ogen.Schema {
146+ Type : "object" ,
147+ Properties : []ogen.Property {{Name : "id" , Schema : & ogen.Schema {Type : "string" }}},
148+ }
149+
150+ tests := []struct {
151+ name string
152+ style string
153+ explode * bool
154+ schema * ogen.Schema
155+ want bool
156+ }{
157+ {"Unset" , "" , nil , stringArraySchema (), true },
158+ {"Form" , "form" , nil , stringArraySchema (), true },
159+ {"SpaceDelimited" , "spaceDelimited" , nil , stringArraySchema (), false },
160+ {"PipeDelimited" , "pipeDelimited" , nil , stringArraySchema (), false },
161+ {"DeepObject" , "deepObject" , nil , object , true },
162+ {"SpaceDelimitedExplodeTrue" , "spaceDelimited" , explodePtr (true ), stringArraySchema (), true },
163+ {"PipeDelimitedExplodeTrue" , "pipeDelimited" , explodePtr (true ), stringArraySchema (), true },
164+ {"FormExplodeFalse" , "form" , explodePtr (false ), stringArraySchema (), false },
165+ }
166+ for _ , tt := range tests {
167+ t .Run (tt .name , func (t * testing.T ) {
168+ root := & ogen.Spec {
169+ OpenAPI : "3.0.3" ,
170+ Paths : map [string ]* ogen.PathItem {
171+ "/items" : {
172+ Get : & ogen.Operation {
173+ OperationID : "listItems" ,
174+ Parameters : []* ogen.Parameter {
175+ {
176+ Name : "ids" ,
177+ In : "query" ,
178+ Style : tt .style ,
179+ Explode : tt .explode ,
180+ Schema : tt .schema ,
181+ },
182+ },
183+ Responses : map [string ]* ogen.Response {
184+ "200" : {Description : "OK" },
185+ },
186+ },
187+ },
188+ },
189+ }
190+
191+ a := require .New (t )
192+
193+ var raw yaml.Node
194+ a .NoError (raw .Encode (root ))
195+ root .Raw = & raw
196+
197+ spec , err := Parse (root , Settings {
198+ RootURL : testRootURL ,
199+ })
200+ a .NoError (err )
201+ a .Len (spec .Operations , 1 )
202+
203+ params := spec .Operations [0 ].Parameters
204+ a .Len (params , 1 )
205+ a .Equal (tt .want , params [0 ].Explode )
206+ })
207+ }
208+ }
209+
210+ func TestEncodingExplodeDefault (t * testing.T ) {
211+ tests := []struct {
212+ name string
213+ style string
214+ explode * bool
215+ want bool
216+ }{
217+ {"Unset" , "" , nil , true },
218+ {"Form" , "form" , nil , true },
219+ {"SpaceDelimited" , "spaceDelimited" , nil , false },
220+ {"PipeDelimited" , "pipeDelimited" , nil , false },
221+ {"PipeDelimitedExplodeTrue" , "pipeDelimited" , explodePtr (true ), true },
222+ }
223+ for _ , tt := range tests {
224+ t .Run (tt .name , func (t * testing.T ) {
225+ const ct = "application/x-www-form-urlencoded"
226+
227+ root := & ogen.Spec {
228+ OpenAPI : "3.0.3" ,
229+ Paths : map [string ]* ogen.PathItem {
230+ "/submit" : {
231+ Post : & ogen.Operation {
232+ OperationID : "submit" ,
233+ RequestBody : & ogen.RequestBody {
234+ Required : true ,
235+ Content : map [string ]ogen.Media {
236+ ct : {
237+ Schema : & ogen.Schema {
238+ Type : "object" ,
239+ Properties : []ogen.Property {{Name : "tags" , Schema : stringArraySchema ()}},
240+ },
241+ Encoding : map [string ]ogen.Encoding {
242+ "tags" : {Style : tt .style , Explode : tt .explode },
243+ },
244+ },
245+ },
246+ },
247+ Responses : map [string ]* ogen.Response {
248+ "200" : {Description : "OK" },
249+ },
250+ },
251+ },
252+ },
253+ }
254+
255+ a := require .New (t )
256+
257+ var raw yaml.Node
258+ a .NoError (raw .Encode (root ))
259+ root .Raw = & raw
260+
261+ spec , err := Parse (root , Settings {
262+ RootURL : testRootURL ,
263+ })
264+ a .NoError (err )
265+ a .Len (spec .Operations , 1 )
266+
267+ encoding := spec .Operations [0 ].RequestBody .Content [ct ].Encoding
268+ a .Contains (encoding , "tags" )
269+ a .Equal (tt .want , encoding ["tags" ].Explode )
270+ })
271+ }
272+ }
273+
274+ func TestHeaderExplodeDefault (t * testing.T ) {
275+ tests := []struct {
276+ name string
277+ explode * bool
278+ want bool
279+ }{
280+ {"Unset" , nil , false },
281+ {"ExplodeTrue" , explodePtr (true ), true },
282+ }
283+ for _ , tt := range tests {
284+ t .Run (tt .name , func (t * testing.T ) {
285+ root := & ogen.Spec {
286+ OpenAPI : "3.0.3" ,
287+ Paths : map [string ]* ogen.PathItem {
288+ "/items" : {
289+ Get : & ogen.Operation {
290+ OperationID : "listItems" ,
291+ Responses : map [string ]* ogen.Response {
292+ "200" : {
293+ Description : "OK" ,
294+ Headers : map [string ]* ogen.Header {
295+ "X-Tags" : {
296+ Explode : tt .explode ,
297+ Schema : stringArraySchema (),
298+ },
299+ },
300+ },
301+ },
302+ },
303+ },
304+ },
305+ }
306+
307+ a := require .New (t )
308+
309+ var raw yaml.Node
310+ a .NoError (raw .Encode (root ))
311+ root .Raw = & raw
312+
313+ spec , err := Parse (root , Settings {
314+ RootURL : testRootURL ,
315+ })
316+ a .NoError (err )
317+ a .Len (spec .Operations , 1 )
318+
319+ headers := spec .Operations [0 ].Responses .StatusCode [200 ].Headers
320+ a .Contains (headers , "X-Tags" )
321+ a .Equal (tt .want , headers ["X-Tags" ].Explode )
322+ })
323+ }
324+ }
0 commit comments