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

#1140 added SchemaExample() to set the example to schema #1148

Merged
merged 1 commit into from Mar 7, 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
18 changes: 18 additions & 0 deletions operation.go
Expand Up @@ -360,6 +360,7 @@ const (
defaultTag = "default"
enumsTag = "enums"
exampleTag = "example"
schemaExampleTag = "schemaExample"
formatTag = "format"
validateTag = "validate"
minimumTag = "minimum"
Expand Down Expand Up @@ -393,6 +394,8 @@ var regexAttributes = map[string]*regexp.Regexp{
collectionFormatTag: regexp.MustCompile(`(?i)\s+collectionFormat\(.*\)`),
// example(0)
exampleTag: regexp.MustCompile(`(?i)\s+example\(.*\)`),
// schemaExample(0)
schemaExampleTag: regexp.MustCompile(`(?i)\s+schemaExample\(.*\)`),
}

func (operation *Operation) parseAndExtractionParamAttribute(commentLine, objectType, schemaType string, param *spec.Parameter) error {
Expand All @@ -415,6 +418,8 @@ func (operation *Operation) parseAndExtractionParamAttribute(commentLine, object
param.Format = attr
case exampleTag:
err = setExample(param, schemaType, attr)
case schemaExampleTag:
err = setSchemaExample(param, schemaType, attr)
case extensionsTag:
_ = setExtensionParam(param, attr)
case collectionFormatTag:
Expand Down Expand Up @@ -530,6 +535,19 @@ func setDefault(param *spec.Parameter, schemaType string, value string) error {
return nil
}

func setSchemaExample(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
}
// skip schema
if param.Schema == nil {
return nil
}
param.Schema.Example = val
return nil
}

func setExample(param *spec.Parameter, schemaType string, value string) error {
val, err := defineType(schemaType, value)
if err != nil {
Expand Down
45 changes: 45 additions & 0 deletions operation_test.go
Expand Up @@ -1882,6 +1882,51 @@ func TestParseParamCommentByExampleUnsupportedType(t *testing.T) {
assert.Equal(t, param.Example, float64(10))
}

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

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

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

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

setSchemaExample(&param, "something", "random value")
assert.Nil(t, param.Schema)

setSchemaExample(&param, STRING, "string value")
assert.Nil(t, param.Schema)

param.Schema = &spec.Schema{}
setSchemaExample(&param, STRING, "string value")
assert.Equal(t, param.Schema.Example, "string value")

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

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

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

Expand Down