Debian Codegraph @

schema_validation/validate_schema_coercion_test.go

raw
1 // Copyright 2025 Princess B33f Heavy Industries / Dave Shanley
2 // SPDX-License-Identifier: MIT
3
4 package schema_validation
5
6 import (
7 "encoding/json"
8 "testing"
9
10 "github.com/pb33f/libopenapi"
11 "github.com/stretchr/testify/assert"
12
13 "github.com/pb33f/libopenapi-validator/config"
14 )
15
16 func TestSchemaValidator_ScalarCoercion_Boolean(t *testing.T) {
17 spec := `openapi: 3.0.0
18 paths:
19 /test:
20 post:
21 requestBody:
22 content:
23 application/json:
24 schema:
25 type: object
26 properties:
27 active:
28 type: boolean
29 count:
30 type: integer`
31
32 doc, _ := libopenapi.NewDocument([]byte(spec))
33 m, errs := doc.BuildV3Model()
34 assert.Empty(t, errs)
35
36 schema := m.Model.Paths.PathItems.GetOrZero("/test").Post.RequestBody.Content.GetOrZero("application/json").Schema
37
38 // Test data with string values that should coerce
39 body := map[string]interface{}{
40 "active": "true", // String that should coerce to boolean
41 "count": "42", // String that should coerce to integer
42 }
43 bodyBytes, _ := json.Marshal(body)
44
45 // Test with coercion enabled
46 vWithCoercion := NewSchemaValidator(config.WithScalarCoercion())
47 valid, errors := vWithCoercion.ValidateSchemaStringWithVersion(schema.Schema(), string(bodyBytes), 3.0)
48 assert.True(t, valid, "Validation should pass with scalar coercion enabled")
49 assert.Empty(t, errors, "Should have no validation errors")
50
51 // Test with coercion disabled (default)
52 vWithoutCoercion := NewSchemaValidator()
53 valid, errors = vWithoutCoercion.ValidateSchemaStringWithVersion(schema.Schema(), string(bodyBytes), 3.0)
54 assert.False(t, valid, "Validation should fail with scalar coercion disabled")
55 assert.NotEmpty(t, errors, "Should have validation errors")
56 }
57
58 func TestSchemaValidator_ScalarCoercion_InvalidStrings(t *testing.T) {
59 spec := `openapi: 3.0.0
60 paths:
61 /test:
62 post:
63 requestBody:
64 content:
65 application/json:
66 schema:
67 type: object
68 properties:
69 active:
70 type: boolean
71 count:
72 type: number`
73
74 doc, _ := libopenapi.NewDocument([]byte(spec))
75 m, errs := doc.BuildV3Model()
76 assert.Empty(t, errs)
77
78 schema := m.Model.Paths.PathItems.GetOrZero("/test").Post.RequestBody.Content.GetOrZero("application/json").Schema
79
80 // Test data with invalid coercion strings
81 body := map[string]interface{}{
82 "active": "yes", // Invalid boolean string
83 "count": "abc", // Invalid number string
84 }
85 bodyBytes, _ := json.Marshal(body)
86
87 // Even with coercion enabled, invalid strings should fail
88 vWithCoercion := NewSchemaValidator(config.WithScalarCoercion())
89 valid, errors := vWithCoercion.ValidateSchemaStringWithVersion(schema.Schema(), string(bodyBytes), 3.0)
90 assert.False(t, valid, "Validation should fail with invalid coercion strings")
91 assert.NotEmpty(t, errors, "Should have validation errors for invalid strings")
92 }
93
94 func TestSchemaValidator_ScalarCoercion_MixedTypes(t *testing.T) {
95 spec := `openapi: 3.0.0
96 paths:
97 /test:
98 post:
99 requestBody:
100 content:
101 application/json:
102 schema:
103 type: object
104 properties:
105 flag:
106 type: boolean
107 score:
108 type: number
109 rank:
110 type: integer
111 name:
112 type: string`
113
114 doc, _ := libopenapi.NewDocument([]byte(spec))
115 m, errs := doc.BuildV3Model()
116 assert.Empty(t, errs)
117
118 schema := m.Model.Paths.PathItems.GetOrZero("/test").Post.RequestBody.Content.GetOrZero("application/json").Schema
119
120 // Test mixed coercion and normal values
121 body := map[string]interface{}{
122 "flag": "false", // Boolean coercion
123 "score": "95.5", // Number coercion
124 "rank": "1", // Integer coercion
125 "name": "Alice", // Normal string (no coercion)
126 }
127 bodyBytes, _ := json.Marshal(body)
128
129 vWithCoercion := NewSchemaValidator(config.WithScalarCoercion())
130 valid, errors := vWithCoercion.ValidateSchemaStringWithVersion(schema.Schema(), string(bodyBytes), 3.0)
131 assert.True(t, valid, "Validation should pass with mixed coercion")
132 assert.Empty(t, errors, "Should have no validation errors")
133 }
134
135 func TestSchemaValidator_ScalarCoercion_WithNullable(t *testing.T) {
136 spec := `openapi: 3.0.0
137 paths:
138 /test:
139 post:
140 requestBody:
141 content:
142 application/json:
143 schema:
144 type: object
145 properties:
146 active:
147 type: boolean
148 nullable: true`
149
150 doc, _ := libopenapi.NewDocument([]byte(spec))
151 m, errs := doc.BuildV3Model()
152 assert.Empty(t, errs)
153
154 schema := m.Model.Paths.PathItems.GetOrZero("/test").Post.RequestBody.Content.GetOrZero("application/json").Schema
155
156 vWithCoercion := NewSchemaValidator(config.WithScalarCoercion())
157
158 // Test coercion with nullable
159 testCases := []struct {
160 name string
161 value interface{}
162 expected bool
163 }{
164 {"String true", "true", true},
165 {"String false", "false", true},
166 {"Boolean true", true, true},
167 {"Boolean false", false, true},
168 {"Null value", nil, true},
169 {"Invalid string", "yes", false},
170 }
171
172 for _, tc := range testCases {
173 t.Run(tc.name, func(t *testing.T) {
174 body := map[string]interface{}{
175 "active": tc.value,
176 }
177 bodyBytes, _ := json.Marshal(body)
178
179 valid, errors := vWithCoercion.ValidateSchemaStringWithVersion(schema.Schema(), string(bodyBytes), 3.0)
180 if tc.expected {
181 assert.True(t, valid, "Should pass: %s", tc.name)
182 assert.Empty(t, errors, "Should have no errors: %s", tc.name)
183 } else {
184 assert.False(t, valid, "Should fail: %s", tc.name)
185 assert.NotEmpty(t, errors, "Should have errors: %s", tc.name)
186 }
187 })
188 }
189 }
190
191 func TestSchemaValidator_ScalarCoercion_EdgeCases(t *testing.T) {
192 spec := `openapi: 3.0.0
193 paths:
194 /test:
195 post:
196 requestBody:
197 content:
198 application/json:
199 schema:
200 type: object
201 properties:
202 scientific:
203 type: number
204 leadingZero:
205 type: integer
206 negative:
207 type: number`
208
209 doc, _ := libopenapi.NewDocument([]byte(spec))
210 m, errs := doc.BuildV3Model()
211 assert.Empty(t, errs)
212
213 schema := m.Model.Paths.PathItems.GetOrZero("/test").Post.RequestBody.Content.GetOrZero("application/json").Schema
214
215 vWithCoercion := NewSchemaValidator(config.WithScalarCoercion())
216
217 // Test edge cases
218 testCases := []struct {
219 body map[string]interface{}
220 expected bool
221 desc string
222 }{
223 {map[string]interface{}{"scientific": "1.23e-10"}, true, "Scientific notation"},
224 {map[string]interface{}{"leadingZero": "007"}, false, "Leading zeros not allowed for integers"},
225 {map[string]interface{}{"negative": "-0"}, true, "Negative zero"},
226 {map[string]interface{}{"scientific": "1.23E+5"}, true, "Uppercase E notation"},
227 }
228
229 for _, tc := range testCases {
230 t.Run(tc.desc, func(t *testing.T) {
231 bodyBytes, _ := json.Marshal(tc.body)
232
233 valid, errors := vWithCoercion.ValidateSchemaStringWithVersion(schema.Schema(), string(bodyBytes), 3.0)
234 if tc.expected {
235 assert.True(t, valid, "Should pass: %s", tc.desc)
236 assert.Empty(t, errors, "Should have no errors: %s", tc.desc)
237 } else {
238 assert.False(t, valid, "Should fail: %s", tc.desc)
239 assert.NotEmpty(t, errors, "Should have errors: %s", tc.desc)
240 }
241 })
242 }
243 }