-
-
Notifications
You must be signed in to change notification settings - Fork 128
Expand file tree
/
Copy pathschema_customizer_test.go
More file actions
167 lines (152 loc) · 5.12 KB
/
Copy pathschema_customizer_test.go
File metadata and controls
167 lines (152 loc) · 5.12 KB
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package fuego
import (
"reflect"
"testing"
"github.com/getkin/kin-openapi/openapi3"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestDetermineFieldConstraints(t *testing.T) {
t.Run("non-struct type is a no-op", func(t *testing.T) {
schema := &openapi3.Schema{}
determineFieldConstraints(reflect.TypeFor[string](), schema)
assert.Empty(t, schema.Required)
})
t.Run("private field is skipped", func(t *testing.T) {
type S struct {
private string //nolint:unused
}
schema := &openapi3.Schema{
Properties: openapi3.Schemas{},
}
determineFieldConstraints(reflect.TypeFor[S](), schema)
assert.Empty(t, schema.Required)
})
t.Run("json tag - is skipped", func(t *testing.T) {
type S struct {
Hidden string `json:"-"`
}
schema := &openapi3.Schema{
Properties: openapi3.Schemas{},
}
determineFieldConstraints(reflect.TypeFor[S](), schema)
assert.Empty(t, schema.Required)
})
t.Run("field without omitempty is required", func(t *testing.T) {
type S struct {
Name string `json:"name"`
}
schema := &openapi3.Schema{
Properties: openapi3.Schemas{
"name": &openapi3.SchemaRef{Value: &openapi3.Schema{}},
},
}
determineFieldConstraints(reflect.TypeFor[S](), schema)
assert.Contains(t, schema.Required, "name")
})
t.Run("field with omitempty is not required", func(t *testing.T) {
type S struct {
Name string `json:"name,omitempty"`
}
schema := &openapi3.Schema{
Properties: openapi3.Schemas{
"name": &openapi3.SchemaRef{Value: &openapi3.Schema{}},
},
}
determineFieldConstraints(reflect.TypeFor[S](), schema)
assert.NotContains(t, schema.Required, "name")
})
t.Run("field with validate:\"required\" is required", func(t *testing.T) {
type S struct {
Name string `json:"name,omitempty" validate:"required"`
}
schema := &openapi3.Schema{
Properties: openapi3.Schemas{
"name": &openapi3.SchemaRef{Value: &openapi3.Schema{}},
},
}
determineFieldConstraints(reflect.TypeFor[S](), schema)
assert.Contains(t, schema.Required, "name")
assert.False(t, schema.Properties["name"].Value.Type.Includes("null"))
})
t.Run("slice field is nullable", func(t *testing.T) {
type S struct {
Items []string `json:"items"`
}
schema := &openapi3.Schema{
Properties: openapi3.Schemas{
"items": &openapi3.SchemaRef{Value: &openapi3.Schema{}},
},
}
determineFieldConstraints(reflect.TypeFor[S](), schema)
assert.True(t, schema.Properties["items"].Value.Type.Includes("null"))
})
t.Run("map field is nullable", func(t *testing.T) {
type S struct {
Meta map[string]string `json:"meta"`
}
schema := &openapi3.Schema{
Properties: openapi3.Schemas{
"meta": &openapi3.SchemaRef{Value: &openapi3.Schema{}},
},
}
determineFieldConstraints(reflect.TypeFor[S](), schema)
assert.True(t, schema.Properties["meta"].Value.Type.Includes("null"))
})
t.Run("string field is not nullable", func(t *testing.T) {
type S struct {
Name string `json:"name"`
}
schema := &openapi3.Schema{
Properties: openapi3.Schemas{
"name": &openapi3.SchemaRef{Value: &openapi3.Schema{}},
},
}
determineFieldConstraints(reflect.TypeFor[S](), schema)
assert.False(t, schema.Properties["name"].Value.Type.Includes("null"))
})
t.Run("required fields are sorted", func(t *testing.T) {
type S struct {
Zebra string `json:"zebra"`
Apple string `json:"apple"`
Mango string `json:"mango"`
}
schema := &openapi3.Schema{
Properties: openapi3.Schemas{
"zebra": &openapi3.SchemaRef{Value: &openapi3.Schema{}},
"apple": &openapi3.SchemaRef{Value: &openapi3.Schema{}},
"mango": &openapi3.SchemaRef{Value: &openapi3.Schema{}},
},
}
determineFieldConstraints(reflect.TypeFor[S](), schema)
require.Len(t, schema.Required, 3)
assert.Equal(t, []string{"apple", "mango", "zebra"}, schema.Required)
})
}
func TestParseValidateOneof(t *testing.T) {
t.Run("string oneof adds enum values", func(t *testing.T) {
schema := &openapi3.Schema{Type: &openapi3.Types{openapi3.TypeString}}
parseValidate(reflect.StructTag(`validate:"oneof=a b c"`), schema)
assert.Equal(t, []any{"a", "b", "c"}, schema.Enum)
})
t.Run("integer oneof adds typed enum values", func(t *testing.T) {
schema := &openapi3.Schema{Type: &openapi3.Types{openapi3.TypeInteger}}
parseValidate(reflect.StructTag(`validate:"oneof=1 2 3"`), schema)
assert.Equal(t, []any{1, 2, 3}, schema.Enum)
})
t.Run("number oneof adds typed enum values", func(t *testing.T) {
schema := &openapi3.Schema{Type: &openapi3.Types{openapi3.TypeNumber}}
parseValidate(reflect.StructTag(`validate:"oneof=1.5 2.5"`), schema)
assert.Equal(t, []any{1.5, 2.5}, schema.Enum)
})
t.Run("oneof works alongside required", func(t *testing.T) {
schema := &openapi3.Schema{Type: &openapi3.Types{openapi3.TypeString}}
parseValidate(reflect.StructTag(`validate:"required,oneof=foo bar"`), schema)
assert.Equal(t, []any{"foo", "bar"}, schema.Enum)
})
t.Run("no oneof leaves enum nil", func(t *testing.T) {
schema := &openapi3.Schema{Type: &openapi3.Types{openapi3.TypeString}}
parseValidate(reflect.StructTag(`validate:"required"`), schema)
assert.Nil(t, schema.Enum)
})
}