Skip to content

Commit 4536bf2

Browse files
authored
fix: enums in body got parse incorrectly (#1625)
1 parent fe971d2 commit 4536bf2

File tree

2 files changed

+50
-12
lines changed

2 files changed

+50
-12
lines changed

operation.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,8 @@ func (operation *Operation) ParseParamComment(commentLine string, astFile *ast.F
381381
return fmt.Errorf("%s is not supported paramType", paramType)
382382
}
383383

384-
err := operation.parseParamAttribute(commentLine, objectType, refType, &param)
384+
err := operation.parseParamAttribute(commentLine, objectType, refType, paramType, &param)
385+
385386
if err != nil {
386387
return err
387388
}
@@ -436,7 +437,7 @@ var regexAttributes = map[string]*regexp.Regexp{
436437
schemaExampleTag: regexp.MustCompile(`(?i)\s+schemaExample\(.*\)`),
437438
}
438439

439-
func (operation *Operation) parseParamAttribute(comment, objectType, schemaType string, param *spec.Parameter) error {
440+
func (operation *Operation) parseParamAttribute(comment, objectType, schemaType, paramType string, param *spec.Parameter) error {
440441
schemaType = TransToValidSchemeType(schemaType)
441442

442443
for attrKey, re := range regexAttributes {
@@ -447,7 +448,7 @@ func (operation *Operation) parseParamAttribute(comment, objectType, schemaType
447448

448449
switch attrKey {
449450
case enumsTag:
450-
err = setEnumParam(param, attr, objectType, schemaType)
451+
err = setEnumParam(param, attr, objectType, schemaType, paramType)
451452
case minimumTag, maximumTag:
452453
err = setNumberParam(param, attrKey, schemaType, attr, comment)
453454
case defaultTag:
@@ -526,7 +527,7 @@ func setNumberParam(param *spec.Parameter, name, schemaType, attr, commentLine s
526527
}
527528
}
528529

529-
func setEnumParam(param *spec.Parameter, attr, objectType, schemaType string) error {
530+
func setEnumParam(param *spec.Parameter, attr, objectType, schemaType, paramType string) error {
530531
for _, e := range strings.Split(attr, ",") {
531532
e = strings.TrimSpace(e)
532533

@@ -539,7 +540,12 @@ func setEnumParam(param *spec.Parameter, attr, objectType, schemaType string) er
539540
case ARRAY:
540541
param.Items.Enum = append(param.Items.Enum, value)
541542
default:
542-
param.Enum = append(param.Enum, value)
543+
switch paramType {
544+
case "body":
545+
param.Schema.Enum = append(param.Schema.Enum, value)
546+
default:
547+
param.Enum = append(param.Enum, value)
548+
}
543549
}
544550
}
545551

operation_test.go

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1383,6 +1383,36 @@ func TestParseParamCommentByBodyTextPlain(t *testing.T) {
13831383
assert.Equal(t, expected, string(b))
13841384
}
13851385

1386+
// TODO: fix this
1387+
func TestParseParamCommentByBodyEnumsText(t *testing.T) {
1388+
t.Parallel()
1389+
1390+
comment := `@Param text body string true "description" Enums(ENUM1, ENUM2, ENUM3)`
1391+
operation := NewOperation(nil)
1392+
1393+
err := operation.ParseComment(comment, nil)
1394+
1395+
assert.NoError(t, err)
1396+
b, _ := json.MarshalIndent(operation.Parameters, "", " ")
1397+
expected := `[
1398+
{
1399+
"description": "description",
1400+
"name": "text",
1401+
"in": "body",
1402+
"required": true,
1403+
"schema": {
1404+
"type": "string",
1405+
"enum": [
1406+
"ENUM1",
1407+
"ENUM2",
1408+
"ENUM3"
1409+
]
1410+
}
1411+
}
1412+
]`
1413+
assert.Equal(t, expected, string(b))
1414+
}
1415+
13861416
func TestParseParamCommentByBodyTypeWithDeepNestedFields(t *testing.T) {
13871417
t.Parallel()
13881418

@@ -1968,6 +1998,7 @@ func TestParseAndExtractionParamAttribute(t *testing.T) {
19681998
" default(1) maximum(100) minimum(0) format(csv)",
19691999
"",
19702000
NUMBER,
2001+
"",
19712002
&numberParam,
19722003
)
19732004
assert.NoError(t, err)
@@ -1976,38 +2007,39 @@ func TestParseAndExtractionParamAttribute(t *testing.T) {
19762007
assert.Equal(t, "csv", numberParam.SimpleSchema.Format)
19772008
assert.Equal(t, float64(1), numberParam.Default)
19782009

1979-
err = op.parseParamAttribute(" minlength(1)", "", NUMBER, nil)
2010+
err = op.parseParamAttribute(" minlength(1)", "", NUMBER, "", nil)
19802011
assert.Error(t, err)
19812012

1982-
err = op.parseParamAttribute(" maxlength(1)", "", NUMBER, nil)
2013+
err = op.parseParamAttribute(" maxlength(1)", "", NUMBER, "", nil)
19832014
assert.Error(t, err)
19842015

19852016
stringParam := spec.Parameter{}
19862017
err = op.parseParamAttribute(
19872018
" default(test) maxlength(100) minlength(0) format(csv)",
19882019
"",
19892020
STRING,
2021+
"",
19902022
&stringParam,
19912023
)
19922024
assert.NoError(t, err)
19932025
assert.Equal(t, int64(0), *stringParam.MinLength)
19942026
assert.Equal(t, int64(100), *stringParam.MaxLength)
19952027
assert.Equal(t, "csv", stringParam.SimpleSchema.Format)
1996-
err = op.parseParamAttribute(" minimum(0)", "", STRING, nil)
2028+
err = op.parseParamAttribute(" minimum(0)", "", STRING, "", nil)
19972029
assert.Error(t, err)
19982030

1999-
err = op.parseParamAttribute(" maximum(0)", "", STRING, nil)
2031+
err = op.parseParamAttribute(" maximum(0)", "", STRING, "", nil)
20002032
assert.Error(t, err)
20012033

20022034
arrayParram := spec.Parameter{}
2003-
err = op.parseParamAttribute(" collectionFormat(tsv)", ARRAY, STRING, &arrayParram)
2035+
err = op.parseParamAttribute(" collectionFormat(tsv)", ARRAY, STRING, "", &arrayParram)
20042036
assert.Equal(t, "tsv", arrayParram.CollectionFormat)
20052037
assert.NoError(t, err)
20062038

2007-
err = op.parseParamAttribute(" collectionFormat(tsv)", STRING, STRING, nil)
2039+
err = op.parseParamAttribute(" collectionFormat(tsv)", STRING, STRING, "", nil)
20082040
assert.Error(t, err)
20092041

2010-
err = op.parseParamAttribute(" default(0)", "", ARRAY, nil)
2042+
err = op.parseParamAttribute(" default(0)", "", ARRAY, "", nil)
20112043
assert.NoError(t, err)
20122044
}
20132045

0 commit comments

Comments
 (0)