Skip to content

Commit 7f46bdf

Browse files
authored
openapi3: Implement YAML Marshaler interface for AdditionalProperties (#922)
1 parent 05453ef commit 7f46bdf

File tree

4 files changed

+74
-0
lines changed

4 files changed

+74
-0
lines changed

.github/docs/openapi3.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,9 @@ type AdditionalProperties struct {
134134
func (addProps AdditionalProperties) MarshalJSON() ([]byte, error)
135135
MarshalJSON returns the JSON encoding of AdditionalProperties.
136136

137+
func (addProps AdditionalProperties) MarshalYAML() (interface{}, error)
138+
MarshalYAML returns the YAML encoding of AdditionalProperties.
139+
137140
func (addProps *AdditionalProperties) UnmarshalJSON(data []byte) error
138141
UnmarshalJSON sets AdditionalProperties to a copy of data.
139142

openapi3/additionalProperties_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package openapi3_test
2+
3+
import (
4+
"bytes"
5+
"context"
6+
"os"
7+
"testing"
8+
9+
"github.com/stretchr/testify/require"
10+
"gopkg.in/yaml.v3"
11+
12+
"github.com/getkin/kin-openapi/openapi3"
13+
)
14+
15+
func TestMarshalAdditionalProperties(t *testing.T) {
16+
ctx := context.Background()
17+
data, err := os.ReadFile("testdata/test.openapi.additionalproperties.yml")
18+
require.NoError(t, err)
19+
20+
loader := openapi3.NewLoader()
21+
loader.IsExternalRefsAllowed = true
22+
spec, err := loader.LoadFromData(data)
23+
require.NoError(t, err)
24+
25+
err = spec.Validate(ctx)
26+
require.NoError(t, err)
27+
28+
var buf bytes.Buffer
29+
enc := yaml.NewEncoder(&buf)
30+
enc.SetIndent(2)
31+
err = enc.Encode(spec)
32+
require.NoError(t, err)
33+
34+
// Load the doc from the serialized yaml.
35+
spec2, err := loader.LoadFromData(buf.Bytes())
36+
require.NoError(t, err)
37+
38+
err = spec2.Validate(ctx)
39+
require.NoError(t, err)
40+
}

openapi3/schema.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,20 @@ type AdditionalProperties struct {
213213
Schema *SchemaRef
214214
}
215215

216+
// MarshalYAML returns the YAML encoding of AdditionalProperties.
217+
func (addProps AdditionalProperties) MarshalYAML() (interface{}, error) {
218+
if x := addProps.Has; x != nil {
219+
if *x {
220+
return true, nil
221+
}
222+
return false, nil
223+
}
224+
if x := addProps.Schema; x != nil {
225+
return x.Value, nil
226+
}
227+
return nil, nil
228+
}
229+
216230
// MarshalJSON returns the JSON encoding of AdditionalProperties.
217231
func (addProps AdditionalProperties) MarshalJSON() ([]byte, error) {
218232
if x := addProps.Has; x != nil {
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
openapi: 3.0.0
2+
info:
3+
title: "OAI Specification in YAML"
4+
version: 0.0.1
5+
paths: {}
6+
components:
7+
schemas:
8+
TestSchema:
9+
type: object
10+
additionalProperties:
11+
type: array
12+
items:
13+
type: string
14+
TestSchema2:
15+
type: object
16+
additionalProperties:
17+
type: string

0 commit comments

Comments
 (0)