Skip to content

Commit

Permalink
Omit to marshal response description as JSON when $ref is defined (go…
Browse files Browse the repository at this point in the history
…-openapi#127)

* Omit to marshal response description as JSON when $ref is defined

* contributes go-swagger/go-swagger#2429

Signed-off-by: Frederic BIDON <fredbi@yahoo.com>

* fixed the case for SecurityScheme marshaller

Signed-off-by: Frederic BIDON <fredbi@yahoo.com>

* removed cross-packages CI test with go-openapi/analysis

Signed-off-by: Frederic BIDON <fredbi@yahoo.com>

* fixup linting

Signed-off-by: Frederic BIDON <fredbi@yahoo.com>
  • Loading branch information
fredbi authored Nov 18, 2020
1 parent 19e53be commit 81c4553
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@ notifications:
script:
- gotestsum -f short-verbose -- -race -coverprofile=coverage.txt -covermode=atomic ./...
- go get -u github.com/go-openapi/analysis@master
- gotestsum -f short-verbose -- -timeout=30m github.com/go-openapi/analysis/...
#- gotestsum -f short-verbose -- -timeout=30m github.com/go-openapi/analysis/...
4 changes: 2 additions & 2 deletions auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ import (
)

func TestSerialization_AuthSerialization(t *testing.T) {
assertSerializeJSON(t, BasicAuth(), `{"type":"basic","authorizationUrl":""}`)
assertSerializeJSON(t, BasicAuth(), `{"type":"basic"}`)

assertSerializeJSON(t, APIKeyAuth("api-key", "header"), `{"type":"apiKey","name":"api-key","in":"header","authorizationUrl":""}`)
assertSerializeJSON(t, APIKeyAuth("api-key", "header"), `{"type":"apiKey","name":"api-key","in":"header"}`)

assertSerializeJSON(
t,
Expand Down
6 changes: 2 additions & 4 deletions expander_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,8 +276,7 @@ func TestResponseExpansion(t *testing.T) {
expected = spec.Responses["petResponse"]
jazon, _ = json.MarshalIndent(resp, "", " ")
assert.JSONEq(t, `{
"$ref": "#/responses/anotherPet",
"description":""
"$ref": "#/responses/anotherPet"
}`, string(jazon))

err = expandParameterOrResponse(&resp, resolver, basePath)
Expand All @@ -301,8 +300,7 @@ func TestResponseResolve(t *testing.T) {
// resolve resolves the ref, but dos not expand
jazon, _ := json.MarshalIndent(resp2, "", " ")
assert.JSONEq(t, `{
"$ref": "#/responses/petResponse",
"description":""
"$ref": "#/responses/petResponse"
}`, string(jazon))
}

Expand Down
23 changes: 22 additions & 1 deletion response.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,31 @@ func (r *Response) UnmarshalJSON(data []byte) error {

// MarshalJSON converts this items object to JSON
func (r Response) MarshalJSON() ([]byte, error) {
b1, err := json.Marshal(r.ResponseProps)
var (
b1 []byte
err error
)

if r.Ref.String() == "" {
// when there is no $ref, empty description is rendered as an empty string
b1, err = json.Marshal(r.ResponseProps)
} else {
// when there is $ref inside the schema, description should be omitempty-ied
b1, err = json.Marshal(struct {
Description string `json:"description,omitempty"`
Schema *Schema `json:"schema,omitempty"`
Headers map[string]Header `json:"headers,omitempty"`
Examples map[string]interface{} `json:"examples,omitempty"`
}{
Description: r.ResponseProps.Description,
Schema: r.ResponseProps.Schema,
Examples: r.ResponseProps.Examples,
})
}
if err != nil {
return nil, err
}

b2, err := json.Marshal(r.Refable)
if err != nil {
return nil, err
Expand Down
32 changes: 31 additions & 1 deletion security_scheme.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,40 @@ func (s SecurityScheme) JSONLookup(token string) (interface{}, error) {

// MarshalJSON marshal this to JSON
func (s SecurityScheme) MarshalJSON() ([]byte, error) {
b1, err := json.Marshal(s.SecuritySchemeProps)
var (
b1 []byte
err error
)

if s.Type == oauth2 {
// when oauth2, empty AuthorizationURL is added as empty string
b1, err = json.Marshal(s.SecuritySchemeProps)
} else {
// when not oauth2, empty AuthorizationURL should be omitted
b1, err = json.Marshal(struct {
Description string `json:"description,omitempty"`
Type string `json:"type"`
Name string `json:"name,omitempty"` // api key
In string `json:"in,omitempty"` // api key
Flow string `json:"flow,omitempty"` // oauth2
AuthorizationURL string `json:"authorizationUrl,omitempty"` // oauth2
TokenURL string `json:"tokenUrl,omitempty"` // oauth2
Scopes map[string]string `json:"scopes,omitempty"` // oauth2
}{
Description: s.Description,
Type: s.Type,
Name: s.Name,
In: s.In,
Flow: s.Flow,
AuthorizationURL: s.AuthorizationURL,
TokenURL: s.TokenURL,
Scopes: s.Scopes,
})
}
if err != nil {
return nil, err
}

b2, err := json.Marshal(s.VendorExtensible)
if err != nil {
return nil, err
Expand Down
3 changes: 1 addition & 2 deletions swagger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,7 @@ const specJSON = `{
"internalApiKey": {
"type": "apiKey",
"in": "header",
"name": "api_key",
"authorizationUrl": ""
"name": "api_key"
}
},
"security": [{"internalApiKey":[]}],
Expand Down

0 comments on commit 81c4553

Please sign in to comment.