Skip to content

Commit ed98f50

Browse files
authored
Add missing yaml tags in marshaling openapi2.T (#391)
1 parent 7ee1637 commit ed98f50

File tree

3 files changed

+97
-80
lines changed

3 files changed

+97
-80
lines changed

openapi2/openapi2.go

Lines changed: 75 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,20 @@ import (
1212
// T is the root of an OpenAPI v2 document
1313
type T struct {
1414
openapi3.ExtensionProps
15-
Swagger string `json:"swagger"`
16-
Info openapi3.Info `json:"info"`
17-
ExternalDocs *openapi3.ExternalDocs `json:"externalDocs,omitempty"`
18-
Schemes []string `json:"schemes,omitempty"`
19-
Consumes []string `json:"consumes,omitempty"`
20-
Host string `json:"host,omitempty"`
21-
BasePath string `json:"basePath,omitempty"`
22-
Paths map[string]*PathItem `json:"paths,omitempty"`
23-
Definitions map[string]*openapi3.SchemaRef `json:"definitions,omitempty,noref"`
24-
Parameters map[string]*Parameter `json:"parameters,omitempty,noref"`
25-
Responses map[string]*Response `json:"responses,omitempty,noref"`
26-
SecurityDefinitions map[string]*SecurityScheme `json:"securityDefinitions,omitempty"`
27-
Security SecurityRequirements `json:"security,omitempty"`
28-
Tags openapi3.Tags `json:"tags,omitempty"`
15+
Swagger string `json:"swagger" yaml:"swagger"`
16+
Info openapi3.Info `json:"info" yaml:"info"`
17+
ExternalDocs *openapi3.ExternalDocs `json:"externalDocs,omitempty" yaml:"externalDocs,omitempty"`
18+
Schemes []string `json:"schemes,omitempty" yaml:"schemes,omitempty"`
19+
Consumes []string `json:"consumes,omitempty" yaml:"consumes,omitempty"`
20+
Host string `json:"host,omitempty" yaml:"host,omitempty"`
21+
BasePath string `json:"basePath,omitempty" yaml:"basePath,omitempty"`
22+
Paths map[string]*PathItem `json:"paths,omitempty" yaml:"paths,omitempty"`
23+
Definitions map[string]*openapi3.SchemaRef `json:"definitions,omitempty,noref" yaml:"definitions,omitempty,noref"`
24+
Parameters map[string]*Parameter `json:"parameters,omitempty,noref" yaml:"parameters,omitempty,noref"`
25+
Responses map[string]*Response `json:"responses,omitempty,noref" yaml:"responses,omitempty,noref"`
26+
SecurityDefinitions map[string]*SecurityScheme `json:"securityDefinitions,omitempty" yaml:"securityDefinitions,omitempty"`
27+
Security SecurityRequirements `json:"security,omitempty" yaml:"security,omitempty"`
28+
Tags openapi3.Tags `json:"tags,omitempty" yaml:"tags,omitempty"`
2929
}
3030

3131
func (doc *T) MarshalJSON() ([]byte, error) {
@@ -52,15 +52,15 @@ func (doc *T) AddOperation(path string, method string, operation *Operation) {
5252

5353
type PathItem struct {
5454
openapi3.ExtensionProps
55-
Ref string `json:"$ref,omitempty"`
56-
Delete *Operation `json:"delete,omitempty"`
57-
Get *Operation `json:"get,omitempty"`
58-
Head *Operation `json:"head,omitempty"`
59-
Options *Operation `json:"options,omitempty"`
60-
Patch *Operation `json:"patch,omitempty"`
61-
Post *Operation `json:"post,omitempty"`
62-
Put *Operation `json:"put,omitempty"`
63-
Parameters Parameters `json:"parameters,omitempty"`
55+
Ref string `json:"$ref,omitempty" yaml:"$ref,omitempty"`
56+
Delete *Operation `json:"delete,omitempty" yaml:"delete,omitempty"`
57+
Get *Operation `json:"get,omitempty" yaml:"get,omitempty"`
58+
Head *Operation `json:"head,omitempty" yaml:"head,omitempty"`
59+
Options *Operation `json:"options,omitempty" yaml:"options,omitempty"`
60+
Patch *Operation `json:"patch,omitempty" yaml:"patch,omitempty"`
61+
Post *Operation `json:"post,omitempty" yaml:"post,omitempty"`
62+
Put *Operation `json:"put,omitempty" yaml:"put,omitempty"`
63+
Parameters Parameters `json:"parameters,omitempty" yaml:"parameters,omitempty"`
6464
}
6565

6666
func (pathItem *PathItem) MarshalJSON() ([]byte, error) {
@@ -141,16 +141,16 @@ func (pathItem *PathItem) SetOperation(method string, operation *Operation) {
141141

142142
type Operation struct {
143143
openapi3.ExtensionProps
144-
Summary string `json:"summary,omitempty"`
145-
Description string `json:"description,omitempty"`
146-
ExternalDocs *openapi3.ExternalDocs `json:"externalDocs,omitempty"`
147-
Tags []string `json:"tags,omitempty"`
148-
OperationID string `json:"operationId,omitempty"`
149-
Parameters Parameters `json:"parameters,omitempty"`
150-
Responses map[string]*Response `json:"responses"`
151-
Consumes []string `json:"consumes,omitempty"`
152-
Produces []string `json:"produces,omitempty"`
153-
Security *SecurityRequirements `json:"security,omitempty"`
144+
Summary string `json:"summary,omitempty" yaml:"summary,omitempty"`
145+
Description string `json:"description,omitempty" yaml:"description,omitempty"`
146+
ExternalDocs *openapi3.ExternalDocs `json:"externalDocs,omitempty" yaml:"externalDocs,omitempty"`
147+
Tags []string `json:"tags,omitempty" yaml:"tags,omitempty"`
148+
OperationID string `json:"operationId,omitempty" yaml:"operationId,omitempty"`
149+
Parameters Parameters `json:"parameters,omitempty" yaml:"parameters,omitempty"`
150+
Responses map[string]*Response `json:"responses" yaml:"responses"`
151+
Consumes []string `json:"consumes,omitempty" yaml:"consumes,omitempty"`
152+
Produces []string `json:"produces,omitempty" yaml:"produces,omitempty"`
153+
Security *SecurityRequirements `json:"security,omitempty" yaml:"security,omitempty"`
154154
}
155155

156156
func (operation *Operation) MarshalJSON() ([]byte, error) {
@@ -179,30 +179,30 @@ func (ps Parameters) Less(i, j int) bool {
179179

180180
type Parameter struct {
181181
openapi3.ExtensionProps
182-
Ref string `json:"$ref,omitempty"`
183-
In string `json:"in,omitempty"`
184-
Name string `json:"name,omitempty"`
185-
Description string `json:"description,omitempty"`
186-
CollectionFormat string `json:"collectionFormat,omitempty"`
187-
Type string `json:"type,omitempty"`
188-
Format string `json:"format,omitempty"`
189-
Pattern string `json:"pattern,omitempty"`
190-
AllowEmptyValue bool `json:"allowEmptyValue,omitempty"`
191-
Required bool `json:"required,omitempty"`
192-
UniqueItems bool `json:"uniqueItems,omitempty"`
193-
ExclusiveMin bool `json:"exclusiveMinimum,omitempty"`
194-
ExclusiveMax bool `json:"exclusiveMaximum,omitempty"`
195-
Schema *openapi3.SchemaRef `json:"schema,omitempty"`
196-
Items *openapi3.SchemaRef `json:"items,omitempty"`
197-
Enum []interface{} `json:"enum,omitempty"`
198-
MultipleOf *float64 `json:"multipleOf,omitempty"`
199-
Minimum *float64 `json:"minimum,omitempty"`
200-
Maximum *float64 `json:"maximum,omitempty"`
201-
MaxLength *uint64 `json:"maxLength,omitempty"`
202-
MaxItems *uint64 `json:"maxItems,omitempty"`
203-
MinLength uint64 `json:"minLength,omitempty"`
204-
MinItems uint64 `json:"minItems,omitempty"`
205-
Default interface{} `json:"default,omitempty"`
182+
Ref string `json:"$ref,omitempty" yaml:"$ref,omitempty"`
183+
In string `json:"in,omitempty" yaml:"in,omitempty"`
184+
Name string `json:"name,omitempty" yaml:"name,omitempty"`
185+
Description string `json:"description,omitempty" yaml:"description,omitempty"`
186+
CollectionFormat string `json:"collectionFormat,omitempty" yaml:"collectionFormat,omitempty"`
187+
Type string `json:"type,omitempty" yaml:"type,omitempty"`
188+
Format string `json:"format,omitempty" yaml:"format,omitempty"`
189+
Pattern string `json:"pattern,omitempty" yaml:"pattern,omitempty"`
190+
AllowEmptyValue bool `json:"allowEmptyValue,omitempty" yaml:"allowEmptyValue,omitempty"`
191+
Required bool `json:"required,omitempty" yaml:"required,omitempty"`
192+
UniqueItems bool `json:"uniqueItems,omitempty" yaml:"uniqueItems,omitempty"`
193+
ExclusiveMin bool `json:"exclusiveMinimum,omitempty" yaml:"exclusiveMinimum,omitempty"`
194+
ExclusiveMax bool `json:"exclusiveMaximum,omitempty" yaml:"exclusiveMaximum,omitempty"`
195+
Schema *openapi3.SchemaRef `json:"schema,omitempty" yaml:"schema,omitempty"`
196+
Items *openapi3.SchemaRef `json:"items,omitempty" yaml:"items,omitempty"`
197+
Enum []interface{} `json:"enum,omitempty" yaml:"enum,omitempty"`
198+
MultipleOf *float64 `json:"multipleOf,omitempty" yaml:"multipleOf,omitempty"`
199+
Minimum *float64 `json:"minimum,omitempty" yaml:"minimum,omitempty"`
200+
Maximum *float64 `json:"maximum,omitempty" yaml:"maximum,omitempty"`
201+
MaxLength *uint64 `json:"maxLength,omitempty" yaml:"maxLength,omitempty"`
202+
MaxItems *uint64 `json:"maxItems,omitempty" yaml:"maxItems,omitempty"`
203+
MinLength uint64 `json:"minLength,omitempty" yaml:"minLength,omitempty"`
204+
MinItems uint64 `json:"minItems,omitempty" yaml:"minItems,omitempty"`
205+
Default interface{} `json:"default,omitempty" yaml:"default,omitempty"`
206206
}
207207

208208
func (parameter *Parameter) MarshalJSON() ([]byte, error) {
@@ -215,11 +215,11 @@ func (parameter *Parameter) UnmarshalJSON(data []byte) error {
215215

216216
type Response struct {
217217
openapi3.ExtensionProps
218-
Ref string `json:"$ref,omitempty"`
219-
Description string `json:"description,omitempty"`
220-
Schema *openapi3.SchemaRef `json:"schema,omitempty"`
221-
Headers map[string]*Header `json:"headers,omitempty"`
222-
Examples map[string]interface{} `json:"examples,omitempty"`
218+
Ref string `json:"$ref,omitempty" yaml:"$ref,omitempty"`
219+
Description string `json:"description,omitempty" yaml:"description,omitempty"`
220+
Schema *openapi3.SchemaRef `json:"schema,omitempty" yaml:"schema,omitempty"`
221+
Headers map[string]*Header `json:"headers,omitempty" yaml:"headers,omitempty"`
222+
Examples map[string]interface{} `json:"examples,omitempty" yaml:"examples,omitempty"`
223223
}
224224

225225
func (response *Response) MarshalJSON() ([]byte, error) {
@@ -232,9 +232,9 @@ func (response *Response) UnmarshalJSON(data []byte) error {
232232

233233
type Header struct {
234234
openapi3.ExtensionProps
235-
Ref string `json:"$ref,omitempty"`
236-
Description string `json:"description,omitempty"`
237-
Type string `json:"type,omitempty"`
235+
Ref string `json:"$ref,omitempty" yaml:"$ref,omitempty"`
236+
Description string `json:"description,omitempty" yaml:"description,omitempty"`
237+
Type string `json:"type,omitempty" yaml:"type,omitempty"`
238238
}
239239

240240
func (header *Header) MarshalJSON() ([]byte, error) {
@@ -249,16 +249,16 @@ type SecurityRequirements []map[string][]string
249249

250250
type SecurityScheme struct {
251251
openapi3.ExtensionProps
252-
Ref string `json:"$ref,omitempty"`
253-
Description string `json:"description,omitempty"`
254-
Type string `json:"type,omitempty"`
255-
In string `json:"in,omitempty"`
256-
Name string `json:"name,omitempty"`
257-
Flow string `json:"flow,omitempty"`
258-
AuthorizationURL string `json:"authorizationUrl,omitempty"`
259-
TokenURL string `json:"tokenUrl,omitempty"`
260-
Scopes map[string]string `json:"scopes,omitempty"`
261-
Tags openapi3.Tags `json:"tags,omitempty"`
252+
Ref string `json:"$ref,omitempty" yaml:"$ref,omitempty"`
253+
Description string `json:"description,omitempty" yaml:"description,omitempty"`
254+
Type string `json:"type,omitempty" yaml:"type,omitempty"`
255+
In string `json:"in,omitempty" yaml:"in,omitempty"`
256+
Name string `json:"name,omitempty" yaml:"name,omitempty"`
257+
Flow string `json:"flow,omitempty" yaml:"flow,omitempty"`
258+
AuthorizationURL string `json:"authorizationUrl,omitempty" yaml:"authorizationUrl,omitempty"`
259+
TokenURL string `json:"tokenUrl,omitempty" yaml:"tokenUrl,omitempty"`
260+
Scopes map[string]string `json:"scopes,omitempty" yaml:"scopes,omitempty"`
261+
Tags openapi3.Tags `json:"tags,omitempty" yaml:"tags,omitempty"`
262262
}
263263

264264
func (securityScheme *SecurityScheme) MarshalJSON() ([]byte, error) {

openapi2/openapi2_test.go

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"reflect"
88

99
"github.com/getkin/kin-openapi/openapi2"
10+
"github.com/ghodss/yaml"
1011
)
1112

1213
func Example() {
@@ -19,18 +20,33 @@ func Example() {
1920
if err = json.Unmarshal(input, &doc); err != nil {
2021
panic(err)
2122
}
23+
if doc.ExternalDocs.Description != "Find out more about Swagger" {
24+
panic(`doc.ExternalDocs was parsed incorrectly!`)
25+
}
2226

23-
output, err := json.Marshal(doc)
27+
outputJSON, err := json.Marshal(doc)
2428
if err != nil {
2529
panic(err)
2630
}
31+
var docAgainFromJSON openapi2.T
32+
if err = json.Unmarshal(outputJSON, &docAgainFromJSON); err != nil {
33+
panic(err)
34+
}
35+
if !reflect.DeepEqual(doc, docAgainFromJSON) {
36+
fmt.Println("objects doc & docAgainFromJSON should be the same")
37+
}
2738

28-
var docAgain openapi2.T
29-
if err = json.Unmarshal(output, &docAgain); err != nil {
39+
outputYAML, err := yaml.Marshal(doc)
40+
if err != nil {
41+
panic(err)
42+
}
43+
var docAgainFromYAML openapi2.T
44+
if err = yaml.Unmarshal(outputYAML, &docAgainFromYAML); err != nil {
3045
panic(err)
3146
}
32-
if !reflect.DeepEqual(doc, docAgain) {
33-
fmt.Println("objects doc & docAgain should be the same")
47+
if !reflect.DeepEqual(doc, docAgainFromYAML) {
48+
fmt.Println("objects doc & docAgainFromYAML should be the same")
3449
}
50+
3551
// Output:
3652
}

openapi3/response_issue224_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,7 @@ func TestEmptyResponsesAreInvalid(t *testing.T) {
455455

456456
doc, err := NewLoader().LoadFromData([]byte(spec))
457457
require.NoError(t, err)
458+
require.Equal(t, doc.ExternalDocs.Description, "See AsyncAPI example")
458459
err = doc.Validate(context.Background())
459460
require.EqualError(t, err, `invalid paths: the responses object MUST contain at least one response code`)
460461
}

0 commit comments

Comments
 (0)