Skip to content

Commit 5a2e949

Browse files
nobbynobbsRoman Bolkhovitin
and
Roman Bolkhovitin
authored
openapi3: implement YAML Marshaller interface for paths and responses (#931)
* implement YAML Marshaler for paths and responses * gen docs --------- Co-authored-by: Roman Bolkhovitin <[email protected]>
1 parent 134eaa0 commit 5a2e949

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed

.github/docs/openapi3.txt

+6
Original file line numberDiff line numberDiff line change
@@ -963,6 +963,9 @@ func (paths *Paths) Map() (m map[string]*PathItem)
963963
func (paths *Paths) MarshalJSON() ([]byte, error)
964964
MarshalJSON returns the JSON encoding of Paths.
965965

966+
func (paths *Paths) MarshalYAML() (any, error)
967+
Support YAML Marshaler interface for gopkg.in/yaml
968+
966969
func (paths *Paths) Set(key string, value *PathItem)
967970
Set adds or replaces key 'key' of 'paths' with 'value'. Note: 'paths' MUST
968971
be non-nil
@@ -1174,6 +1177,9 @@ func (responses *Responses) Map() (m map[string]*ResponseRef)
11741177
func (responses *Responses) MarshalJSON() ([]byte, error)
11751178
MarshalJSON returns the JSON encoding of Responses.
11761179

1180+
func (responses *Responses) MarshalYAML() (any, error)
1181+
Support YAML Marshaler interface for gopkg.in/yaml
1182+
11771183
func (responses *Responses) Set(key string, value *ResponseRef)
11781184
Set adds or replaces key 'key' of 'responses' with 'value'. Note:
11791185
'responses' MUST be non-nil

openapi3/paths.go

+15
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,21 @@ func (paths *Paths) validateUniqueOperationIDs() error {
218218
return nil
219219
}
220220

221+
// Support YAML Marshaler interface for gopkg.in/yaml
222+
func (paths *Paths) MarshalYAML() (any, error) {
223+
res := make(map[string]any, len(paths.Extensions)+len(paths.m))
224+
225+
for k, v := range paths.Extensions {
226+
res[k] = v
227+
}
228+
229+
for k, v := range paths.m {
230+
res[k] = v
231+
}
232+
233+
return res, nil
234+
}
235+
221236
func normalizeTemplatedPath(path string) (string, uint, map[string]struct{}) {
222237
if strings.IndexByte(path, '{') < 0 {
223238
return path, 0, nil

openapi3/response.go

+15
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,21 @@ func (responses *Responses) Validate(ctx context.Context, opts ...ValidationOpti
9898
return validateExtensions(ctx, responses.Extensions)
9999
}
100100

101+
// Support YAML Marshaler interface for gopkg.in/yaml
102+
func (responses *Responses) MarshalYAML() (any, error) {
103+
res := make(map[string]any, len(responses.Extensions)+len(responses.m))
104+
105+
for k, v := range responses.Extensions {
106+
res[k] = v
107+
}
108+
109+
for k, v := range responses.m {
110+
res[k] = v
111+
}
112+
113+
return res, nil
114+
}
115+
101116
// Response is specified by OpenAPI/Swagger 3.0 standard.
102117
// See https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#response-object
103118
type Response struct {

0 commit comments

Comments
 (0)