generated from sv-tools/go-repo-template
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathbool_or_schema_test.go
87 lines (81 loc) · 1.96 KB
/
bool_or_schema_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package openapi_test
import (
"encoding/json"
"testing"
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v3"
"github.com/sv-tools/openapi"
)
type testAD struct {
AP *openapi.BoolOrSchema `json:"ap,omitempty" yaml:"ap,omitempty"`
Name string `json:"name,omitempty" yaml:"name,omitempty"`
}
func TestAdditionalPropertiesJSON(t *testing.T) {
for _, tt := range []struct {
name string
data string
nilAP bool
allowed bool
nilSchema bool
}{
{
name: "no AP",
data: `{"name": "foo"}`,
nilAP: true,
},
{
name: "false",
data: `{"name": "foo", "ap": false}`,
nilAP: false,
allowed: false,
nilSchema: true,
},
{
name: "true",
data: `{"name": "foo", "ap": true}`,
nilAP: false,
allowed: true,
nilSchema: true,
},
{
name: "schema",
data: `{"name": "foo", "ap": {"title": "bar", "description": "test"}}`,
nilAP: false,
allowed: true,
nilSchema: false,
},
} {
t.Run(tt.name, func(t *testing.T) {
t.Run("json", func(t *testing.T) {
var v testAD
require.NoError(t, json.Unmarshal([]byte(tt.data), &v))
require.Equal(t, "foo", v.Name)
if tt.nilAP {
require.Nil(t, v.AP)
} else {
require.NotNil(t, v.AP)
require.Equal(t, tt.allowed, v.AP.Allowed)
require.Equal(t, tt.nilSchema, v.AP.Schema == nil)
}
newJson, err := json.Marshal(&v)
require.NoError(t, err)
require.JSONEq(t, tt.data, string(newJson))
})
t.Run("yaml", func(t *testing.T) {
var v testAD
require.NoError(t, yaml.Unmarshal([]byte(tt.data), &v))
require.Equal(t, "foo", v.Name)
if tt.nilAP {
require.Nil(t, v.AP)
} else {
require.NotNil(t, v.AP)
require.Equal(t, tt.allowed, v.AP.Allowed)
require.Equal(t, tt.nilSchema, v.AP.Schema == nil)
}
newYaml, err := yaml.Marshal(&v)
require.NoError(t, err)
require.YAMLEq(t, tt.data, string(newYaml))
})
})
}
}