18
18
19
19
import com .fasterxml .jackson .databind .JsonNode ;
20
20
import com .fasterxml .jackson .databind .ObjectMapper ;
21
+ import com .fasterxml .jackson .dataformat .yaml .YAMLMapper ;
21
22
import com .networknt .schema .uri .*;
22
23
import com .networknt .schema .urn .URNFactory ;
23
24
import org .slf4j .Logger ;
@@ -41,6 +42,7 @@ public class JsonSchemaFactory {
41
42
42
43
public static class Builder {
43
44
private ObjectMapper objectMapper = new ObjectMapper ();
45
+ private YAMLMapper yamlMapper = new YAMLMapper ();
44
46
private String defaultMetaSchemaURI ;
45
47
private final Map <String , URIFactory > uriFactoryMap = new HashMap <String , URIFactory >();
46
48
private final Map <String , URIFetcher > uriFetcherMap = new HashMap <String , URIFetcher >();
@@ -77,6 +79,11 @@ public Builder objectMapper(final ObjectMapper objectMapper) {
77
79
return this ;
78
80
}
79
81
82
+ public Builder yamlMapper (final YAMLMapper yamlMapper ) {
83
+ this .yamlMapper = yamlMapper ;
84
+ return this ;
85
+ }
86
+
80
87
public Builder defaultMetaSchemaURI (final String defaultMetaSchemaURI ) {
81
88
this .defaultMetaSchemaURI = defaultMetaSchemaURI ;
82
89
return this ;
@@ -154,6 +161,7 @@ public JsonSchemaFactory build() {
154
161
// create builtin keywords with (custom) formats.
155
162
return new JsonSchemaFactory (
156
163
objectMapper == null ? new ObjectMapper () : objectMapper ,
164
+ yamlMapper == null ? new YAMLMapper (): yamlMapper ,
157
165
defaultMetaSchemaURI ,
158
166
new URISchemeFactory (uriFactoryMap ),
159
167
new URISchemeFetcher (uriFetcherMap ),
@@ -166,7 +174,8 @@ public JsonSchemaFactory build() {
166
174
}
167
175
}
168
176
169
- private final ObjectMapper mapper ;
177
+ private final ObjectMapper jsonMapper ;
178
+ private final YAMLMapper yamlMapper ;
170
179
private final String defaultMetaSchemaURI ;
171
180
private final URISchemeFactory uriFactory ;
172
181
private final URISchemeFetcher uriFetcher ;
@@ -179,7 +188,8 @@ public JsonSchemaFactory build() {
179
188
180
189
181
190
private JsonSchemaFactory (
182
- final ObjectMapper mapper ,
191
+ final ObjectMapper jsonMapper ,
192
+ final YAMLMapper yamlMapper ,
183
193
final String defaultMetaSchemaURI ,
184
194
final URISchemeFactory uriFactory ,
185
195
final URISchemeFetcher uriFetcher ,
@@ -188,8 +198,10 @@ private JsonSchemaFactory(
188
198
final Map <String , String > uriMap ,
189
199
final boolean forceHttps ,
190
200
final boolean removeEmptyFragmentSuffix ) {
191
- if (mapper == null ) {
201
+ if (jsonMapper == null ) {
192
202
throw new IllegalArgumentException ("ObjectMapper must not be null" );
203
+ } else if (yamlMapper == null ) {
204
+ throw new IllegalArgumentException ("YAMLMapper must not be null" );
193
205
} else if (defaultMetaSchemaURI == null || defaultMetaSchemaURI .trim ().isEmpty ()) {
194
206
throw new IllegalArgumentException ("defaultMetaSchemaURI must not be null or empty" );
195
207
} else if (uriFactory == null ) {
@@ -203,7 +215,8 @@ private JsonSchemaFactory(
203
215
} else if (uriMap == null ) {
204
216
throw new IllegalArgumentException ("URL Mappings must not be null" );
205
217
}
206
- this .mapper = mapper ;
218
+ this .jsonMapper = jsonMapper ;
219
+ this .yamlMapper = yamlMapper ;
207
220
this .defaultMetaSchemaURI = defaultMetaSchemaURI ;
208
221
this .uriFactory = uriFactory ;
209
222
this .uriFetcher = uriFetcher ;
@@ -274,7 +287,8 @@ public static Builder builder(final JsonSchemaFactory blueprint) {
274
287
Builder builder = builder ()
275
288
.addMetaSchemas (blueprint .jsonMetaSchemas .values ())
276
289
.defaultMetaSchemaURI (blueprint .defaultMetaSchemaURI )
277
- .objectMapper (blueprint .mapper )
290
+ .objectMapper (blueprint .jsonMapper )
291
+ .yamlMapper (blueprint .yamlMapper )
278
292
.addUriMappings (blueprint .uriMap );
279
293
280
294
for (Map .Entry <String , URIFactory > entry : blueprint .uriFactory .getURIFactories ().entrySet ()) {
@@ -319,7 +333,7 @@ public URIFactory getUriFactory() {
319
333
320
334
public JsonSchema getSchema (final String schema , final SchemaValidatorsConfig config ) {
321
335
try {
322
- final JsonNode schemaNode = mapper .readTree (schema );
336
+ final JsonNode schemaNode = jsonMapper .readTree (schema );
323
337
return newJsonSchema (null , schemaNode , config );
324
338
} catch (IOException ioe ) {
325
339
logger .error ("Failed to load json schema!" , ioe );
@@ -333,7 +347,7 @@ public JsonSchema getSchema(final String schema) {
333
347
334
348
public JsonSchema getSchema (final InputStream schemaStream , final SchemaValidatorsConfig config ) {
335
349
try {
336
- final JsonNode schemaNode = mapper .readTree (schemaStream );
350
+ final JsonNode schemaNode = jsonMapper .readTree (schemaStream );
337
351
return newJsonSchema (null , schemaNode , config );
338
352
} catch (IOException ioe ) {
339
353
logger .error ("Failed to load json schema!" , ioe );
@@ -370,7 +384,14 @@ public JsonSchema getSchema(final URI schemaUri, final SchemaValidatorsConfig co
370
384
371
385
try {
372
386
inputStream = this .uriFetcher .fetch (mappedUri );
373
- final JsonNode schemaNode = mapper .readTree (inputStream );
387
+
388
+ final JsonNode schemaNode ;
389
+ if (isYaml (mappedUri )) {
390
+ schemaNode = yamlMapper .readTree (inputStream );
391
+ } else {
392
+ schemaNode = jsonMapper .readTree (inputStream );
393
+ }
394
+
374
395
final JsonMetaSchema jsonMetaSchema = findMetaSchemaForSchema (schemaNode );
375
396
376
397
JsonSchema jsonSchema ;
@@ -428,6 +449,19 @@ private boolean idMatchesSourceUri(final JsonMetaSchema metaSchema, final JsonNo
428
449
return result ;
429
450
}
430
451
452
+ private boolean isYaml (final URI schemaUri ) {
453
+ final String schemeSpecificPart = schemaUri .getSchemeSpecificPart ();
454
+ final int idx = schemeSpecificPart .lastIndexOf ('.' );
455
+
456
+ if (idx == -1 ) {
457
+ // no extension; assume json
458
+ return false ;
459
+ }
460
+
461
+ final String extension = schemeSpecificPart .substring (idx );
462
+ return (".yml" .equals (extension ) || ".yaml" .equals (extension ));
463
+ }
464
+
431
465
static protected String normalizeMetaSchemaUri (String u , boolean forceHttps , boolean removeEmptyFragmentSuffix ) {
432
466
try {
433
467
URI uri = new URI (u );
0 commit comments