forked from sv-tools/openapi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathschema_test.go
96 lines (91 loc) · 1.98 KB
/
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
88
89
90
91
92
93
94
95
96
package openapi_test
import (
"encoding/json"
"testing"
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v3"
"github.com/sv-tools/openapi"
)
func TestSchema_Marshal_Unmarshal(t *testing.T) {
for _, tt := range []struct {
name string
data string
expected string
emptyExtensions bool
}{
{
name: "spec only",
data: `{"title": "foo"}`,
emptyExtensions: true,
},
{
name: "spec with extension field",
data: `{"title": "foo", "b": "bar"}`,
emptyExtensions: false,
},
} {
t.Run(tt.name, func(t *testing.T) {
t.Run("json", func(t *testing.T) {
var v *openapi.Schema
require.NoError(t, json.Unmarshal([]byte(tt.data), &v))
if tt.emptyExtensions {
require.Empty(t, v.Extensions)
} else {
require.NotEmpty(t, v.Extensions)
}
data, err := json.Marshal(&v)
require.NoError(t, err)
if tt.expected == "" {
tt.expected = tt.data
}
require.JSONEq(t, tt.expected, string(data))
})
t.Run("yaml", func(t *testing.T) {
var v *openapi.Schema
require.NoError(t, yaml.Unmarshal([]byte(tt.data), &v))
if tt.emptyExtensions {
require.Empty(t, v.Extensions)
} else {
require.NotEmpty(t, v.Extensions)
}
data, err := yaml.Marshal(&v)
require.NoError(t, err)
if tt.expected == "" {
tt.expected = tt.data
}
require.YAMLEq(t, tt.expected, string(data))
})
})
}
}
func TestSchema_AddExt(t *testing.T) {
for _, tt := range []struct {
name string
key string
value any
expected map[string]any
}{
{
name: "without prefix",
key: "foo",
value: 42,
expected: map[string]any{
"foo": 42,
},
},
{
name: "with prefix",
key: "x-foo",
value: 43,
expected: map[string]any{
"x-foo": 43,
},
},
} {
t.Run(tt.name, func(t *testing.T) {
ext := openapi.Schema{}
ext.AddExt(tt.key, tt.value)
require.Equal(t, tt.expected, ext.Extensions)
})
}
}