Skip to content

Commit

Permalink
fix: enums in body got parse incorrectly (#1625)
Browse files Browse the repository at this point in the history
  • Loading branch information
hohobilly committed Jul 18, 2023
1 parent fe971d2 commit 4536bf2
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 12 deletions.
16 changes: 11 additions & 5 deletions operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,8 @@ func (operation *Operation) ParseParamComment(commentLine string, astFile *ast.F
return fmt.Errorf("%s is not supported paramType", paramType)
}

err := operation.parseParamAttribute(commentLine, objectType, refType, &param)
err := operation.parseParamAttribute(commentLine, objectType, refType, paramType, &param)

if err != nil {
return err
}
Expand Down Expand Up @@ -436,7 +437,7 @@ var regexAttributes = map[string]*regexp.Regexp{
schemaExampleTag: regexp.MustCompile(`(?i)\s+schemaExample\(.*\)`),
}

func (operation *Operation) parseParamAttribute(comment, objectType, schemaType string, param *spec.Parameter) error {
func (operation *Operation) parseParamAttribute(comment, objectType, schemaType, paramType string, param *spec.Parameter) error {
schemaType = TransToValidSchemeType(schemaType)

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

switch attrKey {
case enumsTag:
err = setEnumParam(param, attr, objectType, schemaType)
err = setEnumParam(param, attr, objectType, schemaType, paramType)
case minimumTag, maximumTag:
err = setNumberParam(param, attrKey, schemaType, attr, comment)
case defaultTag:
Expand Down Expand Up @@ -526,7 +527,7 @@ func setNumberParam(param *spec.Parameter, name, schemaType, attr, commentLine s
}
}

func setEnumParam(param *spec.Parameter, attr, objectType, schemaType string) error {
func setEnumParam(param *spec.Parameter, attr, objectType, schemaType, paramType string) error {
for _, e := range strings.Split(attr, ",") {
e = strings.TrimSpace(e)

Expand All @@ -539,7 +540,12 @@ func setEnumParam(param *spec.Parameter, attr, objectType, schemaType string) er
case ARRAY:
param.Items.Enum = append(param.Items.Enum, value)
default:
param.Enum = append(param.Enum, value)
switch paramType {
case "body":
param.Schema.Enum = append(param.Schema.Enum, value)
default:
param.Enum = append(param.Enum, value)
}
}
}

Expand Down
46 changes: 39 additions & 7 deletions operation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1383,6 +1383,36 @@ func TestParseParamCommentByBodyTextPlain(t *testing.T) {
assert.Equal(t, expected, string(b))
}

// TODO: fix this
func TestParseParamCommentByBodyEnumsText(t *testing.T) {
t.Parallel()

comment := `@Param text body string true "description" Enums(ENUM1, ENUM2, ENUM3)`
operation := NewOperation(nil)

err := operation.ParseComment(comment, nil)

assert.NoError(t, err)
b, _ := json.MarshalIndent(operation.Parameters, "", " ")
expected := `[
{
"description": "description",
"name": "text",
"in": "body",
"required": true,
"schema": {
"type": "string",
"enum": [
"ENUM1",
"ENUM2",
"ENUM3"
]
}
}
]`
assert.Equal(t, expected, string(b))
}

func TestParseParamCommentByBodyTypeWithDeepNestedFields(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -1968,6 +1998,7 @@ func TestParseAndExtractionParamAttribute(t *testing.T) {
" default(1) maximum(100) minimum(0) format(csv)",
"",
NUMBER,
"",
&numberParam,
)
assert.NoError(t, err)
Expand All @@ -1976,38 +2007,39 @@ func TestParseAndExtractionParamAttribute(t *testing.T) {
assert.Equal(t, "csv", numberParam.SimpleSchema.Format)
assert.Equal(t, float64(1), numberParam.Default)

err = op.parseParamAttribute(" minlength(1)", "", NUMBER, nil)
err = op.parseParamAttribute(" minlength(1)", "", NUMBER, "", nil)
assert.Error(t, err)

err = op.parseParamAttribute(" maxlength(1)", "", NUMBER, nil)
err = op.parseParamAttribute(" maxlength(1)", "", NUMBER, "", nil)
assert.Error(t, err)

stringParam := spec.Parameter{}
err = op.parseParamAttribute(
" default(test) maxlength(100) minlength(0) format(csv)",
"",
STRING,
"",
&stringParam,
)
assert.NoError(t, err)
assert.Equal(t, int64(0), *stringParam.MinLength)
assert.Equal(t, int64(100), *stringParam.MaxLength)
assert.Equal(t, "csv", stringParam.SimpleSchema.Format)
err = op.parseParamAttribute(" minimum(0)", "", STRING, nil)
err = op.parseParamAttribute(" minimum(0)", "", STRING, "", nil)
assert.Error(t, err)

err = op.parseParamAttribute(" maximum(0)", "", STRING, nil)
err = op.parseParamAttribute(" maximum(0)", "", STRING, "", nil)
assert.Error(t, err)

arrayParram := spec.Parameter{}
err = op.parseParamAttribute(" collectionFormat(tsv)", ARRAY, STRING, &arrayParram)
err = op.parseParamAttribute(" collectionFormat(tsv)", ARRAY, STRING, "", &arrayParram)
assert.Equal(t, "tsv", arrayParram.CollectionFormat)
assert.NoError(t, err)

err = op.parseParamAttribute(" collectionFormat(tsv)", STRING, STRING, nil)
err = op.parseParamAttribute(" collectionFormat(tsv)", STRING, STRING, "", nil)
assert.Error(t, err)

err = op.parseParamAttribute(" default(0)", "", ARRAY, nil)
err = op.parseParamAttribute(" default(0)", "", ARRAY, "", nil)
assert.NoError(t, err)
}

Expand Down

0 comments on commit 4536bf2

Please sign in to comment.