forked from swaggest/jsonschema-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper_test.go
116 lines (99 loc) · 3.34 KB
/
helper_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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package jsonschema_test
import (
"encoding/json"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/swaggest/jsonschema-go"
)
func TestSchemaOrBool_JSONSchemaBytes(t *testing.T) {
s := jsonschema.Schema{}
s.AddType(jsonschema.String)
b, err := s.ToSchemaOrBool().JSONSchemaBytes()
require.NoError(t, err)
assert.Equal(t, `{"type":"string"}`, string(b))
b, err = s.JSONSchemaBytes()
require.NoError(t, err)
assert.Equal(t, `{"type":"string"}`, string(b))
m, err := s.ToSchemaOrBool().ToSimpleMap()
require.NoError(t, err)
assert.Equal(t, map[string]interface{}{"type": "string"}, m)
sbf := jsonschema.SchemaOrBool{}
sbf.WithTypeBoolean(false)
m, err = sbf.ToSimpleMap()
require.NoError(t, err)
assert.Equal(t, map[string]interface{}{"not": map[string]interface{}{}}, m)
sbt := jsonschema.SchemaOrBool{}
sbt.WithTypeBoolean(true)
m, err = sbt.ToSimpleMap()
require.NoError(t, err)
assert.Equal(t, map[string]interface{}{}, m)
}
func TestSchema_IsTrivial(t *testing.T) {
for _, s := range []struct {
isTrivial bool
name string
schema string
}{
{true, "true schema", "true"},
{false, "false schema", "false"},
{true, "empty schema", "{}"},
{true, "type object", `{"type":"object", "additionalProperties":{"type":"integer"}}`},
{
false, "type object with non-trivial members",
`{"type":"object", "additionalProperties":{"type":"integer","minimum":3}}`,
},
{
true, "type object with properties",
`{"type":"object", "properties":{"foo":{"type":"integer"}}}`,
},
{
false, "type object with non-trivial members",
`{"type":"object", "properties":{"foo":{"type":"integer","minimum":3}}}`,
},
{false, "type fixed array", `{"type":"array", "items":[{"type":"string"}]}`},
{true, "type array", `{"type":"array", "items":{"type":"string"}}`},
{
false, "type array with non-trivial members",
`{"type":"array", "items":{"type":"string", "format":"email"}}`,
},
{true, "type array additionalItems", `{"type":"array", "additionalItems":{"type":"string"}}`},
{
false, "type array additionalItems with non-trivial members",
`{"type":"array", "additionalItems":{"type":"string", "format":"email"}}`,
},
{true, "scalar type", `{"type":"integer"}`},
{true, "scalar nullable type", `{"type":["integer", "null"]}`},
{false, "scalar types", `{"type":["integer", "string"]}`},
{false, "with format", `{"format":"email"}`},
{false, "with not", `{"not":true}`},
{false, "with allOf", `{"allOf":[true]}`},
{false, "with enum", `{"enum":[1,2,3]}`},
{false, "with minItems", `{"minItems":5}`},
{false, "with minProperties", `{"minProperties":5}`},
{false, "with $ref", `{"$ref":"#/definitions/foo","definitions":{"foo":true}}`},
} {
s := s
t.Run(s.name, func(t *testing.T) {
var schema jsonschema.SchemaOrBool
assert.NoError(t, json.Unmarshal([]byte(s.schema), &schema))
assert.Equal(t, s.isTrivial, schema.IsTrivial())
})
}
}
func TestSchema_IsTrivial_reflect(t *testing.T) {
type inner struct {
A uint32 `json:"a"`
}
type outer struct {
I inner `json:"inner"`
}
r := jsonschema.Reflector{}
s, err := r.Reflect(new(outer))
require.NoError(t, err)
assert.True(t, s.IsTrivial(func(ref string) (jsonschema.SchemaOrBool, bool) {
rs, found := s.Definitions[strings.TrimPrefix(ref, "#/definitions/")]
return rs, found
}))
}