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 May 13, 2024
1 parent 2ab0c0e commit 034224a
Show file tree
Hide file tree
Showing 27 changed files with 348 additions and 133 deletions.
15 changes: 12 additions & 3 deletions maps.sh
Original file line number Diff line number Diff line change
Expand Up @@ -156,16 +156,25 @@ EOF

maplike_UnMarsh() {
cat <<EOF >>"$maplike"
// MarshalJSON returns the JSON encoding of ${type#'*'}.
func (${name} ${type}) MarshalJSON() ([]byte, error) {
// MarshalYAML returns the YAML encoding of ${type#'*'}.
func (${name} ${type}) MarshalYAML() (interface{}, error) {
m := make(map[string]interface{}, ${name}.Len()+len(${name}.Extensions))
for k, v := range ${name}.Extensions {
m[k] = v
}
for k, v := range ${name}.Map() {
m[k] = v
}
return json.Marshal(m)
return m, nil
}
// MarshalJSON returns the JSON encoding of ${type#'*'}.
func (${name} ${type}) MarshalJSON() ([]byte, error) {
${name}Yaml, err := ${name}.MarshalYAML()
if err != nil {
return nil, err
}
return json.Marshal(${name}Yaml)
}
// UnmarshalJSON sets ${type#'*'} to a copy of data.
Expand Down
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
45 changes: 36 additions & 9 deletions openapi3/maplike.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,25 @@ func (responses Responses) JSONLookup(token string) (interface{}, error) {
}
}

// MarshalJSON returns the JSON encoding of Responses.
func (responses *Responses) MarshalJSON() ([]byte, error) {
// MarshalYAML returns the YAML encoding of Responses.
func (responses *Responses) MarshalYAML() (interface{}, error) {
m := make(map[string]interface{}, responses.Len()+len(responses.Extensions))
for k, v := range responses.Extensions {
m[k] = v
}
for k, v := range responses.Map() {
m[k] = v
}
return json.Marshal(m)
return m, nil
}

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

// UnmarshalJSON sets Responses to a copy of data.
Expand Down Expand Up @@ -195,16 +204,25 @@ func (callback Callback) JSONLookup(token string) (interface{}, error) {
}
}

// MarshalJSON returns the JSON encoding of Callback.
func (callback *Callback) MarshalJSON() ([]byte, error) {
// MarshalYAML returns the YAML encoding of Callback.
func (callback *Callback) MarshalYAML() (interface{}, error) {
m := make(map[string]interface{}, callback.Len()+len(callback.Extensions))
for k, v := range callback.Extensions {
m[k] = v
}
for k, v := range callback.Map() {
m[k] = v
}
return json.Marshal(m)
return m, nil
}

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

// UnmarshalJSON sets Callback to a copy of data.
Expand Down Expand Up @@ -314,16 +332,25 @@ func (paths Paths) JSONLookup(token string) (interface{}, error) {
}
}

// MarshalJSON returns the JSON encoding of Paths.
func (paths *Paths) MarshalJSON() ([]byte, error) {
// MarshalYAML returns the YAML encoding of Paths.
func (paths *Paths) MarshalYAML() (interface{}, error) {
m := make(map[string]interface{}, paths.Len()+len(paths.Extensions))
for k, v := range paths.Extensions {
m[k] = v
}
for k, v := range paths.Map() {
m[k] = v
}
return json.Marshal(m)
return m, nil
}

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

// UnmarshalJSON sets Paths 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
Loading

0 comments on commit 034224a

Please sign in to comment.