Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add example parameter #1122

Merged
merged 2 commits into from Feb 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 13 additions & 0 deletions operation.go
Expand Up @@ -391,6 +391,8 @@ var regexAttributes = map[string]*regexp.Regexp{
extensionsTag: regexp.MustCompile(`(?i)\s+extensions\(.*\)`),
// for collectionFormat(csv)
collectionFormatTag: regexp.MustCompile(`(?i)\s+collectionFormat\(.*\)`),
// example(0)
exampleTag: regexp.MustCompile(`(?i)\s+example\(.*\)`),
}

func (operation *Operation) parseAndExtractionParamAttribute(commentLine, objectType, schemaType string, param *spec.Parameter) error {
Expand All @@ -411,6 +413,8 @@ func (operation *Operation) parseAndExtractionParamAttribute(commentLine, object
err = setStringParam(param, attrKey, schemaType, attr, commentLine)
case formatTag:
param.Format = attr
case exampleTag:
err = setExample(param, schemaType, attr)
case extensionsTag:
_ = setExtensionParam(param, attr)
case collectionFormatTag:
Expand Down Expand Up @@ -526,6 +530,15 @@ func setDefault(param *spec.Parameter, schemaType string, value string) error {
return nil
}

func setExample(param *spec.Parameter, schemaType string, value string) error {
val, err := defineType(schemaType, value)
if err != nil {
return nil // Don't set a example value if it's not valid
}
param.Example = val
return nil
}

// defineType enum value define the type (object and array unsupported).
func defineType(schemaType string, value string) (v interface{}, err error) {
schemaType = TransToValidSchemeType(schemaType)
Expand Down
61 changes: 61 additions & 0 deletions operation_test.go
Expand Up @@ -1821,6 +1821,67 @@ func TestParseParamCommentByDefault(t *testing.T) {
assert.Equal(t, expected, string(b))
}

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

comment := `@Param some_id query int true "Some ID" Example(10)`
operation := NewOperation(nil)
err := operation.ParseComment(comment, nil)

assert.NoError(t, err)
b, _ := json.MarshalIndent(operation.Parameters, "", " ")
expected := `[
{
"type": "integer",
"example": 10,
"description": "Some ID",
"name": "some_id",
"in": "query",
"required": true
}
]`
assert.Equal(t, expected, string(b))
}

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

comment := `@Param some_id query string true "Some ID" Example(True feelings)`
operation := NewOperation(nil)
err := operation.ParseComment(comment, nil)

assert.NoError(t, err)
b, _ := json.MarshalIndent(operation.Parameters, "", " ")
expected := `[
{
"type": "string",
"example": "True feelings",
"description": "Some ID",
"name": "some_id",
"in": "query",
"required": true
}
]`
assert.Equal(t, expected, string(b))
}

func TestParseParamCommentByExampleUnsupportedType(t *testing.T) {
t.Parallel()
var param spec.Parameter

setExample(&param, "something", "random value")
assert.Equal(t, param.Example, nil)

setExample(&param, STRING, "string value")
assert.Equal(t, param.Example, "string value")

setExample(&param, INTEGER, "10")
assert.Equal(t, param.Example, 10)

setExample(&param, NUMBER, "10")
assert.Equal(t, param.Example, float64(10))
}

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

Expand Down