Skip to content

Commit

Permalink
openapi3: ensure YAML marshalling matches JSON's (#943)
Browse files Browse the repository at this point in the history
for all types.
  • Loading branch information
damien-talos committed Jun 2, 2024
1 parent 2ab0c0e commit 03281ec
Show file tree
Hide file tree
Showing 28 changed files with 427 additions and 137 deletions.
83 changes: 79 additions & 4 deletions .github/docs/openapi3.txt
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,9 @@ func (callback *Callback) Map() (m map[string]*PathItem)
func (callback *Callback) MarshalJSON() ([]byte, error)
MarshalJSON returns the JSON encoding of Callback.

func (callback *Callback) MarshalYAML() (interface{}, error)
MarshalYAML returns the YAML encoding of Callback.

func (callback *Callback) Set(key string, value *PathItem)
Set adds or replaces key 'key' of 'callback' with 'value'. Note: 'callback'
MUST be non-nil
Expand Down Expand Up @@ -235,6 +238,9 @@ func NewComponents() Components
func (components Components) MarshalJSON() ([]byte, error)
MarshalJSON returns the JSON encoding of Components.

func (components Components) MarshalYAML() (interface{}, error)
MarshalYAML returns the YAML encoding of Components.

func (components *Components) UnmarshalJSON(data []byte) error
UnmarshalJSON sets Components to a copy of data.

