Skip to content

Commit e67c965

Browse files
authored
transform struct fields of array enum type in a struct to query/formData params (#1523)
Signed-off-by: sdghchj <[email protected]>
1 parent 314d61f commit e67c965

File tree

4 files changed

+103
-7
lines changed

4 files changed

+103
-7
lines changed

operation.go

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -321,16 +321,26 @@ func (operation *Operation) ParseParamComment(commentLine string, astFile *ast.F
321321
}
322322

323323
switch {
324-
case prop.Type[0] == ARRAY && prop.Items.Schema != nil &&
325-
len(prop.Items.Schema.Type) > 0 && IsSimplePrimitiveType(prop.Items.Schema.Type[0]):
326-
327-
param = createParameter(paramType, prop.Description, formName, prop.Type[0], prop.Items.Schema.Type[0], findInSlice(schema.Required, name), enums, operation.parser.collectionFormatInQuery)
324+
case prop.Type[0] == ARRAY:
325+
if prop.Items.Schema == nil {
326+
continue
327+
}
328+
itemSchema := prop.Items.Schema
329+
if len(itemSchema.Type) == 0 {
330+
itemSchema = operation.parser.getUnderlyingSchema(prop.Items.Schema)
331+
}
332+
if len(itemSchema.Type) == 0 {
333+
continue
334+
}
335+
if !IsSimplePrimitiveType(itemSchema.Type[0]) {
336+
continue
337+
}
338+
param = createParameter(paramType, prop.Description, formName, prop.Type[0], itemSchema.Type[0], findInSlice(schema.Required, name), itemSchema.Enum, operation.parser.collectionFormatInQuery)
328339

329340
case IsSimplePrimitiveType(prop.Type[0]):
330-
param = createParameter(paramType, prop.Description, formName, PRIMITIVE, prop.Type[0], findInSlice(schema.Required, name), enums, operation.parser.collectionFormatInQuery)
341+
param = createParameter(paramType, prop.Description, formName, PRIMITIVE, prop.Type[0], findInSlice(schema.Required, name), nil, operation.parser.collectionFormatInQuery)
331342
default:
332343
operation.parser.debug.Printf("skip field [%s] in %s is not supported type for %s", name, refType, paramType)
333-
334344
continue
335345
}
336346

testdata/enums/api/api.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,20 @@ func API2() {
2929
//
3030
// @Summary test enums fields in formdata request
3131
// @Description test enums fields in formdata request
32-
// @Param stdeunt formData types.Person true "type"
32+
// @Param student formData types.Person true "type"
3333
// @Success 200 "ok"
3434
// @Router /students2 [get]
3535
func API3() {
3636
_ = types.Person{}
3737
}
38+
39+
// post students
40+
//
41+
// @Summary test array enums fields in formdata request
42+
// @Description test array enums fields in formdata request
43+
// @Param student formData types.PersonWithArrayEnum true "type"
44+
// @Success 200 "ok"
45+
// @Router /students4 [get]
46+
func API4() {
47+
_ = types.Person{}
48+
}

testdata/enums/expected.json

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,74 @@
168168
}
169169
}
170170
}
171+
},
172+
"/students4": {
173+
"get": {
174+
"description": "test array enums fields in formdata request",
175+
"summary": "test array enums fields in formdata request",
176+
"parameters": [
177+
{
178+
"type": "array",
179+
"items": {
180+
"enum": [
181+
-1,
182+
1,
183+
2,
184+
3,
185+
4,
186+
5
187+
],
188+
"type": "integer"
189+
},
190+
"name": "class",
191+
"in": "formData"
192+
},
193+
{
194+
"type": "array",
195+
"items": {
196+
"enum": [
197+
1,
198+
2,
199+
4,
200+
8
201+
],
202+
"type": "integer"
203+
},
204+
"name": "mask",
205+
"in": "formData"
206+
},
207+
{
208+
"type": "string",
209+
"name": "name",
210+
"in": "formData"
211+
},
212+
{
213+
"enum": [
214+
"teacher",
215+
"student",
216+
"Other"
217+
],
218+
"type": "string",
219+
"x-enum-comments": {
220+
"Other": "Other",
221+
"Student": "student",
222+
"Teacher": "teacher"
223+
},
224+
"x-enum-varnames": [
225+
"Teacher",
226+
"Student",
227+
"Other"
228+
],
229+
"name": "type",
230+
"in": "formData"
231+
}
232+
],
233+
"responses": {
234+
"200": {
235+
"description": "ok"
236+
}
237+
}
238+
}
171239
}
172240
},
173241
"definitions": {

testdata/enums/types/model.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,10 @@ type Person struct {
5555
Mask Mask
5656
Type Type
5757
}
58+
59+
type PersonWithArrayEnum struct {
60+
Name string
61+
Class []Class
62+
Mask []Mask
63+
Type Type
64+
}

0 commit comments

Comments
 (0)