Skip to content

Commit f85abf4

Browse files
author
Ahmad Moudani
committed
Fix #165
Signed-off-by: Ahmad Moudani <ahmad.moudani@crowdstrike.com>
1 parent df35a52 commit f85abf4

File tree

2 files changed

+153
-7
lines changed

2 files changed

+153
-7
lines changed

responses.go

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"fmt"
2020
"reflect"
2121
"strconv"
22+
"strings"
2223

2324
"github.com/go-openapi/swag"
2425
)
@@ -62,6 +63,7 @@ func (r *Responses) UnmarshalJSON(data []byte) error {
6263
if err := json.Unmarshal(data, &r.ResponsesProps); err != nil {
6364
return err
6465
}
66+
6567
if err := json.Unmarshal(data, &r.VendorExtensible); err != nil {
6668
return err
6769
}
@@ -107,20 +109,31 @@ func (r ResponsesProps) MarshalJSON() ([]byte, error) {
107109

108110
// UnmarshalJSON unmarshals responses from JSON
109111
func (r *ResponsesProps) UnmarshalJSON(data []byte) error {
110-
var res map[string]Response
112+
var res map[string]json.RawMessage
111113
if err := json.Unmarshal(data, &res); err != nil {
112-
return nil
114+
return err
113115
}
116+
114117
if v, ok := res["default"]; ok {
115-
r.Default = &v
118+
var defaultRes Response
119+
if err := json.Unmarshal(v, &defaultRes); err != nil {
120+
return err
121+
}
122+
r.Default = &defaultRes
116123
delete(res, "default")
117124
}
118125
for k, v := range res {
119-
if nk, err := strconv.Atoi(k); err == nil {
120-
if r.StatusCodeResponses == nil {
121-
r.StatusCodeResponses = map[int]Response{}
126+
if !strings.HasPrefix(k, "x-") {
127+
var statusCodeResp Response
128+
if err := json.Unmarshal(v, &statusCodeResp); err != nil {
129+
return err
130+
}
131+
if nk, err := strconv.Atoi(k); err == nil {
132+
if r.StatusCodeResponses == nil {
133+
r.StatusCodeResponses = map[int]Response{}
134+
}
135+
r.StatusCodeResponses[nk] = statusCodeResp
122136
}
123-
r.StatusCodeResponses[nk] = v
124137
}
125138
}
126139
return nil

responses_test.go

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
// Copyright 2017 go-swagger maintainers
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package spec
16+
17+
import (
18+
"encoding/json"
19+
"testing"
20+
21+
"github.com/stretchr/testify/assert"
22+
)
23+
24+
var responses = Responses{
25+
VendorExtensible: VendorExtensible{
26+
Extensions: map[string]interface{}{
27+
"x-go-name": "PutDogExists",
28+
},
29+
},
30+
ResponsesProps: ResponsesProps{
31+
StatusCodeResponses: map[int]Response{
32+
200: {
33+
Refable: Refable{Ref: MustCreateRef("Dog")},
34+
VendorExtensible: VendorExtensible{
35+
Extensions: map[string]interface{}{
36+
"x-go-name": "PutDogExists",
37+
},
38+
},
39+
ResponseProps: ResponseProps{
40+
Description: "Dog exists",
41+
Schema: &Schema{SchemaProps: SchemaProps{Type: []string{"string"}}},
42+
},
43+
},
44+
},
45+
},
46+
}
47+
48+
const responsesJSON = `{
49+
"x-go-name": "PutDogExists",
50+
"200": {
51+
"$ref": "Dog",
52+
"x-go-name": "PutDogExists",
53+
"description": "Dog exists",
54+
"schema": {
55+
"type": "string"
56+
}
57+
}
58+
}`
59+
60+
func TestIntegrationResponses(t *testing.T) {
61+
var actual Responses
62+
if assert.NoError(t, json.Unmarshal([]byte(responsesJSON), &actual)) {
63+
assert.EqualValues(t, actual, responses)
64+
}
65+
66+
assertParsesJSON(t, responsesJSON, responses)
67+
}
68+
69+
func TestJSONLookupResponses(t *testing.T) {
70+
resp200, ok := responses.StatusCodeResponses[200]
71+
if !assert.True(t, ok) {
72+
t.FailNow()
73+
return
74+
}
75+
76+
res, err := resp200.JSONLookup("$ref")
77+
if !assert.NoError(t, err) {
78+
t.FailNow()
79+
return
80+
}
81+
if assert.IsType(t, &Ref{}, res) {
82+
ref := res.(*Ref)
83+
assert.EqualValues(t, MustCreateRef("Dog"), *ref)
84+
}
85+
86+
var def string
87+
res, err = resp200.JSONLookup("description")
88+
if !assert.NoError(t, err) || !assert.NotNil(t, res) || !assert.IsType(t, def, res) {
89+
t.FailNow()
90+
return
91+
}
92+
def = res.(string)
93+
assert.Equal(t, "Dog exists", def)
94+
95+
var x *interface{}
96+
res, err = responses.JSONLookup("x-go-name")
97+
if !assert.NoError(t, err) || !assert.NotNil(t, res) || !assert.IsType(t, x, res) {
98+
t.FailNow()
99+
return
100+
}
101+
102+
x = res.(*interface{})
103+
assert.EqualValues(t, "PutDogExists", *x)
104+
105+
res, err = responses.JSONLookup("unknown")
106+
if !assert.Error(t, err) || !assert.Nil(t, res) {
107+
t.FailNow()
108+
return
109+
}
110+
}
111+
112+
func TestResponsesBuild(t *testing.T) {
113+
resp := NewResponse().
114+
WithDescription("some response").
115+
WithSchema(new(Schema).Typed("object", "")).
116+
AddHeader("x-header", ResponseHeader().Typed("string", "")).
117+
AddExample("application/json", `{"key":"value"}`)
118+
jazon, _ := json.MarshalIndent(resp, "", " ")
119+
assert.JSONEq(t, `{
120+
"description": "some response",
121+
"schema": {
122+
"type": "object"
123+
},
124+
"headers": {
125+
"x-header": {
126+
"type": "string"
127+
}
128+
},
129+
"examples": {
130+
"application/json": "{\"key\":\"value\"}"
131+
}
132+
}`, string(jazon))
133+
}

0 commit comments

Comments
 (0)