-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSchemaParser.java
More file actions
227 lines (202 loc) · 7.45 KB
/
Copy pathSchemaParser.java
File metadata and controls
227 lines (202 loc) · 7.45 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
package com.retailsvc.http.spec.schema;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
public final class SchemaParser {
private SchemaParser() {}
private static final String FORMAT_KEY = "format";
public static Schema parse(Object raw) {
if (raw instanceof Boolean b) {
return b ? new AlwaysSchema() : new NeverSchema();
}
if (raw instanceof Map<?, ?> map) {
@SuppressWarnings("unchecked")
Map<String, Object> typed = (Map<String, Object>) map;
return parseMap(typed);
}
throw new IllegalArgumentException("schema must be a boolean or an object, was: " + raw);
}
@SuppressWarnings("unchecked")
private static Schema parseMap(Map<String, Object> raw) {
if (raw.containsKey("$ref")) {
return new RefSchema((String) raw.get("$ref"));
}
List<Schema> assertions = new ArrayList<>();
Schema base = parseBaseIfPresent(raw);
if (base != null) {
assertions.add(base);
}
if (raw.containsKey("allOf")) {
assertions.addAll(parseList(raw, "allOf"));
}
if (raw.containsKey("anyOf")) {
assertions.add(new AnyOfSchema(parseList(raw, "anyOf")));
}
if (raw.containsKey("oneOf")) {
assertions.add(new OneOfSchema(parseList(raw, "oneOf")));
}
if (raw.containsKey("not")) {
assertions.add(new NotSchema(parse(raw.get("not"))));
}
return switch (assertions.size()) {
case 0 -> permissiveObject();
case 1 -> assertions.getFirst();
default -> new AllOfSchema(List.copyOf(assertions));
};
}
@SuppressWarnings("unchecked")
private static Schema parseBaseIfPresent(Map<String, Object> raw) {
if (raw.containsKey("const")) {
return new ConstSchema(raw.get("const"));
}
if (raw.containsKey("enum") && !raw.containsKey("type")) {
return new EnumSchema(List.copyOf((List<Object>) raw.get("enum")));
}
Set<TypeName> types = parseTypes(raw);
if (types.isEmpty() && !hasObjectShapeKeywords(raw) && !hasArrayShapeKeywords(raw)) {
return null;
}
if (types.isEmpty() && hasObjectShapeKeywords(raw)) {
return parseObject(raw, types);
}
if (types.isEmpty() && hasArrayShapeKeywords(raw)) {
return parseArray(raw, types);
}
TypeName primary =
types.stream().filter(t -> t != TypeName.NULL).findFirst().orElse(TypeName.NULL);
return switch (primary) {
case STRING -> parseString(raw, types);
case INTEGER -> parseInteger(raw, types);
case NUMBER -> parseNumber(raw, types);
case BOOLEAN -> new BooleanSchema(types);
case NULL -> new NullSchema();
case OBJECT -> parseObject(raw, types);
case ARRAY -> parseArray(raw, types);
};
}
private static boolean hasObjectShapeKeywords(Map<String, Object> raw) {
return raw.containsKey("properties")
|| raw.containsKey("required")
|| raw.containsKey("additionalProperties")
|| raw.containsKey("minProperties")
|| raw.containsKey("maxProperties");
}
private static boolean hasArrayShapeKeywords(Map<String, Object> raw) {
return raw.containsKey("items")
|| raw.containsKey("minItems")
|| raw.containsKey("maxItems")
|| raw.containsKey("uniqueItems");
}
private static Schema permissiveObject() {
return new ObjectSchema(
Set.of(), Map.of(), List.of(), new AdditionalProperties.Allowed(), null, null);
}
private static Set<TypeName> parseTypes(Map<String, Object> raw) {
Object t = raw.get("type");
EnumSet<TypeName> out = EnumSet.noneOf(TypeName.class);
if (t instanceof String s) {
out.add(TypeName.fromJsonSchema(s));
} else if (t instanceof List<?> list) {
for (Object name : list) {
out.add(TypeName.fromJsonSchema((String) name));
}
}
if (Boolean.TRUE.equals(raw.get("nullable"))) {
out.add(TypeName.NULL);
}
return out;
}
@SuppressWarnings("unchecked")
private static StringSchema parseString(Map<String, Object> raw, Set<TypeName> types) {
return new StringSchema(
types,
(String) raw.get("pattern"),
toIntOrNull(raw.get("minLength")),
toIntOrNull(raw.get("maxLength")),
(String) raw.get(FORMAT_KEY),
(List<String>) raw.get("enum"));
}
private static IntegerSchema parseInteger(Map<String, Object> raw, Set<TypeName> types) {
return new IntegerSchema(
types,
toLongOrNull(raw.get("minimum")),
toLongOrNull(raw.get("maximum")),
toLongOrNull(raw.get("exclusiveMinimum")),
toLongOrNull(raw.get("exclusiveMaximum")),
toLongOrNull(raw.get("multipleOf")),
(String) raw.get(FORMAT_KEY));
}
private static NumberSchema parseNumber(Map<String, Object> raw, Set<TypeName> types) {
return new NumberSchema(
types,
(Number) raw.get("minimum"),
(Number) raw.get("maximum"),
(Number) raw.get("exclusiveMinimum"),
(Number) raw.get("exclusiveMaximum"),
(Number) raw.get("multipleOf"),
(String) raw.get(FORMAT_KEY));
}
@SuppressWarnings("unchecked")
private static ObjectSchema parseObject(Map<String, Object> raw, Set<TypeName> types) {
Map<String, Object> rawProps = (Map<String, Object>) raw.getOrDefault("properties", Map.of());
Map<String, Schema> properties = new LinkedHashMap<>();
for (var e : rawProps.entrySet()) {
properties.put(e.getKey(), parse(e.getValue()));
}
List<String> required = (List<String>) raw.getOrDefault("required", List.of());
AdditionalProperties ap = parseAdditionalProperties(raw.get("additionalProperties"));
return new ObjectSchema(
types,
Map.copyOf(properties),
List.copyOf(required),
ap,
toIntOrNull(raw.get("minProperties")),
toIntOrNull(raw.get("maxProperties")));
}
@SuppressWarnings("unchecked")
private static AdditionalProperties parseAdditionalProperties(Object value) {
return switch (value) {
case null -> new AdditionalProperties.Allowed();
case Boolean b when b -> new AdditionalProperties.Allowed();
case Boolean _ -> new AdditionalProperties.Forbidden();
default -> new AdditionalProperties.SchemaConstraint(parse(value));
};
}
@SuppressWarnings("unchecked")
private static ArraySchema parseArray(Map<String, Object> raw, Set<TypeName> types) {
Object itemsRaw = raw.get("items");
Schema itemSchema;
if (itemsRaw == null) {
itemSchema = new NullSchema();
} else if (itemsRaw instanceof Boolean b) {
itemSchema = b ? new AlwaysSchema() : new NeverSchema();
} else {
Map<String, Object> items = (Map<String, Object>) itemsRaw;
itemSchema = items.isEmpty() ? new NullSchema() : parse(items);
}
return new ArraySchema(
types,
itemSchema,
toIntOrNull(raw.get("minItems")),
toIntOrNull(raw.get("maxItems")),
Boolean.TRUE.equals(raw.get("uniqueItems")));
}
@SuppressWarnings("unchecked")
private static List<Schema> parseList(Map<String, Object> raw, String key) {
List<Object> raws = (List<Object>) raw.get(key);
List<Schema> out = new ArrayList<>(raws.size());
for (Object r : raws) {
out.add(parse(r));
}
return List.copyOf(out);
}
private static Integer toIntOrNull(Object v) {
return v == null ? null : ((Number) v).intValue();
}
private static Long toLongOrNull(Object v) {
return v == null ? null : ((Number) v).longValue();
}
}