Skip to content

Commit c9bca77

Browse files
authored
fix field of enum type as formdata (#1511)
* fix field of enum type as formdata Signed-off-by: sdghchj <[email protected]>
1 parent 122a2e2 commit c9bca77

File tree

5 files changed

+204
-11
lines changed

5 files changed

+204
-11
lines changed

operation.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -305,9 +305,12 @@ func (operation *Operation) ParseParamComment(commentLine string, astFile *ast.F
305305
items := schema.Properties.ToOrderedSchemaItems()
306306

307307
for _, item := range items {
308-
name, prop := item.Name, item.Schema
308+
name, prop := item.Name, &item.Schema
309309
if len(prop.Type) == 0 {
310-
continue
310+
prop = operation.parser.getUnderlyingSchema(prop)
311+
if len(prop.Type) == 0 {
312+
continue
313+
}
311314
}
312315

313316
var formName = name

parser.go

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1430,24 +1430,41 @@ func getFieldType(file *ast.File, field ast.Expr, genericParamTypeDefs map[strin
14301430
}
14311431
}
14321432

1433-
// GetSchemaTypePath get path of schema type.
1434-
func (parser *Parser) GetSchemaTypePath(schema *spec.Schema, depth int) []string {
1435-
if schema == nil || depth == 0 {
1433+
func (parser *Parser) getUnderlyingSchema(schema *spec.Schema) *spec.Schema {
1434+
if schema == nil {
14361435
return nil
14371436
}
14381437

1439-
name := schema.Ref.String()
1440-
if name != "" {
1441-
if pos := strings.LastIndexByte(name, '/'); pos >= 0 {
1442-
name = name[pos+1:]
1438+
if url := schema.Ref.GetURL(); url != nil {
1439+
if pos := strings.LastIndexByte(url.Fragment, '/'); pos >= 0 {
1440+
name := url.Fragment[pos+1:]
14431441
if schema, ok := parser.swagger.Definitions[name]; ok {
1444-
return parser.GetSchemaTypePath(&schema, depth)
1442+
return &schema
14451443
}
14461444
}
1445+
}
14471446

1447+
if len(schema.AllOf) > 0 {
1448+
merged := &spec.Schema{}
1449+
MergeSchema(merged, schema)
1450+
for _, s := range schema.AllOf {
1451+
MergeSchema(merged, parser.getUnderlyingSchema(&s))
1452+
}
1453+
return merged
1454+
}
1455+
return nil
1456+
}
1457+
1458+
// GetSchemaTypePath get path of schema type.
1459+
func (parser *Parser) GetSchemaTypePath(schema *spec.Schema, depth int) []string {
1460+
if schema == nil || depth == 0 {
14481461
return nil
14491462
}
14501463

1464+
if underlying := parser.getUnderlyingSchema(schema); underlying != nil {
1465+
return parser.GetSchemaTypePath(underlying, depth)
1466+
}
1467+
14511468
if len(schema.Type) > 0 {
14521469
switch schema.Type[0] {
14531470
case ARRAY:

schema.go

Lines changed: 77 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package swag
33
import (
44
"errors"
55
"fmt"
6-
76
"github.com/go-openapi/spec"
87
)
98

@@ -215,3 +214,80 @@ func BuildCustomSchema(types []string) (*spec.Schema, error) {
215214
return PrimitiveSchema(types[0]), nil
216215
}
217216
}
217+
218+
// MergeSchema merge schemas
219+
func MergeSchema(dst *spec.Schema, src *spec.Schema) *spec.Schema {
220+
if len(src.Type) > 0 {
221+
dst.Type = src.Type
222+
}
223+
if len(src.Properties) > 0 {
224+
dst.Properties = src.Properties
225+
}
226+
if src.Items != nil {
227+
dst.Items = src.Items
228+
}
229+
if src.AdditionalProperties != nil {
230+
dst.AdditionalProperties = src.AdditionalProperties
231+
}
232+
if len(src.Description) > 0 {
233+
dst.Description = src.Description
234+
}
235+
if src.Nullable {
236+
dst.Nullable = src.Nullable
237+
}
238+
if len(src.Format) > 0 {
239+
dst.Format = src.Format
240+
}
241+
if src.Default != nil {
242+
dst.Default = src.Default
243+
}
244+
if src.Example != nil {
245+
dst.Example = src.Example
246+
}
247+
if len(src.Extensions) > 0 {
248+
dst.Extensions = src.Extensions
249+
}
250+
if src.Maximum != nil {
251+
dst.Maximum = src.Maximum
252+
}
253+
if src.Minimum != nil {
254+
dst.Minimum = src.Minimum
255+
}
256+
if src.ExclusiveMaximum {
257+
dst.ExclusiveMaximum = src.ExclusiveMaximum
258+
}
259+
if src.ExclusiveMinimum {
260+
dst.ExclusiveMinimum = src.ExclusiveMinimum
261+
}
262+
if src.MaxLength != nil {
263+
dst.MaxLength = src.MaxLength
264+
}
265+
if src.MinLength != nil {
266+
dst.MinLength = src.MinLength
267+
}
268+
if len(src.Pattern) > 0 {
269+
dst.Pattern = src.Pattern
270+
}
271+
if src.MaxItems != nil {
272+
dst.MaxItems = src.MaxItems
273+
}
274+
if src.MinItems != nil {
275+
dst.MinItems = src.MinItems
276+
}
277+
if src.UniqueItems {
278+
dst.UniqueItems = src.UniqueItems
279+
}
280+
if src.MultipleOf != nil {
281+
dst.MultipleOf = src.MultipleOf
282+
}
283+
if len(src.Enum) > 0 {
284+
dst.Enum = src.Enum
285+
}
286+
if len(src.Extensions) > 0 {
287+
dst.Extensions = src.Extensions
288+
}
289+
if len(src.ExtraProps) > 0 {
290+
dst.ExtraProps = src.ExtraProps
291+
}
292+
return dst
293+
}

testdata/enums/api/api.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,14 @@ func API() {
2424
func API2() {
2525
_ = types.Person{}
2626
}
27+
28+
// post students
29+
//
30+
// @Summary test enums fields in formdata request
31+
// @Description test enums fields in formdata request
32+
// @Param stdeunt formData types.Person true "type"
33+
// @Success 200 "ok"
34+
// @Router /students2 [get]
35+
func API3() {
36+
_ = types.Person{}
37+
}

testdata/enums/expected.json

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,92 @@
8282
}
8383
}
8484
}
85+
},
86+
"/students2": {
87+
"get": {
88+
"description": "test enums fields in formdata request",
89+
"summary": "test enums fields in formdata request",
90+
"parameters": [
91+
{
92+
"enum": [
93+
-1,
94+
1,
95+
2,
96+
3,
97+
4,
98+
5
99+
],
100+
"type": "integer",
101+
"x-enum-comments": {
102+
"A": "AAA",
103+
"B": "BBB"
104+
},
105+
"x-enum-varnames": [
106+
"None",
107+
"A",
108+
"B",
109+
"C",
110+
"D",
111+
"F"
112+
],
113+
"name": "class",
114+
"in": "formData"
115+
},
116+
{
117+
"enum": [
118+
1,
119+
2,
120+
4,
121+
8
122+
],
123+
"type": "integer",
124+
"x-enum-comments": {
125+
"Mask1": "Mask1",
126+
"Mask2": "Mask2",
127+
"Mask3": "Mask3",
128+
"Mask4": "Mask4"
129+
},
130+
"x-enum-varnames": [
131+
"Mask1",
132+
"Mask2",
133+
"Mask3",
134+
"Mask4"
135+
],
136+
"name": "mask",
137+
"in": "formData"
138+
},
139+
{
140+
"type": "string",
141+
"name": "name",
142+
"in": "formData"
143+
},
144+
{
145+
"enum": [
146+
"teacher",
147+
"student",
148+
"Other"
149+
],
150+
"type": "string",
151+
"x-enum-comments": {
152+
"Other": "Other",
153+
"Student": "student",
154+
"Teacher": "teacher"
155+
},
156+
"x-enum-varnames": [
157+
"Teacher",
158+
"Student",
159+
"Other"
160+
],
161+
"name": "type",
162+
"in": "formData"
163+
}
164+
],
165+
"responses": {
166+
"200": {
167+
"description": "ok"
168+
}
169+
}
170+
}
85171
}
86172
},
87173
"definitions": {

0 commit comments

Comments
 (0)