Skip to content

Commit 61323ba

Browse files
authored
Fix root classpath resolve (#1000)
1 parent 7abb68c commit 61323ba

File tree

7 files changed

+111
-3
lines changed

7 files changed

+111
-3
lines changed

src/main/java/com/networknt/schema/AbsoluteIri.java

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,10 @@ public static String resolve(String parent, String iri) {
101101
String base = parent;
102102
int scheme = parent.indexOf("://");
103103
if (scheme == -1) {
104-
scheme = 0;
104+
scheme = parent.indexOf(':');
105+
if (scheme == -1) {
106+
scheme = 0;
107+
}
105108
} else {
106109
scheme = scheme + 3;
107110
}
@@ -114,7 +117,15 @@ public static String resolve(String parent, String iri) {
114117
} else if (".".equals(iriParts[x])) {
115118
// skip
116119
} else {
117-
base = base + "/" + iriParts[x];
120+
if (base.endsWith(":")) {
121+
if (parent.length() > base.length() && parent.charAt(base.length()) == '/') {
122+
base = base + "/" + iriParts[x];
123+
} else {
124+
base = base + iriParts[x];
125+
}
126+
} else {
127+
base = base + "/" + iriParts[x];
128+
}
118129
}
119130
}
120131
if (iri.endsWith("/")) {
@@ -127,6 +138,12 @@ public static String resolve(String parent, String iri) {
127138

128139
protected static String parent(String iri, int scheme) {
129140
int slash = iri.lastIndexOf('/');
141+
if (slash == -1) {
142+
slash = iri.lastIndexOf(':');
143+
if (slash != -1) {
144+
slash = slash + 1;
145+
}
146+
}
130147
if (slash != -1 && slash > scheme) {
131148
return iri.substring(0, slash);
132149
}

src/main/java/com/networknt/schema/OutputFormat.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ T format(JsonSchema jsonSchema, Set<ValidationMessage> validationMessages,
6060
/**
6161
* The Boolean output format.
6262
*/
63-
public static final Flag BOOLEAN = new Flag();
63+
public static final Boolean BOOLEAN = new Boolean();
6464

6565
/**
6666
* The Flag output format.

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,30 @@ void relativeAtRootWithTrailingSlash() {
6060
@Test
6161
void relativeAtRootWithSchemeSpecificPart() {
6262
AbsoluteIri iri = new AbsoluteIri("classpath:resource");
63+
assertEquals("classpath:test.json", iri.resolve("test.json").toString());
64+
}
65+
66+
@Test
67+
void relativeAtRootWithSchemeSpecificPartNoPath() {
68+
AbsoluteIri iri = new AbsoluteIri("classpath:");
69+
assertEquals("classpath:test.json", iri.resolve("test.json").toString());
70+
}
71+
72+
@Test
73+
void relativeAtRootWithSchemeSpecificPartSlash() {
74+
AbsoluteIri iri = new AbsoluteIri("classpath:/resource");
75+
assertEquals("classpath:/test.json", iri.resolve("test.json").toString());
76+
}
77+
78+
@Test
79+
void relativeAtRootWithSchemeSpecificPartNoPathTrailingSlash() {
80+
AbsoluteIri iri = new AbsoluteIri("classpath:/");
81+
assertEquals("classpath:/test.json", iri.resolve("test.json").toString());
82+
}
83+
84+
@Test
85+
void relativeAtRootWithSchemeSpecificPartTrailingSlash() {
86+
AbsoluteIri iri = new AbsoluteIri("classpath:resource/");
6387
assertEquals("classpath:resource/test.json", iri.resolve("test.json").toString());
6488
}
6589

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package com.networknt.schema;
1717

1818
import static org.junit.jupiter.api.Assertions.assertEquals;
19+
import static org.junit.jupiter.api.Assertions.assertFalse;
1920

2021
import java.util.Collections;
2122
import java.util.Set;
@@ -103,4 +104,36 @@ void resolveComplex() {
103104
Set<ValidationMessage> messages = jsonSchema.validate("\"string\"", InputFormat.JSON);
104105
assertEquals(1, messages.size());
105106
}
107+
108+
@Test
109+
void classPathSlash() {
110+
JsonSchemaFactory factory = JsonSchemaFactory.getInstance(VersionFlag.V201909);
111+
JsonSchema schema = factory.getSchema(SchemaLocation.of("classpath:/schema/main/main.json"));
112+
String inputData = "{\r\n"
113+
+ " \"fields\": {\r\n"
114+
+ " \"ids\": {\r\n"
115+
+ " \"value\": {\r\n"
116+
+ " \"value\": 1\r\n"
117+
+ " }\r\n"
118+
+ " }\r\n"
119+
+ " }\r\n"
120+
+ "}";
121+
assertFalse(schema.validate(inputData, InputFormat.JSON, OutputFormat.BOOLEAN));
122+
}
123+
124+
@Test
125+
void classPathNoSlash() {
126+
JsonSchemaFactory factory = JsonSchemaFactory.getInstance(VersionFlag.V201909);
127+
JsonSchema schema = factory.getSchema(SchemaLocation.of("classpath:schema/main/main.json"));
128+
String inputData = "{\r\n"
129+
+ " \"fields\": {\r\n"
130+
+ " \"ids\": {\r\n"
131+
+ " \"value\": {\r\n"
132+
+ " \"value\": 1\r\n"
133+
+ " }\r\n"
134+
+ " }\r\n"
135+
+ " }\r\n"
136+
+ "}";
137+
assertFalse(schema.validate(inputData, InputFormat.JSON, OutputFormat.BOOLEAN));
138+
}
106139
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"title": "child schema",
3+
"type": "object",
4+
"properties": {
5+
"value": {
6+
"$ref": "./child2.json"
7+
}
8+
}
9+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"title": "child2 schema",
3+
"type": "object",
4+
"properties": {
5+
"value": {
6+
"type": "string",
7+
"$comment": "child value."
8+
}
9+
}
10+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"$schema": "https://json-schema.org/draft/2019-09/schema",
3+
"title": "main schema",
4+
"type": "object",
5+
"properties": {
6+
"fields": {
7+
"type": "object",
8+
"properties": {
9+
"ids": {
10+
"$ref": "../common/child.json"
11+
}
12+
}
13+
}
14+
}
15+
}

0 commit comments

Comments
 (0)