Expand All @@ -255,6 +261,9 @@ type Contact struct {
func (contact Contact) MarshalJSON() ([]byte, error)
MarshalJSON returns the JSON encoding of Contact.

func (contact Contact) MarshalYAML() (interface{}, error)
MarshalYAML returns the YAML encoding of Contact.

func (contact *Contact) UnmarshalJSON(data []byte) error
UnmarshalJSON sets Contact to a copy of data.

Expand Down Expand Up @@ -295,6 +304,9 @@ type Discriminator struct {
func (discriminator Discriminator) MarshalJSON() ([]byte, error)
MarshalJSON returns the JSON encoding of Discriminator.

func (discriminator Discriminator) MarshalYAML() (interface{}, error)
MarshalYAML returns the YAML encoding of Discriminator.

func (discriminator *Discriminator) UnmarshalJSON(data []byte) error
UnmarshalJSON sets Discriminator to a copy of data.

Expand All @@ -319,6 +331,9 @@ func NewEncoding() *Encoding
func (encoding Encoding) MarshalJSON() ([]byte, error)
MarshalJSON returns the JSON encoding of Encoding.

func (encoding Encoding) MarshalYAML() (interface{}, error)
MarshalYAML returns the YAML encoding of Encoding.

func (encoding *Encoding) SerializationMethod() *SerializationMethod
SerializationMethod returns a serialization method of request body.
When serialization method is not defined the method returns the default
Expand Down Expand Up @@ -350,6 +365,9 @@ func NewExample(value interface{}) *Example
func (example Example) MarshalJSON() ([]byte, error)
MarshalJSON returns the JSON encoding of Example.

func (example Example) MarshalYAML() (interface{}, error)
MarshalYAML returns the YAML encoding of Example.

func (example *Example) UnmarshalJSON(data []byte) error
UnmarshalJSON sets Example to a copy of data.

Expand Down Expand Up @@ -399,6 +417,9 @@ type ExternalDocs struct {
func (e ExternalDocs) MarshalJSON() ([]byte, error)
MarshalJSON returns the JSON encoding of ExternalDocs.

func (e ExternalDocs) MarshalYAML() (interface{}, error)
MarshalYAML returns the YAML encoding of ExternalDocs.

func (e *ExternalDocs) UnmarshalJSON(data []byte) error
UnmarshalJSON sets ExternalDocs to a copy of data.

Expand Down Expand Up @@ -487,6 +508,9 @@ type Info struct {
func (info Info) MarshalJSON() ([]byte, error)
MarshalJSON returns the JSON encoding of Info.

func (info Info) MarshalYAML() (interface{}, error)
MarshalYAML returns the YAML encoding of Info.

func (info *Info) UnmarshalJSON(data []byte) error
UnmarshalJSON sets Info to a copy of data.

Expand All @@ -505,6 +529,9 @@ type License struct {
func (license License) MarshalJSON() ([]byte, error)
MarshalJSON returns the JSON encoding of License.

func (license License) MarshalYAML() (interface{}, error)
MarshalYAML returns the YAML encoding of License.

func (license *License) UnmarshalJSON(data []byte) error
UnmarshalJSON sets License to a copy of data.

Expand All @@ -527,6 +554,9 @@ type Link struct {
func (link Link) MarshalJSON() ([]byte, error)
MarshalJSON returns the JSON encoding of Link.

func (link Link) MarshalYAML() (interface{}, error)
MarshalYAML returns the YAML encoding of Link.

func (link *Link) UnmarshalJSON(data []byte) error
UnmarshalJSON sets Link to a copy of data.

Expand Down Expand Up @@ -622,6 +652,9 @@ func (mediaType MediaType) JSONLookup(token string) (interface{}, error)
func (mediaType MediaType) MarshalJSON() ([]byte, error)
MarshalJSON returns the JSON encoding of MediaType.

func (mediaType MediaType) MarshalYAML() (interface{}, error)
MarshalYAML returns the YAML encoding of MediaType.

func (mediaType *MediaType) UnmarshalJSON(data []byte) error
UnmarshalJSON sets MediaType to a copy of data.

Expand Down Expand Up @@ -687,6 +720,9 @@ type OAuthFlow struct {
func (flow OAuthFlow) MarshalJSON() ([]byte, error)
MarshalJSON returns the JSON encoding of OAuthFlow.

func (flow OAuthFlow) MarshalYAML() (interface{}, error)
MarshalYAML returns the YAML encoding of OAuthFlow.

func (flow *OAuthFlow) UnmarshalJSON(data []byte) error
UnmarshalJSON sets OAuthFlow to a copy of data.

Expand All @@ -708,6 +744,9 @@ type OAuthFlows struct {
func (flows OAuthFlows) MarshalJSON() ([]byte, error)
MarshalJSON returns the JSON encoding of OAuthFlows.

func (flows OAuthFlows) MarshalYAML() (interface{}, error)
MarshalYAML returns the YAML encoding of OAuthFlows.

func (flows *OAuthFlows) UnmarshalJSON(data []byte) error
UnmarshalJSON sets OAuthFlows to a copy of data.

Expand Down Expand Up @@ -769,6 +808,9 @@ func (operation Operation) JSONLookup(token string) (interface{}, error)
func (operation Operation) MarshalJSON() ([]byte, error)
MarshalJSON returns the JSON encoding of Operation.

func (operation Operation) MarshalYAML() (interface{}, error)
MarshalYAML returns the YAML encoding of Operation.

func (operation *Operation) UnmarshalJSON(data []byte) error
UnmarshalJSON sets Operation to a copy of data.

Expand Down Expand Up @@ -811,6 +853,9 @@ func (parameter Parameter) JSONLookup(token string) (interface{}, error)
func (parameter Parameter) MarshalJSON() ([]byte, error)
MarshalJSON returns the JSON encoding of Parameter.

func (parameter Parameter) MarshalYAML() (interface{}, error)
MarshalYAML returns the YAML encoding of Parameter.

func (parameter *Parameter) SerializationMethod() (*SerializationMethod, error)
SerializationMethod returns a parameter's serialization method. When a
parameter's serialization method is not defined the method returns the
Expand Down Expand Up @@ -901,6 +946,9 @@ func (pathItem *PathItem) GetOperation(method string) *Operation
func (pathItem PathItem) MarshalJSON() ([]byte, error)
MarshalJSON returns the JSON encoding of PathItem.

func (pathItem PathItem) MarshalYAML() (interface{}, error)
MarshalYAML returns the YAML encoding of PathItem.

func (pathItem *PathItem) Operations() map[string]*Operation

func (pathItem *PathItem) SetOperation(method string, operation *Operation)
Expand Down Expand Up @@ -963,8 +1011,8 @@ func (paths *Paths) Map() (m map[string]*PathItem)
func (paths *Paths) MarshalJSON() ([]byte, error)
MarshalJSON returns the JSON encoding of Paths.

func (paths *Paths) MarshalYAML() (any, error)
Support YAML Marshaler interface for gopkg.in/yaml
func (paths *Paths) MarshalYAML() (interface{}, error)
MarshalYAML returns the YAML encoding of Paths.

func (paths *Paths) Set(key string, value *PathItem)
Set adds or replaces key 'key' of 'paths' with 'value'. Note: 'paths' MUST
Expand Down Expand Up @@ -1031,6 +1079,9 @@ func (requestBody *RequestBody) GetMediaType(mediaType string) *MediaType
func (requestBody RequestBody) MarshalJSON() ([]byte, error)
MarshalJSON returns the JSON encoding of RequestBody.

func (requestBody RequestBody) MarshalYAML() (interface{}, error)
MarshalYAML returns the YAML encoding of RequestBody.

func (requestBody *RequestBody) UnmarshalJSON(data []byte) error
UnmarshalJSON sets RequestBody to a copy of data.

Expand Down Expand Up @@ -1097,6 +1148,9 @@ func NewResponse() *Response
func (response Response) MarshalJSON() ([]byte, error)
MarshalJSON returns the JSON encoding of Response.

func (response Response) MarshalYAML() (interface{}, error)
MarshalYAML returns the YAML encoding of Response.

func (response *Response) UnmarshalJSON(data []byte) error
UnmarshalJSON sets Response to a copy of data.

Expand Down Expand Up @@ -1177,8 +1231,8 @@ func (responses *Responses) Map() (m map[string]*ResponseRef)
func (responses *Responses) MarshalJSON() ([]byte, error)
MarshalJSON returns the JSON encoding of Responses.

func (responses *Responses) MarshalYAML() (any, error)
Support YAML Marshaler interface for gopkg.in/yaml
func (responses *Responses) MarshalYAML() (interface{}, error)
MarshalYAML returns the YAML encoding of Responses.

func (responses *Responses) Set(key string, value *ResponseRef)
Set adds or replaces key 'key' of 'responses' with 'value'. Note:
Expand Down Expand Up @@ -1307,6 +1361,9 @@ func (schema Schema) JSONLookup(token string) (interface{}, error)
func (schema Schema) MarshalJSON() ([]byte, error)
MarshalJSON returns the JSON encoding of Schema.

func (schema Schema) MarshalYAML() (interface{}, error)
MarshalYAML returns the YAML encoding of Schema.

func (schema *Schema) NewRef() *SchemaRef

func (schema *Schema) PermitsNull() bool
Expand Down Expand Up @@ -1536,6 +1593,9 @@ func NewSecurityScheme() *SecurityScheme
func (ss SecurityScheme) MarshalJSON() ([]byte, error)
MarshalJSON returns the JSON encoding of SecurityScheme.

func (ss SecurityScheme) MarshalYAML() (interface{}, error)
MarshalYAML returns the YAML encoding of SecurityScheme.

func (ss *SecurityScheme) UnmarshalJSON(data []byte) error
UnmarshalJSON sets SecurityScheme to a copy of data.

Expand Down Expand Up @@ -1611,6 +1671,9 @@ func (server *Server) BasePath() (string, error)
func (server Server) MarshalJSON() ([]byte, error)
MarshalJSON returns the JSON encoding of Server.

func (server Server) MarshalYAML() (interface{}, error)
MarshalYAML returns the YAML encoding of Server.

func (server Server) MatchRawURL(input string) ([]string, string, bool)

func (server Server) ParameterNames() ([]string, error)
Expand All @@ -1634,6 +1697,9 @@ type ServerVariable struct {
func (serverVariable ServerVariable) MarshalJSON() ([]byte, error)
MarshalJSON returns the JSON encoding of ServerVariable.

func (serverVariable ServerVariable) MarshalYAML() (interface{}, error)
MarshalYAML returns the YAML encoding of ServerVariable.

func (serverVariable *ServerVariable) UnmarshalJSON(data []byte) error
UnmarshalJSON sets ServerVariable to a copy of data.

Expand Down Expand Up @@ -1699,6 +1765,9 @@ func (doc *T) JSONLookup(token string) (interface{}, error)
func (doc *T) MarshalJSON() ([]byte, error)
MarshalJSON returns the JSON encoding of T.

func (doc *T) MarshalYAML() (interface{}, error)
MarshalYAML returns the YAML encoding of T.

func (doc *T) UnmarshalJSON(data []byte) error
UnmarshalJSON sets T to a copy of data.

Expand All @@ -1719,6 +1788,9 @@ type Tag struct {
func (t Tag) MarshalJSON() ([]byte, error)
MarshalJSON returns the JSON encoding of Tag.

func (t Tag) MarshalYAML() (interface{}, error)
MarshalYAML returns the YAML encoding of Tag.

func (t *Tag) UnmarshalJSON(data []byte) error
UnmarshalJSON sets Tag to a copy of data.

Expand Down Expand Up @@ -1813,6 +1885,9 @@ type XML struct {
func (xml XML) MarshalJSON() ([]byte, error)
MarshalJSON returns the JSON encoding of XML.

func (xml XML) MarshalYAML() (interface{}, error)
MarshalYAML returns the YAML encoding of XML.

func (xml *XML) UnmarshalJSON(data []byte) error
UnmarshalJSON sets XML to a copy of data.

Expand Down
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
Loading

0 comments on commit 03281ec

Please sign in to comment.