@@ -48,7 +48,7 @@ public static class Builder {
48
48
private final Map <String , JsonMetaSchema > jsonMetaSchemas = new HashMap <String , JsonMetaSchema >();
49
49
private final Map <String , String > uriMap = new HashMap <String , String >();
50
50
private boolean forceHttps = true ;
51
-
51
+ private boolean removeEmptyFragmentSuffix = true ;
52
52
53
53
public Builder () {
54
54
// Adds support for creating {@link URL}s.
@@ -145,6 +145,11 @@ public Builder forceHttps(boolean forceHttps) {
145
145
return this ;
146
146
}
147
147
148
+ public Builder removeEmptyFragmentSuffix (boolean removeEmptyFragmentSuffix ) {
149
+ this .removeEmptyFragmentSuffix = removeEmptyFragmentSuffix ;
150
+ return this ;
151
+ }
152
+
148
153
public JsonSchemaFactory build () {
149
154
// create builtin keywords with (custom) formats.
150
155
return new JsonSchemaFactory (
@@ -155,7 +160,8 @@ public JsonSchemaFactory build() {
155
160
urnFactory ,
156
161
jsonMetaSchemas ,
157
162
uriMap ,
158
- forceHttps
163
+ forceHttps ,
164
+ removeEmptyFragmentSuffix
159
165
);
160
166
}
161
167
}
@@ -169,6 +175,7 @@ public JsonSchemaFactory build() {
169
175
private final Map <String , String > uriMap ;
170
176
private final ConcurrentMap <URI , JsonSchema > uriSchemaCache = new ConcurrentHashMap <URI , JsonSchema >();
171
177
private final boolean forceHttps ;
178
+ private final boolean removeEmptyFragmentSuffix ;
172
179
173
180
174
181
private JsonSchemaFactory (
@@ -179,7 +186,8 @@ private JsonSchemaFactory(
179
186
final URNFactory urnFactory ,
180
187
final Map <String , JsonMetaSchema > jsonMetaSchemas ,
181
188
final Map <String , String > uriMap ,
182
- final boolean forceHttps ) {
189
+ final boolean forceHttps ,
190
+ final boolean removeEmptyFragmentSuffix ) {
183
191
if (mapper == null ) {
184
192
throw new IllegalArgumentException ("ObjectMapper must not be null" );
185
193
} else if (defaultMetaSchemaURI == null || defaultMetaSchemaURI .trim ().isEmpty ()) {
@@ -203,6 +211,7 @@ private JsonSchemaFactory(
203
211
this .jsonMetaSchemas = jsonMetaSchemas ;
204
212
this .uriMap = uriMap ;
205
213
this .forceHttps = forceHttps ;
214
+ this .removeEmptyFragmentSuffix = removeEmptyFragmentSuffix ;
206
215
}
207
216
208
217
/**
@@ -281,7 +290,7 @@ protected ValidationContext createValidationContext(final JsonNode schemaNode) {
281
290
282
291
private JsonMetaSchema findMetaSchemaForSchema (final JsonNode schemaNode ) {
283
292
final JsonNode uriNode = schemaNode .get ("$schema" );
284
- final String uri = uriNode == null || uriNode .isNull () ? defaultMetaSchemaURI : normalizeMetaSchemaUri (uriNode .textValue (), forceHttps );
293
+ final String uri = uriNode == null || uriNode .isNull () ? defaultMetaSchemaURI : normalizeMetaSchemaUri (uriNode .textValue (), forceHttps , removeEmptyFragmentSuffix );
285
294
final JsonMetaSchema jsonMetaSchema = jsonMetaSchemas .get (uri );
286
295
if (jsonMetaSchema == null ) {
287
296
throw new JsonSchemaException ("Unknown MetaSchema: " + uri );
@@ -409,12 +418,17 @@ private boolean idMatchesSourceUri(final JsonMetaSchema metaSchema, final JsonNo
409
418
return result ;
410
419
}
411
420
412
- static protected String normalizeMetaSchemaUri (String u , boolean forceHttps ) {
421
+ static protected String normalizeMetaSchemaUri (String u , boolean forceHttps , boolean removeEmptyFragmentSuffix ) {
413
422
try {
414
423
URI uri = new URI (u );
415
424
String scheme = forceHttps ? "https" : uri .getScheme ();
416
425
URI newUri = new URI (scheme , uri .getUserInfo (), uri .getHost (), uri .getPort (), uri .getPath (), null , null );
417
- return newUri .toString ();
426
+
427
+ if (!removeEmptyFragmentSuffix && u .endsWith ("#" )) {
428
+ return newUri + "#" ;
429
+ } else {
430
+ return newUri .toString ();
431
+ }
418
432
} catch (URISyntaxException e ) {
419
433
throw new JsonSchemaException ("Wrong MetaSchema URI: " + u );
420
434
}
0 commit comments