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

Support primitive type with format in response #1754

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -978,7 +978,15 @@ func (operation *Operation) parseAPIObjectSchema(commentLine, schemaType, refTyp

return spec.ArrayProperty(schema), nil
default:
return PrimitiveSchema(schemaType), nil
schema := PrimitiveSchema(schemaType)
if refType != schemaType {
//refer to https://swagger.io/specification/v2/#dataTypeFormat
if !validPrimitiveFormat[schemaType][refType] {
return nil, fmt.Errorf("invalid format %s of type %s", refType, schemaType)
}
schema.Format = refType
}
return schema, nil
}
}

Expand Down
29 changes: 28 additions & 1 deletion operation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ func TestOperation_ParseResponseWithDefault(t *testing.T) {

assert.Equal(t, "An empty response", operation.Responses.Default.Description)

comment = `@Success 200,default {string} Response "A response"`
comment = `@Success 200,default {string} string "A response"`
operation = NewOperation(nil)

err = operation.ParseComment(comment, nil)
Expand All @@ -244,6 +244,33 @@ func TestOperation_ParseResponseWithDefault(t *testing.T) {
assert.Equal(t, "A response", operation.Responses.StatusCodeResponses[200].Description)
}

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

comment := `@Success 200 {integer} int16 "response with format"`
operation := NewOperation(nil)
err := operation.ParseComment(comment, nil)
assert.Error(t, err)

comment = `@Success 200 {integer} int32 "response with format"`
operation = NewOperation(nil)
err = operation.ParseComment(comment, nil)
assert.NoError(t, err)
b, _ := json.MarshalIndent(operation, "", " ")
expected := `{
"responses": {
"200": {
"description": "response with format",
"schema": {
"type": "integer",
"format": "int32"
}
}
}
}`
assert.Equal(t, expected, string(b))
}

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

Expand Down
6 changes: 6 additions & 0 deletions schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ const (
IgnoreNameOverridePrefix = '$'
)

var validPrimitiveFormat = map[string]map[string]bool{
INTEGER: {"int32": true, "int64": true},
NUMBER: {"float": true, "double": true},
STRING: {"byte": true, "binary": true, "date": true, "date-time": true, "password": true},
}

// CheckSchemaType checks if typeName is not a name of primitive type.
func CheckSchemaType(typeName string) error {
if !IsPrimitiveType(typeName) {
Expand Down
2 changes: 1 addition & 1 deletion testdata/struct_comment/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import "net/http"
// @Accept json
// @Produce json
// @Param post_id path int true "Some ID" Format(int64)
// @Success 200 {string} web.Post
// @Success 200 {string} string ""
// @Failure 400 {object} web.APIError "We need ID!!"
// @Failure 404 {object} web.APIError "Can not find ID"
// @Router /posts/{post_id} [get]
Expand Down
Loading