Skip to content

Commit

Permalink
added SchemaExample() to set the example to schema #1140
Browse files Browse the repository at this point in the history
  • Loading branch information
mschneider82 committed Mar 7, 2022
1 parent faad956 commit 42e36c5
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
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
41 changes: 41 additions & 0 deletions operation_test.go
Expand Up @@ -1882,6 +1882,47 @@ 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.Equal(t, param.Example, nil)

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

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

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

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

Expand Down

0 comments on commit 42e36c5

Please sign in to comment.