From 6d6791c105b38bb1990de1a7b0a590ef26e21598 Mon Sep 17 00:00:00 2001 From: hohobilly Date: Tue, 18 Jul 2023 01:34:46 +0900 Subject: [PATCH] fix: enums in body got parse incorrectly --- operation.go | 16 +++++++++++----- operation_test.go | 46 +++++++++++++++++++++++++++++++++++++++------- 2 files changed, 50 insertions(+), 12 deletions(-) diff --git a/operation.go b/operation.go index 3485f51f4..8583f5f97 100644 --- a/operation.go +++ b/operation.go @@ -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, ¶m) + err := operation.parseParamAttribute(commentLine, objectType, refType, paramType, ¶m) + if err != nil { return err } @@ -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 { @@ -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: @@ -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) @@ -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) + } } } diff --git a/operation_test.go b/operation_test.go index e71bda4ee..6b684bc2c 100644 --- a/operation_test.go +++ b/operation_test.go @@ -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() @@ -1968,6 +1998,7 @@ func TestParseAndExtractionParamAttribute(t *testing.T) { " default(1) maximum(100) minimum(0) format(csv)", "", NUMBER, + "", &numberParam, ) assert.NoError(t, err) @@ -1976,10 +2007,10 @@ 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{} @@ -1987,27 +2018,28 @@ func TestParseAndExtractionParamAttribute(t *testing.T) { " 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) }