Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

transform struct fields of array enum type to query/formData params #1523

Merged
merged 1 commit into from
Apr 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}