Skip to content

Commit b4abbc2

Browse files
authored
Fix ref with sibling id from 2019-09 (#1203)
1 parent 7bebfeb commit b4abbc2

File tree

2 files changed

+95
-1
lines changed

2 files changed

+95
-1
lines changed

src/main/java/com/networknt/schema/keyword/RefValidator.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import com.networknt.schema.Schema;
2424
import com.networknt.schema.SchemaException;
2525
import com.networknt.schema.SchemaRef;
26+
import com.networknt.schema.SpecificationVersion;
2627
import com.networknt.schema.path.NodePath;
2728
import com.networknt.schema.utils.ThreadSafeCachingSupplier;
2829
import com.networknt.schema.SchemaLocation;
@@ -146,7 +147,8 @@ private static void copySchemaResources(SchemaContext schemaContext, Schema sche
146147
private static String resolve(Schema parentSchema, String refValue) {
147148
// $ref prevents a sibling $id from changing the base uri
148149
Schema base = parentSchema;
149-
if (parentSchema.getId() != null && parentSchema.getParentSchema() != null) {
150+
if (parentSchema.getId() != null && parentSchema.getParentSchema() != null && parentSchema.getSchemaContext()
151+
.getDialect().getSpecificationVersion().getOrder() <= SpecificationVersion.DRAFT_7.getOrder()) {
150152
base = parentSchema.getParentSchema();
151153
}
152154
return SchemaLocation.resolve(base.getSchemaLocation(), refValue);

src/test/java/com/networknt/schema/RefValidatorTest.java

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import static org.junit.jupiter.api.Assertions.assertEquals;
1919
import static org.junit.jupiter.api.Assertions.assertFalse;
20+
import static org.junit.jupiter.api.Assertions.assertTrue;
2021

2122
import java.util.Collections;
2223
import java.util.List;
@@ -134,4 +135,95 @@ void classPathNoSlash() {
134135
+ "}";
135136
assertFalse(schema.validate(inputData, InputFormat.JSON, OutputFormat.BOOLEAN));
136137
}
138+
139+
@Test
140+
void refSiblingId07AndBelow() {
141+
String schemaData = "{\r\n"
142+
+ " \"$id\": \"http://localhost:1234/sibling_id/base/\",\r\n"
143+
+ " \"definitions\": {\r\n"
144+
+ " \"foo\": {\r\n"
145+
+ " \"$id\": \"http://localhost:1234/sibling_id/foo.json\",\r\n"
146+
+ " \"type\": \"string\"\r\n"
147+
+ " },\r\n"
148+
+ " \"base_foo\": {\r\n"
149+
+ " \"$comment\": \"this canonical uri is http://localhost:1234/sibling_id/base/foo.json\",\r\n"
150+
+ " \"$id\": \"foo.json\",\r\n"
151+
+ " \"type\": \"number\"\r\n"
152+
+ " }\r\n"
153+
+ " },\r\n"
154+
+ " \"allOf\": [\r\n"
155+
+ " {\r\n"
156+
+ " \"$comment\": \"$ref resolves to http://localhost:1234/sibling_id/base/foo.json, not http://localhost:1234/sibling_id/foo.json\",\r\n"
157+
+ " \"$id\": \"http://localhost:1234/sibling_id/\",\r\n"
158+
+ " \"$ref\": \"foo.json\"\r\n"
159+
+ " }\r\n"
160+
+ " ]\r\n"
161+
+ "}";
162+
SchemaRegistry factory = SchemaRegistry.withDefaultDialect(SpecificationVersion.DRAFT_7);
163+
Schema schema = factory.getSchema(schemaData);
164+
String inputData = "1";
165+
assertTrue(schema.validate(inputData, InputFormat.JSON, OutputFormat.BOOLEAN));
166+
}
167+
168+
@Test
169+
void refSiblingId201909AndAbove() {
170+
String schemaData = "{\r\n"
171+
+ " \"$id\": \"http://localhost:1234/sibling_id/base/\",\r\n"
172+
+ " \"definitions\": {\r\n"
173+
+ " \"foo\": {\r\n"
174+
+ " \"$id\": \"http://localhost:1234/sibling_id/foo.json\",\r\n"
175+
+ " \"type\": \"string\"\r\n"
176+
+ " },\r\n"
177+
+ " \"base_foo\": {\r\n"
178+
+ " \"$comment\": \"this canonical uri is http://localhost:1234/sibling_id/base/foo.json\",\r\n"
179+
+ " \"$id\": \"foo.json\",\r\n"
180+
+ " \"type\": \"number\"\r\n"
181+
+ " }\r\n"
182+
+ " },\r\n"
183+
+ " \"allOf\": [\r\n"
184+
+ " {\r\n"
185+
+ " \"$comment\": \"$ref resolves to http://localhost:1234/sibling_id/base/foo.json, not http://localhost:1234/sibling_id/foo.json\",\r\n"
186+
+ " \"$id\": \"http://localhost:1234/sibling_id/\",\r\n"
187+
+ " \"$ref\": \"foo.json\"\r\n"
188+
+ " }\r\n"
189+
+ " ]\r\n"
190+
+ "}";
191+
SchemaRegistry factory = SchemaRegistry.withDefaultDialect(SpecificationVersion.DRAFT_2019_09);
192+
Schema schema = factory.getSchema(schemaData);
193+
String inputData = "\"a\"";
194+
assertTrue(schema.validate(inputData, InputFormat.JSON, OutputFormat.BOOLEAN));
195+
}
196+
197+
@Test
198+
void refSiblingIdInner201909AndAbove() {
199+
String schemaData = "{\r\n"
200+
+ " \"$schema\": \"https://json-schema.org/draft/2020-12/schema\",\r\n"
201+
+ " \"$id\": \"http://example.com/schema-relative-uri-defs1.json\",\r\n"
202+
+ " \"properties\": {\r\n"
203+
+ " \"foo\": {\r\n"
204+
+ " \"$id\": \"schema-relative-uri-defs2.json\",\r\n"
205+
+ " \"$defs\": {\r\n"
206+
+ " \"inner\": {\r\n"
207+
+ " \"properties\": {\r\n"
208+
+ " \"bar\": {\r\n"
209+
+ " \"type\": \"string\"\r\n"
210+
+ " }\r\n"
211+
+ " }\r\n"
212+
+ " }\r\n"
213+
+ " },\r\n"
214+
+ " \"$ref\": \"#/$defs/inner\"\r\n"
215+
+ " }\r\n"
216+
+ " },\r\n"
217+
+ " \"$ref\": \"schema-relative-uri-defs2.json\"\r\n"
218+
+ "}";
219+
SchemaRegistry factory = SchemaRegistry.withDefaultDialect(SpecificationVersion.DRAFT_2019_09);
220+
Schema schema = factory.getSchema(schemaData);
221+
String inputData = "{\r\n"
222+
+ " \"foo\": {\r\n"
223+
+ " \"bar\": \"a\"\r\n"
224+
+ " },\r\n"
225+
+ " \"bar\": \"a\"\r\n"
226+
+ "}";
227+
assertTrue(schema.validate(inputData, InputFormat.JSON, OutputFormat.BOOLEAN));
228+
}
137229
}

0 commit comments

Comments
 (0)