Skip to content

Commit

Permalink
openapi3: implement YAML Marshaller interface for paths and responses (
Browse files Browse the repository at this point in the history
…getkin#931)

* implement YAML Marshaler for paths and responses

* gen docs

---------

Co-authored-by: Roman Bolkhovitin <bolkhovitin_rv@itms.tech>
  • Loading branch information
nobbynobbs and Roman Bolkhovitin authored Apr 6, 2024
1 parent 134eaa0 commit 5a2e949
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .github/docs/openapi3.txt
Original file line number Diff line number Diff line change
Expand Up @@ -963,6 +963,9 @@ 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) Set(key string, value *PathItem)
Set adds or replaces key 'key' of 'paths' with 'value'. Note: 'paths' MUST
be non-nil
Expand Down Expand Up @@ -1174,6 +1177,9 @@ 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) Set(key string, value *ResponseRef)
Set adds or replaces key 'key' of 'responses' with 'value'. Note:
'responses' MUST be non-nil
Expand Down
15 changes: 15 additions & 0 deletions openapi3/paths.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,21 @@ func (paths *Paths) validateUniqueOperationIDs() error {
return nil
}

// Support YAML Marshaler interface for gopkg.in/yaml
func (paths *Paths) MarshalYAML() (any, error) {
res := make(map[string]any, len(paths.Extensions)+len(paths.m))

for k, v := range paths.Extensions {
res[k] = v
}

for k, v := range paths.m {
res[k] = v
}

return res, nil
}

func normalizeTemplatedPath(path string) (string, uint, map[string]struct{}) {
if strings.IndexByte(path, '{') < 0 {
return path, 0, nil
Expand Down
15 changes: 15 additions & 0 deletions openapi3/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,21 @@ func (responses *Responses) Validate(ctx context.Context, opts ...ValidationOpti
return validateExtensions(ctx, responses.Extensions)
}

// Support YAML Marshaler interface for gopkg.in/yaml
func (responses *Responses) MarshalYAML() (any, error) {
res := make(map[string]any, len(responses.Extensions)+len(responses.m))

for k, v := range responses.Extensions {
res[k] = v
}

for k, v := range responses.m {
res[k] = v
}

return res, nil
}

// Response is specified by OpenAPI/Swagger 3.0 standard.
// See https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#response-object
type Response struct {
Expand Down

0 comments on commit 5a2e949

Please sign in to comment.