Skip to content

Commit

Permalink
Ensure that YAML marshalling matches the JSON marshalling
Browse files Browse the repository at this point in the history
for all types.
  • Loading branch information
damien-talos committed Feb 28, 2024
1 parent 672dc32 commit 82e3fb7
Show file tree
Hide file tree
Showing 21 changed files with 253 additions and 27 deletions.
11 changes: 10 additions & 1 deletion openapi3/components.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,15 @@ func NewComponents() Components {

// MarshalJSON returns the JSON encoding of Components.
func (components Components) MarshalJSON() ([]byte, error) {
x, err := components.MarshalYAML()
if err != nil {
return nil, err
}
return json.Marshal(x)
}

// MarshalYAML returns the YAML encoding of Components.
func (components Components) MarshalYAML() (interface{}, error) {
m := make(map[string]interface{}, 9+len(components.Extensions))
for k, v := range components.Extensions {
m[k] = v
Expand Down Expand Up @@ -74,7 +83,7 @@ func (components Components) MarshalJSON() ([]byte, error) {
if x := components.Callbacks; len(x) != 0 {
m["callbacks"] = x
}
return json.Marshal(m)
return m, nil
}

// UnmarshalJSON sets Components to a copy of data.
Expand Down
11 changes: 10 additions & 1 deletion openapi3/contact.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@ type Contact struct {

// MarshalJSON returns the JSON encoding of Contact.
func (contact Contact) MarshalJSON() ([]byte, error) {
x, err := contact.MarshalYAML()
if err != nil {
return nil, err
}
return json.Marshal(x)
}

// MarshalYAML returns the YAML encoding of Contact.
func (contact Contact) MarshalYAML() (interface{}, error) {
m := make(map[string]interface{}, 3+len(contact.Extensions))
for k, v := range contact.Extensions {
m[k] = v
Expand All @@ -30,7 +39,7 @@ func (contact Contact) MarshalJSON() ([]byte, error) {
if x := contact.Email; x != "" {
m["email"] = x
}
return json.Marshal(m)
return m, nil
}

// UnmarshalJSON sets Contact to a copy of data.
Expand Down
11 changes: 10 additions & 1 deletion openapi3/discriminator.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@ type Discriminator struct {

// MarshalJSON returns the JSON encoding of Discriminator.
func (discriminator Discriminator) MarshalJSON() ([]byte, error) {
x, err := discriminator.MarshalYAML()
if err != nil {
return nil, err
}
return json.Marshal(x)
}

// MarshalYAML returns the YAML encoding of Discriminator.
func (discriminator Discriminator) MarshalYAML() (interface{}, error) {
m := make(map[string]interface{}, 2+len(discriminator.Extensions))
for k, v := range discriminator.Extensions {
m[k] = v
Expand All @@ -24,7 +33,7 @@ func (discriminator Discriminator) MarshalJSON() ([]byte, error) {
if x := discriminator.Mapping; len(x) != 0 {
m["mapping"] = x
}
return json.Marshal(m)
return m, nil
}

// UnmarshalJSON sets Discriminator to a copy of data.
Expand Down
11 changes: 10 additions & 1 deletion openapi3/encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,15 @@ func (encoding *Encoding) WithHeaderRef(name string, ref *HeaderRef) *Encoding {

// MarshalJSON returns the JSON encoding of Encoding.
func (encoding Encoding) MarshalJSON() ([]byte, error) {
x, err := encoding.MarshalYAML()
if err != nil {
return nil, err
}
return json.Marshal(x)
}

// MarshalYAML returns the YAML encoding of Encoding.
func (encoding Encoding) MarshalYAML() (interface{}, error) {
m := make(map[string]interface{}, 5+len(encoding.Extensions))
for k, v := range encoding.Extensions {
m[k] = v
Expand All @@ -60,7 +69,7 @@ func (encoding Encoding) MarshalJSON() ([]byte, error) {
if x := encoding.AllowReserved; x {
m["allowReserved"] = x
}
return json.Marshal(m)
return m, nil
}

// UnmarshalJSON sets Encoding to a copy of data.
Expand Down
11 changes: 10 additions & 1 deletion openapi3/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ func NewExample(value interface{}) *Example {

// MarshalJSON returns the JSON encoding of Example.
func (example Example) MarshalJSON() ([]byte, error) {
x, err := example.MarshalYAML()
if err != nil {
return nil, err
}
return json.Marshal(x)
}

// MarshalYAML returns the YAML encoding of Example.
func (example Example) MarshalYAML() (interface{}, error) {
m := make(map[string]interface{}, 4+len(example.Extensions))
for k, v := range example.Extensions {
m[k] = v
Expand All @@ -39,7 +48,7 @@ func (example Example) MarshalJSON() ([]byte, error) {
if x := example.ExternalValue; x != "" {
m["externalValue"] = x
}
return json.Marshal(m)
return m, nil
}

// UnmarshalJSON sets Example to a copy of data.
Expand Down
11 changes: 10 additions & 1 deletion openapi3/external_docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@ type ExternalDocs struct {

// MarshalJSON returns the JSON encoding of ExternalDocs.
func (e ExternalDocs) MarshalJSON() ([]byte, error) {
x, err := e.MarshalYAML()
if err != nil {
return nil, err
}
return json.Marshal(x)
}

// MarshalYAML returns the YAML encoding of ExternalDocs.
func (e ExternalDocs) MarshalYAML() (interface{}, error) {
m := make(map[string]interface{}, 2+len(e.Extensions))
for k, v := range e.Extensions {
m[k] = v
Expand All @@ -29,7 +38,7 @@ func (e ExternalDocs) MarshalJSON() ([]byte, error) {
if x := e.URL; x != "" {
m["url"] = x
}
return json.Marshal(m)
return m, nil
}

// UnmarshalJSON sets ExternalDocs to a copy of data.
Expand Down
11 changes: 10 additions & 1 deletion openapi3/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ type Info struct {

// MarshalJSON returns the JSON encoding of Info.
func (info Info) MarshalJSON() ([]byte, error) {
x, err := info.MarshalYAML()
if err != nil {
return nil, err
}
return json.Marshal(x)
}

// MarshalYAML returns the YAML encoding of Info.
func (info Info) MarshalYAML() (interface{}, error) {
m := make(map[string]interface{}, 6+len(info.Extensions))
for k, v := range info.Extensions {
m[k] = v
Expand All @@ -39,7 +48,7 @@ func (info Info) MarshalJSON() ([]byte, error) {
m["license"] = x
}
m["version"] = info.Version
return json.Marshal(m)
return m, nil
}

// UnmarshalJSON sets Info to a copy of data.
Expand Down
11 changes: 10 additions & 1 deletion openapi3/license.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@ type License struct {

// MarshalJSON returns the JSON encoding of License.
func (license License) MarshalJSON() ([]byte, error) {
x, err := license.MarshalYAML()
if err != nil {
return nil, err
}
return json.Marshal(x)
}

// MarshalYAML returns the YAML encoding of License.
func (license License) MarshalYAML() (interface{}, error) {
m := make(map[string]interface{}, 2+len(license.Extensions))
for k, v := range license.Extensions {
m[k] = v
Expand All @@ -25,7 +34,7 @@ func (license License) MarshalJSON() ([]byte, error) {
if x := license.URL; x != "" {
m["url"] = x
}
return json.Marshal(m)
return m, nil
}

// UnmarshalJSON sets License to a copy of data.
Expand Down
11 changes: 10 additions & 1 deletion openapi3/link.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ type Link struct {

// MarshalJSON returns the JSON encoding of Link.
func (link Link) MarshalJSON() ([]byte, error) {
x, err := link.MarshalYAML()
if err != nil {
return nil, err
}
return json.Marshal(x)
}

// MarshalYAML returns the YAML encoding of Link.
func (link Link) MarshalYAML() (interface{}, error) {
m := make(map[string]interface{}, 6+len(link.Extensions))
for k, v := range link.Extensions {
m[k] = v
Expand All @@ -46,7 +55,7 @@ func (link Link) MarshalJSON() ([]byte, error) {
m["requestBody"] = x
}

return json.Marshal(m)
return m, nil
}

// UnmarshalJSON sets Link to a copy of data.
Expand Down
11 changes: 10 additions & 1 deletion openapi3/media_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,15 @@ func (mediaType *MediaType) WithEncoding(name string, enc *Encoding) *MediaType

// MarshalJSON returns the JSON encoding of MediaType.
func (mediaType MediaType) MarshalJSON() ([]byte, error) {
x, err := mediaType.MarshalYAML()
if err != nil {
return nil, err
}
return json.Marshal(x)
}

// MarshalYAML returns the YAML encoding of MediaType.
func (mediaType MediaType) MarshalYAML() (interface{}, error) {
m := make(map[string]interface{}, 4+len(mediaType.Extensions))
for k, v := range mediaType.Extensions {
m[k] = v
Expand All @@ -81,7 +90,7 @@ func (mediaType MediaType) MarshalJSON() ([]byte, error) {
if x := mediaType.Encoding; len(x) != 0 {
m["encoding"] = x
}
return json.Marshal(m)
return m, nil
}

// UnmarshalJSON sets MediaType to a copy of data.
Expand Down
11 changes: 10 additions & 1 deletion openapi3/openapi3.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,15 @@ func (doc *T) JSONLookup(token string) (interface{}, error) {

// MarshalJSON returns the JSON encoding of T.
func (doc T) MarshalJSON() ([]byte, error) {
x, err := doc.MarshalYAML()
if err != nil {
return nil, err
}
return json.Marshal(x)
}

// MarshalYAML returns the YAML encoding of T.
func (doc T) MarshalYAML() (interface{}, error) {
m := make(map[string]interface{}, 4+len(doc.Extensions))
for k, v := range doc.Extensions {
m[k] = v
Expand All @@ -77,7 +86,7 @@ func (doc T) MarshalJSON() ([]byte, error) {
if x := doc.ExternalDocs; x != nil {
m["externalDocs"] = x
}
return json.Marshal(m)
return m, nil
}

// UnmarshalJSON sets T to a copy of data.
Expand Down
11 changes: 10 additions & 1 deletion openapi3/operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,15 @@ func NewOperation() *Operation {

// MarshalJSON returns the JSON encoding of Operation.
func (operation Operation) MarshalJSON() ([]byte, error) {
x, err := operation.MarshalYAML()
if err != nil {
return nil, err
}
return json.Marshal(x)
}

// MarshalYAML returns the YAML encoding of Operation.
func (operation Operation) MarshalYAML() (interface{}, error) {
m := make(map[string]interface{}, 12+len(operation.Extensions))
for k, v := range operation.Extensions {
m[k] = v
Expand Down Expand Up @@ -96,7 +105,7 @@ func (operation Operation) MarshalJSON() ([]byte, error) {
if x := operation.ExternalDocs; x != nil {
m["externalDocs"] = x
}
return json.Marshal(m)
return m, nil
}

// UnmarshalJSON sets Operation to a copy of data.
Expand Down
11 changes: 10 additions & 1 deletion openapi3/parameter.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,15 @@ func (parameter *Parameter) WithSchema(value *Schema) *Parameter {

// MarshalJSON returns the JSON encoding of Parameter.
func (parameter Parameter) MarshalJSON() ([]byte, error) {
x, err := parameter.MarshalYAML()
if err != nil {
return nil, err
}
return json.Marshal(x)
}

// MarshalYAML returns the YAML encoding of Parameter.
func (parameter Parameter) MarshalYAML() (interface{}, error) {
m := make(map[string]interface{}, 13+len(parameter.Extensions))
for k, v := range parameter.Extensions {
m[k] = v
Expand Down Expand Up @@ -195,7 +204,7 @@ func (parameter Parameter) MarshalJSON() ([]byte, error) {
m["content"] = x
}

return json.Marshal(m)
return m, nil
}

// UnmarshalJSON sets Parameter to a copy of data.
Expand Down
11 changes: 10 additions & 1 deletion openapi3/path_item.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@ type PathItem struct {

// MarshalJSON returns the JSON encoding of PathItem.
func (pathItem PathItem) MarshalJSON() ([]byte, error) {
x, err := pathItem.MarshalYAML()
if err != nil {
return nil, err
}
return json.Marshal(x)
}

// MarshalYAML returns the YAML encoding of PathItem.
func (pathItem PathItem) MarshalYAML() (interface{}, error) {
if ref := pathItem.Ref; ref != "" {
return json.Marshal(Ref{Ref: ref})
}
Expand Down Expand Up @@ -78,7 +87,7 @@ func (pathItem PathItem) MarshalJSON() ([]byte, error) {
if x := pathItem.Parameters; len(x) != 0 {
m["parameters"] = x
}
return json.Marshal(m)
return m, nil
}

// UnmarshalJSON sets PathItem to a copy of data.
Expand Down
11 changes: 10 additions & 1 deletion openapi3/request_body.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,15 @@ func (requestBody *RequestBody) GetMediaType(mediaType string) *MediaType {

// MarshalJSON returns the JSON encoding of RequestBody.
func (requestBody RequestBody) MarshalJSON() ([]byte, error) {
x, err := requestBody.MarshalYAML()
if err != nil {
return nil, err
}
return json.Marshal(x)
}

// MarshalYAML returns the YAML encoding of RequestBody.
func (requestBody RequestBody) MarshalYAML() (interface{}, error) {
m := make(map[string]interface{}, 3+len(requestBody.Extensions))
for k, v := range requestBody.Extensions {
m[k] = v
Expand All @@ -88,7 +97,7 @@ func (requestBody RequestBody) MarshalJSON() ([]byte, error) {
if x := requestBody.Content; true {
m["content"] = x
}
return json.Marshal(m)
return m, nil
}

// UnmarshalJSON sets RequestBody to a copy of data.
Expand Down
11 changes: 10 additions & 1 deletion openapi3/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,15 @@ func (response *Response) WithJSONSchemaRef(schema *SchemaRef) *Response {

// MarshalJSON returns the JSON encoding of Response.
func (response Response) MarshalJSON() ([]byte, error) {
x, err := response.MarshalYAML()
if err != nil {
return nil, err
}
return json.Marshal(x)
}

// MarshalYAML returns the YAML encoding of Response.
func (response Response) MarshalYAML() (interface{}, error) {
m := make(map[string]interface{}, 4+len(response.Extensions))
for k, v := range response.Extensions {
m[k] = v
Expand All @@ -151,7 +160,7 @@ func (response Response) MarshalJSON() ([]byte, error) {
if x := response.Links; len(x) != 0 {
m["links"] = x
}
return json.Marshal(m)
return m, nil
}

// UnmarshalJSON sets Response to a copy of data.
Expand Down
Loading

0 comments on commit 82e3fb7

Please sign in to comment.