Skip to content

Commit

Permalink
fix field of enum type as formdata (#1511)
Browse files Browse the repository at this point in the history
* fix field of enum type as formdata

Signed-off-by: sdghchj <sdghchj@qq.com>
  • Loading branch information
sdghchj authored Mar 26, 2023
1 parent 122a2e2 commit c9bca77
Show file tree
Hide file tree
Showing 5 changed files with 204 additions and 11 deletions.
7 changes: 5 additions & 2 deletions operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,9 +305,12 @@ func (operation *Operation) ParseParamComment(commentLine string, astFile *ast.F
items := schema.Properties.ToOrderedSchemaItems()

for _, item := range items {
name, prop := item.Name, item.Schema
name, prop := item.Name, &item.Schema
if len(prop.Type) == 0 {
continue
prop = operation.parser.getUnderlyingSchema(prop)
if len(prop.Type) == 0 {
continue
}
}

var formName = name
Expand Down
33 changes: 25 additions & 8 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -1430,24 +1430,41 @@ func getFieldType(file *ast.File, field ast.Expr, genericParamTypeDefs map[strin
}
}

// GetSchemaTypePath get path of schema type.
func (parser *Parser) GetSchemaTypePath(schema *spec.Schema, depth int) []string {
if schema == nil || depth == 0 {
func (parser *Parser) getUnderlyingSchema(schema *spec.Schema) *spec.Schema {
if schema == nil {
return nil
}

name := schema.Ref.String()
if name != "" {
if pos := strings.LastIndexByte(name, '/'); pos >= 0 {
name = name[pos+1:]
if url := schema.Ref.GetURL(); url != nil {
if pos := strings.LastIndexByte(url.Fragment, '/'); pos >= 0 {
name := url.Fragment[pos+1:]
if schema, ok := parser.swagger.Definitions[name]; ok {
return parser.GetSchemaTypePath(&schema, depth)
return &schema
}
}
}

if len(schema.AllOf) > 0 {
merged := &spec.Schema{}
MergeSchema(merged, schema)
for _, s := range schema.AllOf {
MergeSchema(merged, parser.getUnderlyingSchema(&s))
}
return merged
}
return nil
}

// GetSchemaTypePath get path of schema type.
func (parser *Parser) GetSchemaTypePath(schema *spec.Schema, depth int) []string {
if schema == nil || depth == 0 {
return nil
}

if underlying := parser.getUnderlyingSchema(schema); underlying != nil {
return parser.GetSchemaTypePath(underlying, depth)
}

if len(schema.Type) > 0 {
switch schema.Type[0] {
case ARRAY:
Expand Down
78 changes: 77 additions & 1 deletion schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package swag
import (
"errors"
"fmt"

"github.com/go-openapi/spec"
)

Expand Down Expand Up @@ -215,3 +214,80 @@ func BuildCustomSchema(types []string) (*spec.Schema, error) {
return PrimitiveSchema(types[0]), nil
}
}

// MergeSchema merge schemas
func MergeSchema(dst *spec.Schema, src *spec.Schema) *spec.Schema {
if len(src.Type) > 0 {
dst.Type = src.Type
}
if len(src.Properties) > 0 {
dst.Properties = src.Properties
}
if src.Items != nil {
dst.Items = src.Items
}
if src.AdditionalProperties != nil {
dst.AdditionalProperties = src.AdditionalProperties
}
if len(src.Description) > 0 {
dst.Description = src.Description
}
if src.Nullable {
dst.Nullable = src.Nullable
}
if len(src.Format) > 0 {
dst.Format = src.Format
}
if src.Default != nil {
dst.Default = src.Default
}
if src.Example != nil {
dst.Example = src.Example
}
if len(src.Extensions) > 0 {
dst.Extensions = src.Extensions
}
if src.Maximum != nil {
dst.Maximum = src.Maximum
}
if src.Minimum != nil {
dst.Minimum = src.Minimum
}
if src.ExclusiveMaximum {
dst.ExclusiveMaximum = src.ExclusiveMaximum
}
if src.ExclusiveMinimum {
dst.ExclusiveMinimum = src.ExclusiveMinimum
}
if src.MaxLength != nil {
dst.MaxLength = src.MaxLength
}
if src.MinLength != nil {
dst.MinLength = src.MinLength
}
if len(src.Pattern) > 0 {
dst.Pattern = src.Pattern
}
if src.MaxItems != nil {
dst.MaxItems = src.MaxItems
}
if src.MinItems != nil {
dst.MinItems = src.MinItems
}
if src.UniqueItems {
dst.UniqueItems = src.UniqueItems
}
if src.MultipleOf != nil {
dst.MultipleOf = src.MultipleOf
}
if len(src.Enum) > 0 {
dst.Enum = src.Enum
}
if len(src.Extensions) > 0 {
dst.Extensions = src.Extensions
}
if len(src.ExtraProps) > 0 {
dst.ExtraProps = src.ExtraProps
}
return dst
}
11 changes: 11 additions & 0 deletions testdata/enums/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,14 @@ func API() {
func API2() {
_ = types.Person{}
}

// post students
//
// @Summary test enums fields in formdata request
// @Description test enums fields in formdata request
// @Param stdeunt formData types.Person true "type"
// @Success 200 "ok"
// @Router /students2 [get]
func API3() {
_ = types.Person{}
}
86 changes: 86 additions & 0 deletions testdata/enums/expected.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,92 @@
}
}
}
},
"/students2": {
"get": {
"description": "test enums fields in formdata request",
"summary": "test enums fields in formdata request",
"parameters": [
{
"enum": [
-1,
1,
2,
3,
4,
5
],
"type": "integer",
"x-enum-comments": {
"A": "AAA",
"B": "BBB"
},
"x-enum-varnames": [
"None",
"A",
"B",
"C",
"D",
"F"
],
"name": "class",
"in": "formData"
},
{
"enum": [
1,
2,
4,
8
],
"type": "integer",
"x-enum-comments": {
"Mask1": "Mask1",
"Mask2": "Mask2",
"Mask3": "Mask3",
"Mask4": "Mask4"
},
"x-enum-varnames": [
"Mask1",
"Mask2",
"Mask3",
"Mask4"
],
"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

0 comments on commit c9bca77

Please sign in to comment.