Skip to content

Commit 8ff41bc

Browse files
authored
fix: invalid schemas, test suite (#211)
* fix: invalid schema files * feat: validate schemas against metaschema
1 parent 6b3df2a commit 8ff41bc

File tree

5 files changed

+53
-9
lines changed

5 files changed

+53
-9
lines changed

artifacts/src/main/resources/common/protocol-version-schema.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,9 @@
7878
},
7979
"profile": {
8080
"type": "array",
81-
"items": "string"
81+
"items": {
82+
"type": "string"
83+
}
8284
}
8385
},
8486
"required": [

artifacts/src/main/resources/negotiation/contract-schema.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@
3232
"oneOf": [
3333
{
3434
"type": "array",
35-
"items": "string"
35+
"items": {
36+
"type": "string"
37+
}
3638
},
3739
{
3840
"type": "string"

artifacts/src/test/java/org/eclipse/dsp/context/fixtures/AbstractJsonLdTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636

3737
import static com.apicatalog.jsonld.JsonLd.compact;
3838
import static com.apicatalog.jsonld.JsonLd.expand;
39-
import static com.networknt.schema.SpecVersion.VersionFlag.V202012;
39+
import static com.networknt.schema.SpecVersion.VersionFlag.V201909;
4040
import static java.lang.String.format;
4141
import static org.assertj.core.api.Assertions.assertThat;
4242
import static org.eclipse.dsp.DspConstants.DSP_CONTEXT;
@@ -61,7 +61,7 @@ protected void verifyRoundTrip(String jsonFile, String schemaFile) {
6161
var expanded = expand(JsonDocument.of(message)).options(options).get();
6262
var compacted = compact(JsonDocument.of(expanded), JsonDocument.of(compactionContext)).options(options).get();
6363

64-
var schemaFactory = JsonSchemaFactory.getInstance(V202012, builder ->
64+
var schemaFactory = JsonSchemaFactory.getInstance(V201909, builder ->
6565
builder.schemaMappers(schemaMappers -> schemaMappers.mapPrefix(DSP_PREFIX, CLASSPATH_SCHEMA))
6666
);
6767

artifacts/src/test/java/org/eclipse/dsp/schema/common/InvalidVersionSchemaTest.java

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ void verifyInvalidCases() throws IOException {
3737
assertThat(schema.validate(INVALID_AUTH_MISSING_PROTOCOL, JSON).iterator().next().getType()).isEqualTo(REQUIRED);
3838
assertThat(schema.validate(INVALID_AUTH_PROFILE_NOT_AN_ARRAY, JSON).iterator().next().getType()).isEqualTo(TYPE);
3939
assertThat(schema.validate(INVALID_SERVICEID_NOT_A_STRING, JSON).iterator().next().getType()).isEqualTo(TYPE);
40+
assertThat(schema.validate(INVALID_AUTH_PROFILE_NOT_A_STRING, JSON).iterator().next().getType()).isEqualTo(TYPE);
4041
}
4142

4243
@BeforeEach
@@ -160,14 +161,32 @@ void setUp() {
160161
"version": "1.0",
161162
"path": "/some/path/v1",
162163
"binding": "HTTPS",
163-
"auth": [{
164+
"auth": {
164165
"protocol": "some-protocol",
165166
"version": "2",
166-
"profile": "one-profile"
167-
}],
167+
"profile": ["one-profile"]
168+
},
168169
"serviceId": 666
169170
}
170171
]
171172
}
172173
""";
174+
175+
private static final String INVALID_AUTH_PROFILE_NOT_A_STRING = """
176+
{
177+
"protocolVersions": [
178+
{
179+
"version": "1.0",
180+
"path": "/some/path/v1",
181+
"binding": "HTTPS",
182+
"auth": {
183+
"protocol": "some-protocol",
184+
"version": "2",
185+
"profile": [666]
186+
},
187+
"serviceId": "some-uuid"
188+
}
189+
]
190+
}
191+
""";
173192
}

artifacts/src/test/java/org/eclipse/dsp/schema/fixtures/AbstractSchemaTest.java

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,25 @@
99
*
1010
* Contributors:
1111
* Metaform Systems, Inc. - initial API and implementation
12+
* SAP SE - fixes
1213
*
1314
*/
1415

1516
package org.eclipse.dsp.schema.fixtures;
1617

18+
import com.fasterxml.jackson.databind.JsonNode;
1719
import com.fasterxml.jackson.databind.ObjectMapper;
1820
import com.networknt.schema.JsonSchema;
1921
import com.networknt.schema.JsonSchemaFactory;
2022
import com.networknt.schema.SchemaLocation;
23+
import com.networknt.schema.SpecVersion;
2124
import com.networknt.schema.ValidationMessage;
2225

23-
import static com.networknt.schema.SpecVersion.VersionFlag.V202012;
26+
import java.io.File;
27+
import java.io.IOException;
28+
29+
import static com.networknt.schema.SchemaId.V201909;
30+
import static org.assertj.core.api.Assertions.assertThat;
2431
import static org.eclipse.dsp.DspConstants.DSP_PREFIX;
2532

2633
/**
@@ -35,15 +42,29 @@ public abstract class AbstractSchemaTest {
3542
protected static final String ENUM = "enum";
3643

3744
private static final String CLASSPATH_SCHEMA = "classpath:/";
45+
private static final String MAIN_RESOURCES = "src/main/resources";
46+
3847

3948
protected ObjectMapper mapper = new ObjectMapper();
4049
protected JsonSchema schema;
50+
protected JsonSchema metaSchema;
4151

4252
protected void setUp(String schemaFile) {
43-
var schemaFactory = JsonSchemaFactory.getInstance(V202012, builder ->
53+
var schemaFactory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V201909, builder ->
4454
builder.schemaMappers(schemaMappers -> schemaMappers.mapPrefix(DSP_PREFIX, CLASSPATH_SCHEMA))
4555
);
4656

57+
metaSchema = schemaFactory.getSchema(SchemaLocation.of(V201909));
58+
59+
JsonNode schemaAsNode;
60+
try {
61+
schemaAsNode = mapper.readTree(new File(MAIN_RESOURCES + schemaFile));
62+
} catch (IOException e) {
63+
throw new RuntimeException();
64+
}
65+
66+
assertThat(metaSchema.validate(schemaAsNode)).isEmpty();
67+
4768
schema = schemaFactory.getSchema(SchemaLocation.of(DSP_PREFIX + schemaFile));
4869
}
4970

0 commit comments

Comments
 (0)