Skip to content

Commit

Permalink
transform struct fields of array enum type in a struct to query/formD…
Browse files Browse the repository at this point in the history
…ata params (#1523)

Signed-off-by: sdghchj <[email protected]>
  • Loading branch information
sdghchj authored Apr 4, 2023
1 parent 314d61f commit e67c965
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 7 deletions.
22 changes: 16 additions & 6 deletions operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,16 +321,26 @@ func (operation *Operation) ParseParamComment(commentLine string, astFile *ast.F
}

switch {
case prop.Type[0] == ARRAY && prop.Items.Schema != nil &&
len(prop.Items.Schema.Type) > 0 && IsSimplePrimitiveType(prop.Items.Schema.Type[0]):

param = createParameter(paramType, prop.Description, formName, prop.Type[0], prop.Items.Schema.Type[0], findInSlice(schema.Required, name), enums, operation.parser.collectionFormatInQuery)
case prop.Type[0] == ARRAY:
if prop.Items.Schema == nil {
continue
}
itemSchema := prop.Items.Schema
if len(itemSchema.Type) == 0 {
itemSchema = operation.parser.getUnderlyingSchema(prop.Items.Schema)
}
if len(itemSchema.Type) == 0 {
continue
}
if !IsSimplePrimitiveType(itemSchema.Type[0]) {
continue
}
param = createParameter(paramType, prop.Description, formName, prop.Type[0], itemSchema.Type[0], findInSlice(schema.Required, name), itemSchema.Enum, operation.parser.collectionFormatInQuery)

case IsSimplePrimitiveType(prop.Type[0]):
param = createParameter(paramType, prop.Description, formName, PRIMITIVE, prop.Type[0], findInSlice(schema.Required, name), enums, operation.parser.collectionFormatInQuery)
param = createParameter(paramType, prop.Description, formName, PRIMITIVE, prop.Type[0], findInSlice(schema.Required, name), nil, operation.parser.collectionFormatInQuery)
default:
operation.parser.debug.Printf("skip field [%s] in %s is not supported type for %s", name, refType, paramType)

continue
}

Expand Down
13 changes: 12 additions & 1 deletion testdata/enums/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,20 @@ func API2() {
//
// @Summary test enums fields in formdata request
// @Description test enums fields in formdata request
// @Param stdeunt formData types.Person true "type"
// @Param student formData types.Person true "type"
// @Success 200 "ok"
// @Router /students2 [get]
func API3() {
_ = types.Person{}
}

// post students
//
// @Summary test array enums fields in formdata request
// @Description test array enums fields in formdata request
// @Param student formData types.PersonWithArrayEnum true "type"
// @Success 200 "ok"
// @Router /students4 [get]
func API4() {
_ = types.Person{}
}
68 changes: 68 additions & 0 deletions testdata/enums/expected.json
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,74 @@
}
}
}
},
"/students4": {
"get": {
"description": "test array enums fields in formdata request",
"summary": "test array enums fields in formdata request",
"parameters": [
{
"type": "array",
"items": {
"enum": [
-1,
1,
2,
3,
4,
5
],
"type": "integer"
},
"name": "class",
"in": "formData"
},
{
"type": "array",
"items": {
"enum": [
1,
2,
4,
8
],
"type": "integer"
},
"name": "mask",
"in": "formData"
},
{
"type": "string",
"name": "name",
"in": "formData"
},
{
"enum": [
"teacher",
"student",
"Other"
],
"type": "string",
"x-enum-comments": {
"Other": "Other",
"Student": "student",
"Teacher": "teacher"
},
"x-enum-varnames": [
"Teacher",
"Student",
"Other"
],
"name": "type",
"in": "formData"
}
],
"responses": {
"200": {
"description": "ok"
}
}
}
}
},
"definitions": {
Expand Down
7 changes: 7 additions & 0 deletions testdata/enums/types/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,10 @@ type Person struct {
Mask Mask
Type Type
}

type PersonWithArrayEnum struct {
Name string
Class []Class
Mask []Mask
Type Type
}

0 comments on commit e67c965

Please sign in to comment.