-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSchemaParserTest.java
More file actions
444 lines (402 loc) · 16.7 KB
/
Copy pathSchemaParserTest.java
File metadata and controls
444 lines (402 loc) · 16.7 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
package com.retailsvc.http.spec.schema;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import java.util.List;
import java.util.Map;
import org.junit.jupiter.api.Test;
class SchemaParserTest {
@Test
void parsesString() {
Schema s = SchemaParser.parse(Map.of("type", "string", "minLength", 1, "maxLength", 64));
assertThat(s).isInstanceOf(StringSchema.class);
StringSchema str = (StringSchema) s;
assertThat(str.minLength()).isEqualTo(1);
assertThat(str.maxLength()).isEqualTo(64);
}
@Test
void parsesIntegerWithFormat() {
Schema s = SchemaParser.parse(Map.of("type", "integer", "format", "int64", "minimum", 0));
assertThat(s).isInstanceOf(IntegerSchema.class);
assertThat(((IntegerSchema) s).format()).isEqualTo("int64");
assertThat(((IntegerSchema) s).minimum().longValue()).isZero();
}
@Test
void parsesNumber() {
Schema s = SchemaParser.parse(Map.of("type", "number", "multipleOf", 0.5));
assertThat(s).isInstanceOf(NumberSchema.class);
assertThat(((NumberSchema) s).multipleOf()).isEqualTo(0.5);
}
@Test
void parsesBoolean() {
assertThat(SchemaParser.parse(Map.of("type", "boolean"))).isInstanceOf(BooleanSchema.class);
}
@Test
void parsesNull() {
assertThat(SchemaParser.parse(Map.of("type", "null"))).isInstanceOf(NullSchema.class);
}
@Test
void parsesRef() {
Schema s = SchemaParser.parse(Map.of("$ref", "#/components/schemas/User"));
assertThat(s).isInstanceOf(RefSchema.class);
assertThat(((RefSchema) s).pointer()).isEqualTo("#/components/schemas/User");
}
@Test
void parsesTypeArrayWithNullForNullable() {
Schema s = SchemaParser.parse(Map.of("type", List.of("string", "null")));
assertThat(s).isInstanceOf(StringSchema.class);
assertThat(s.types()).containsExactlyInAnyOrder(TypeName.STRING, TypeName.NULL);
}
@Test
void parsesLegacyNullableTrueAsTypeUnion() {
Schema s = SchemaParser.parse(Map.of("type", "string", "nullable", true));
assertThat(s.types()).containsExactlyInAnyOrder(TypeName.STRING, TypeName.NULL);
}
@Test
void parsesObjectWithRequiredAndProperties() {
Map<String, Object> raw =
Map.of(
"type", "object",
"required", List.of("name"),
"properties", Map.of("name", Map.of("type", "string")));
ObjectSchema o = (ObjectSchema) SchemaParser.parse(raw);
assertThat(o.required()).containsExactly("name");
assertThat(o.properties()).containsKey("name");
assertThat(o.properties().get("name")).isInstanceOf(StringSchema.class);
assertThat(o.additionalProperties()).isInstanceOf(AdditionalProperties.Allowed.class);
}
@Test
void parsesObjectWithAdditionalPropertiesFalse() {
Map<String, Object> raw = Map.of("type", "object", "additionalProperties", false);
ObjectSchema o = (ObjectSchema) SchemaParser.parse(raw);
assertThat(o.additionalProperties()).isInstanceOf(AdditionalProperties.Forbidden.class);
}
@Test
void parsesObjectWithAdditionalPropertiesSchema() {
Map<String, Object> raw =
Map.of("type", "object", "additionalProperties", Map.of("type", "string"));
ObjectSchema o = (ObjectSchema) SchemaParser.parse(raw);
assertThat(o.additionalProperties()).isInstanceOf(AdditionalProperties.SchemaConstraint.class);
}
@Test
void parsesArrayWithItems() {
Map<String, Object> raw =
Map.of(
"type",
"array",
"items",
Map.of("type", "integer"),
"minItems",
1,
"uniqueItems",
true);
ArraySchema a = (ArraySchema) SchemaParser.parse(raw);
assertThat(a.items()).isInstanceOf(IntegerSchema.class);
assertThat(a.minItems()).isEqualTo(1);
assertThat(a.uniqueItems()).isTrue();
}
@Test
void parsesOneOf() {
Map<String, Object> raw =
Map.of("oneOf", List.of(Map.of("type", "string"), Map.of("type", "integer")));
OneOfSchema o = (OneOfSchema) SchemaParser.parse(raw);
assertThat(o.options()).hasSize(2);
assertThat(o.options().get(0)).isInstanceOf(StringSchema.class);
}
@Test
void parsesAnyOfAllOfNot() {
assertThat(SchemaParser.parse(Map.of("anyOf", List.of(Map.of("type", "string")))))
.isInstanceOf(AnyOfSchema.class);
// allOf with a single branch flattens to the branch itself (no wrapper AllOfSchema).
assertThat(SchemaParser.parse(Map.of("allOf", List.of(Map.of("type", "string")))))
.isInstanceOf(StringSchema.class);
// allOf with multiple branches flattens into AllOfSchema.
assertThat(
SchemaParser.parse(
Map.of(
"allOf",
List.of(Map.of("type", "string"), Map.of("type", "string", "minLength", 1)))))
.isInstanceOf(AllOfSchema.class);
assertThat(SchemaParser.parse(Map.of("not", Map.of("type", "null"))))
.isInstanceOf(NotSchema.class);
}
@Test
void parsesConst() {
assertThat(SchemaParser.parse(Map.of("const", 42))).isInstanceOf(ConstSchema.class);
assertThat(((ConstSchema) SchemaParser.parse(Map.of("const", "a"))).value()).isEqualTo("a");
}
@Test
void parsesTopLevelEnumWithoutType() {
Schema s = SchemaParser.parse(Map.of("enum", List.of(1, 2, 3)));
assertThat(s).isInstanceOf(EnumSchema.class);
assertThat(((EnumSchema) s).values()).containsExactly(1, 2, 3);
}
@Test
void enumOnStringStaysAsStringSchema() {
Schema s = SchemaParser.parse(Map.of("type", "string", "enum", List.of("a", "b")));
assertThat(s).isInstanceOf(StringSchema.class);
assertThat(((StringSchema) s).enumValues()).containsExactly("a", "b");
}
@Test
void allOfWithSiblingTypeWrapsInImplicitAllOf() {
Schema s =
SchemaParser.parse(
Map.of(
"type", "object",
"required", List.of("x"),
"allOf", List.of(Map.of("type", "object", "required", List.of("y")))));
assertThat(s).isInstanceOf(AllOfSchema.class);
AllOfSchema all = (AllOfSchema) s;
assertThat(all.parts()).hasSize(2);
assertThat(all.parts().get(0)).isInstanceOf(ObjectSchema.class);
assertThat(((ObjectSchema) all.parts().get(0)).required()).containsExactly("x");
assertThat(all.parts().get(1)).isInstanceOf(ObjectSchema.class);
assertThat(((ObjectSchema) all.parts().get(1)).required()).containsExactly("y");
}
@Test
void anyOfWithSiblingTypeWrapsInImplicitAllOf() {
Schema s =
SchemaParser.parse(
Map.of(
"type",
"string",
"anyOf",
List.of(
Map.of("type", "string", "minLength", 1),
Map.of("type", "string", "maxLength", 10))));
assertThat(s).isInstanceOf(AllOfSchema.class);
AllOfSchema all = (AllOfSchema) s;
assertThat(all.parts()).hasSize(2);
assertThat(all.parts().get(0)).isInstanceOf(StringSchema.class);
assertThat(all.parts().get(1)).isInstanceOf(AnyOfSchema.class);
assertThat(((AnyOfSchema) all.parts().get(1)).options()).hasSize(2);
}
@Test
void oneOfWithSiblingTypeWrapsInImplicitAllOf() {
Schema s =
SchemaParser.parse(
Map.of(
"type",
"string",
"oneOf",
List.of(
Map.of("type", "string", "minLength", 1),
Map.of("type", "string", "maxLength", 10))));
assertThat(s).isInstanceOf(AllOfSchema.class);
AllOfSchema all = (AllOfSchema) s;
assertThat(all.parts()).hasSize(2);
assertThat(all.parts().get(0)).isInstanceOf(StringSchema.class);
assertThat(all.parts().get(1)).isInstanceOf(OneOfSchema.class);
}
@Test
void notWithSiblingTypeWrapsInImplicitAllOf() {
Schema s =
SchemaParser.parse(
Map.of("type", "string", "not", Map.of("type", "string", "maxLength", 2)));
assertThat(s).isInstanceOf(AllOfSchema.class);
AllOfSchema all = (AllOfSchema) s;
assertThat(all.parts()).hasSize(2);
assertThat(all.parts().get(0)).isInstanceOf(StringSchema.class);
assertThat(all.parts().get(1)).isInstanceOf(NotSchema.class);
}
@Test
void multipleCombinatorsInOneSchemaWrapInAllOf() {
Schema s =
SchemaParser.parse(
Map.of(
"anyOf", List.of(Map.of("type", "string"), Map.of("type", "integer")),
"not", Map.of("type", "boolean")));
assertThat(s).isInstanceOf(AllOfSchema.class);
AllOfSchema all = (AllOfSchema) s;
assertThat(all.parts()).hasSize(2);
assertThat(all.parts().get(0)).isInstanceOf(AnyOfSchema.class);
assertThat(all.parts().get(1)).isInstanceOf(NotSchema.class);
}
@Test
void parsesEmptySchemaAsPermissiveObject() {
// {} emits no type assertion (permissive ObjectSchema); JSON Schema 3.1 default.
// Behaviour change from the previous parser, which returned NullSchema.
Schema s = SchemaParser.parse(Map.of());
assertThat(s).isInstanceOf(ObjectSchema.class);
ObjectSchema obj = (ObjectSchema) s;
assertThat(obj.types()).isEmpty();
assertThat(obj.properties()).isEmpty();
assertThat(obj.required()).isEmpty();
assertThat(obj.additionalProperties()).isInstanceOf(AdditionalProperties.Allowed.class);
}
@Test
void parsesEmptyAllOfAsPermissiveObject() {
// allOf: [] contributes zero assertions; with no base assertion, the parser
// falls back to permissive object.
Schema s = SchemaParser.parse(Map.of("allOf", List.of()));
assertThat(s).isInstanceOf(ObjectSchema.class);
}
@Test
void allOfBranchesFlattenIntoOuterAllOf() {
Schema s =
SchemaParser.parse(
Map.of(
"type",
"string",
"allOf",
List.of(
Map.of("type", "string", "minLength", 1),
Map.of("type", "string", "maxLength", 10))));
assertThat(s).isInstanceOf(AllOfSchema.class);
AllOfSchema all = (AllOfSchema) s;
// Base + the two allOf branches flattened.
assertThat(all.parts()).hasSize(3);
assertThat(all.parts().get(0)).isInstanceOf(StringSchema.class);
assertThat(all.parts().get(1)).isInstanceOf(StringSchema.class);
assertThat(all.parts().get(2)).isInstanceOf(StringSchema.class);
assertThat(((StringSchema) all.parts().get(1)).minLength()).isEqualTo(1);
assertThat(((StringSchema) all.parts().get(2)).maxLength()).isEqualTo(10);
}
@Test
void aloneCombinatorStillReturnsCombinatorRecord() {
// Regression: when no base assertions are present, the result is still the
// single combinator record, not an AllOfSchema with a single child.
Schema s =
SchemaParser.parse(
Map.of("oneOf", List.of(Map.of("type", "string"), Map.of("type", "integer"))));
assertThat(s).isInstanceOf(OneOfSchema.class);
assertThat(((OneOfSchema) s).options()).hasSize(2);
}
@Test
void refWithSiblingsIsParsedSolo() {
// Deliberate limitation: $ref returns immediately and ignores sibling keywords.
// JSON Schema 2020-12 allows $ref + siblings but that interaction is a separate gap.
Schema s =
SchemaParser.parse(
Map.of(
"$ref", "#/components/schemas/Foo",
"type", "string",
"minLength", 5));
assertThat(s).isInstanceOf(RefSchema.class);
assertThat(((RefSchema) s).pointer()).isEqualTo("#/components/schemas/Foo");
}
@Test
void nestedAllOfIsNotFlattenedWhenBasePresent() {
// Flattening is one-level: when a base assertion plus an outer allOf coexist,
// the outer allOf branches are pulled into the assertions list, but any
// allOf nested inside one of those branches stays wrapped.
Schema s =
SchemaParser.parse(
Map.of(
"type",
"object",
"allOf",
List.of(
Map.of(
"allOf", List.of(Map.of("type", "string"), Map.of("type", "integer"))))));
assertThat(s).isInstanceOf(AllOfSchema.class);
AllOfSchema all = (AllOfSchema) s;
assertThat(all.parts()).hasSize(2);
assertThat(all.parts().get(0)).isInstanceOf(ObjectSchema.class);
assertThat(all.parts().get(1)).isInstanceOf(AllOfSchema.class);
assertThat(((AllOfSchema) all.parts().get(1)).parts()).hasSize(2);
}
@Test
void constWithSiblingAllOfWrapsInImplicitAllOf() {
// const acts as a base assertion, so a sibling combinator wraps both in AllOf.
Schema s =
SchemaParser.parse(
Map.of("const", 5, "allOf", List.of(Map.of("type", "integer", "minimum", 0))));
assertThat(s).isInstanceOf(AllOfSchema.class);
AllOfSchema all = (AllOfSchema) s;
assertThat(all.parts()).hasSize(2);
assertThat(all.parts().get(0)).isInstanceOf(ConstSchema.class);
assertThat(((ConstSchema) all.parts().get(0)).value()).isEqualTo(5);
assertThat(all.parts().get(1)).isInstanceOf(IntegerSchema.class);
}
@Test
void notWithEmptyInnerSchemaWrapsPermissiveObject() {
// not: {} parses the inner empty schema as the permissive ObjectSchema.
Schema s = SchemaParser.parse(Map.of("not", Map.of()));
assertThat(s).isInstanceOf(NotSchema.class);
assertThat(((NotSchema) s).schema()).isInstanceOf(ObjectSchema.class);
}
@Test
void parsesImplicitObjectFromShapeKeywords() {
// Schema with object-shape keywords but no explicit type still produces
// an ObjectSchema (parseBaseIfPresent's implicit-object branch).
Schema s =
SchemaParser.parse(
Map.of("required", List.of("x"), "properties", Map.of("x", Map.of("type", "string"))));
assertThat(s).isInstanceOf(ObjectSchema.class);
ObjectSchema obj = (ObjectSchema) s;
assertThat(obj.types()).isEmpty();
assertThat(obj.required()).containsExactly("x");
assertThat(obj.properties().get("x")).isInstanceOf(StringSchema.class);
}
@Test
void parsesImplicitArrayFromShapeKeywords() {
// Schema with array-shape keywords but no explicit type still produces
// an ArraySchema (parseBaseIfPresent's implicit-array branch).
Schema s = SchemaParser.parse(Map.of("items", Map.of("type", "integer"), "minItems", 1));
assertThat(s).isInstanceOf(ArraySchema.class);
ArraySchema arr = (ArraySchema) s;
assertThat(arr.types()).isEmpty();
assertThat(arr.items()).isInstanceOf(IntegerSchema.class);
assertThat(arr.minItems()).isEqualTo(1);
}
@Test
void oneOfContainingNestedAnyOfRecurses() {
// Pins that combinator branches are themselves passed through parse(),
// so nested combinators survive intact.
Schema s =
SchemaParser.parse(
Map.of(
"oneOf",
List.of(
Map.of("anyOf", List.of(Map.of("type", "string"), Map.of("type", "integer"))),
Map.of("type", "boolean"))));
assertThat(s).isInstanceOf(OneOfSchema.class);
OneOfSchema one = (OneOfSchema) s;
assertThat(one.options()).hasSize(2);
assertThat(one.options().get(0)).isInstanceOf(AnyOfSchema.class);
assertThat(((AnyOfSchema) one.options().get(0)).options()).hasSize(2);
assertThat(one.options().get(1)).isInstanceOf(BooleanSchema.class);
}
@Test
void parsesTrueAsAlwaysSchema() {
assertThat(SchemaParser.parse(Boolean.TRUE)).isInstanceOf(AlwaysSchema.class);
}
@Test
void parsesFalseAsNeverSchema() {
assertThat(SchemaParser.parse(Boolean.FALSE)).isInstanceOf(NeverSchema.class);
}
@Test
void rejectsNonMapNonBooleanRawSchema() {
assertThatThrownBy(() -> SchemaParser.parse("oops"))
.isInstanceOf(IllegalArgumentException.class)
.hasMessageContaining("schema must be a boolean or an object");
}
@Test
void parsesObjectWithBooleanPropertySchemas() {
Schema s =
SchemaParser.parse(
Map.of("type", "object", "properties", Map.of("x", Boolean.TRUE, "y", Boolean.FALSE)));
assertThat(s).isInstanceOf(ObjectSchema.class);
ObjectSchema obj = (ObjectSchema) s;
assertThat(obj.properties().get("x")).isInstanceOf(AlwaysSchema.class);
assertThat(obj.properties().get("y")).isInstanceOf(NeverSchema.class);
}
@Test
void parsesArrayWithBooleanItemsSchema() {
Schema s = SchemaParser.parse(Map.of("type", "array", "items", Boolean.TRUE));
assertThat(s).isInstanceOf(ArraySchema.class);
assertThat(((ArraySchema) s).items()).isInstanceOf(AlwaysSchema.class);
}
@Test
void parsesArrayWithBooleanFalseItems() {
Schema s = SchemaParser.parse(Map.of("type", "array", "items", Boolean.FALSE));
assertThat(s).isInstanceOf(ArraySchema.class);
assertThat(((ArraySchema) s).items()).isInstanceOf(NeverSchema.class);
}
@Test
void rejectsNullRawSchema() {
assertThatThrownBy(() -> SchemaParser.parse(null))
.isInstanceOf(IllegalArgumentException.class)
.hasMessageContaining("schema must be a boolean or an object");
}
}