File tree 4 files changed +74
-0
lines changed 4 files changed +74
-0
lines changed Original file line number Diff line number Diff line change @@ -134,6 +134,9 @@ type AdditionalProperties struct {
134
134
func (addProps AdditionalProperties) MarshalJSON() ([]byte, error)
135
135
MarshalJSON returns the JSON encoding of AdditionalProperties.
136
136
137
+ func (addProps AdditionalProperties) MarshalYAML() (interface{}, error)
138
+ MarshalYAML returns the YAML encoding of AdditionalProperties.
139
+
137
140
func (addProps *AdditionalProperties) UnmarshalJSON(data []byte) error
138
141
UnmarshalJSON sets AdditionalProperties to a copy of data.
139
142
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change @@ -213,6 +213,20 @@ type AdditionalProperties struct {
213
213
Schema * SchemaRef
214
214
}
215
215
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
+
216
230
// MarshalJSON returns the JSON encoding of AdditionalProperties.
217
231
func (addProps AdditionalProperties ) MarshalJSON () ([]byte , error ) {
218
232
if x := addProps .Has ; x != nil {
Original file line number Diff line number Diff line change
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
You can’t perform that action at this time.
0 commit comments