Commit fc67e09e authored by devesh bajpai's avatar devesh bajpai
Browse files

Merge branch 'fix-check-invalid-ref' into 'master'

Check invalid refences in schema

See merge request !106
parents bc51b1bd 1c200cca
Pipeline #42868 passed with stages
in 99 minutes and 3 seconds
......@@ -249,6 +249,7 @@ The following software have components provided under the terms of this license:
- AWS Java SDK for Amazon Rekognition (from https://aws.amazon.com/sdkforjava)
- AWS Java SDK for Amazon Route 53 Auto Naming (from https://aws.amazon.com/sdkforjava)
- AWS Java SDK for Amazon Route 53 Resolver (from https://aws.amazon.com/sdkforjava)
- AWS Java SDK for Amazon Route53 (from https://aws.amazon.com/sdkforjava)
- AWS Java SDK for Amazon S3 (from https://aws.amazon.com/sdkforjava)
- AWS Java SDK for Amazon S3 on Outposts (from https://aws.amazon.com/sdkforjava)
- AWS Java SDK for Amazon SES (from https://aws.amazon.com/sdkforjava)
......
......@@ -49,23 +49,29 @@ public class SchemaResolver {
JSONObject originalSchema = new JSONObject(schema);
Map<String, String> refSchemas = new HashMap<>();
findAndResolveRef(originalSchema, refSchemas);
JSONObject definition = fetchObjectFromJSON(originalSchema, SchemaConstants.DEFINITIONS);
Map<String, Object> definitionMap = new HashMap<>();
if (definition != null) {
definitionMap = definition.toMap();
}
findAndResolveRef(originalSchema, refSchemas);
for (Map.Entry<String, String> entry : refSchemas.entrySet()) {
JSONObject refSchema = new JSONObject(entry.getValue());
JSONObject refSchemaDef = fetchObjectFromJSON(refSchema, SchemaConstants.DEFINITIONS);
if (refSchemaDef != null)
definitionMap.putAll(refSchemaDef.toMap());
refSchema.remove(SchemaConstants.DEFINITIONS);
definitionMap.put(entry.getKey(), refSchema);
for (Map.Entry<String, String> entry : refSchemas.entrySet()) {
if (entry.getValue() == null) {
if (!definitionMap.containsKey(entry.getKey())) {
throw new BadRequestException(
String.format("Invalid input, no '%s' definition found but provided as a reference", entry.getKey()));
}
} else {
JSONObject refSchema = new JSONObject(entry.getValue());
JSONObject refSchemaDef = fetchObjectFromJSON(refSchema, SchemaConstants.DEFINITIONS);
if (refSchemaDef != null)
definitionMap.putAll(refSchemaDef.toMap());
refSchema.remove(SchemaConstants.DEFINITIONS);
definitionMap.put(entry.getKey(), refSchema);
}
}
if (!definitionMap.isEmpty()) {
originalSchema.put(SchemaConstants.DEFINITIONS, definitionMap);
......@@ -119,6 +125,7 @@ public class SchemaResolver {
throws BadRequestException, ApplicationException {
String value = String.valueOf(object);
if (value.startsWith("#")) {
refSchemas.put(value.substring(value.lastIndexOf('/') + 1), null);
return value;
} else if (value.startsWith("https")) {
String refSchema = getSchemaFromExternal(value);
......
......@@ -118,4 +118,27 @@ public class SchemaResolverTest {
fail("Should not succeed");
}
@Test
public void testResolveSchema_noDefinitionForRef()
throws JSONException, BadRequestException, ApplicationException, NotFoundException, IOException {
String originalSchema = new FileUtils().read("/test_schema/originalSchemaWithRefButNoDefinition.json");
String referenceSchema = new FileUtils().read("/test_schema/referenceSchema.json");
Mockito.when(schemaService.getSchema("os:wks:anyCrsFeatureCollection.1.0")).thenReturn(referenceSchema);
expectedException.expect(BadRequestException.class);
expectedException.expectMessage(
"Invalid input, no 'person' definition found but provided as a reference");
schemaResolver.resolveSchema(originalSchema);
fail("Should not succeed");
}
@Test
public void testResolveSchema_withDefinitionForRef()
throws JSONException, BadRequestException, ApplicationException, NotFoundException, IOException {
String resolvedSchema = new FileUtils().read("/test_schema/resolvedSchemaWithDefinition.json");
String originalSchema = new FileUtils().read("/test_schema/originalSchemaRefWithDefinition.json");
String referenceSchema = new FileUtils().read("/test_schema/referenceSchema.json");
Mockito.when(schemaService.getSchema("os:wks:anyCrsFeatureCollection.1.0")).thenReturn(referenceSchema);
JSONAssert.assertEquals(resolvedSchema, schemaResolver.resolveSchema(originalSchema), JSONCompareMode.NON_EXTENSIBLE);
}
}
\ No newline at end of file
......@@ -10,9 +10,6 @@
"description": "The well's original location as AnyCrsFeatureCollection - a structure similar to but distinct from GeoJSON.",
"title": "Original CRS Location",
"$ref": "os:wks:anyCrsFeatureCollection.1.0"
},
"allOf": {
"$ref": "#/definition/os:wks:well.1.0"
},
"locationWGS84": {
"description": "The well's location as GeoJSON FeatureCollection.",
......
{
"$schema": "http://json-schema.org/draft-07/schema#",
"x-os-lifecycle-state": "published",
"description": "The entity well.",
"title": "Well",
"type": "object",
"definitions": {
"person": {
"type": "object",
"properties": {
"name": {
"type": "string",
"$ref": "os:wks:anyCrsFeatureCollection.1.0"
}
}
}
},
"properties": {
"person": {
"description": "Information for person",
"title": "Information for person",
"$ref": "#/definitions/person"
},
"locationOriginalCRS": {
"description": "The well's original location as AnyCrsFeatureCollection - a structure similar to but distinct from GeoJSON.",
"title": "Original CRS Location",
"$ref": "os:wks:anyCrsFeatureCollection.1.0"
},
"locationWGS84": {
"description": "The well's location as GeoJSON FeatureCollection.",
"title": "WGS84 Location",
"$ref": "https://geojson.org/schema/FeatureCollection.json",
"example": {
"features": [
{
"geometry": {
"coordinates": [
-92.11569999999999,
29.8823,
153.4779442519685
],
"type": "Point"
},
"type": "Feature",
"properties": {
"name": "Newton 2-31"
}
}
],
"type": "FeatureCollection"
}
}
}
}
......@@ -10,9 +10,6 @@
"description": "The well's original location as AnyCrsFeatureCollection - a structure similar to but distinct from GeoJSON.",
"title": "Original CRS Location",
"$ref": "os:wks:anyCrsFeatureCollection.1.0.json"
},
"allOf": {
"$ref": "#/definition/os:wks:well.1.0"
},
"locationWGS84": {
"description": "The well's location as GeoJSON FeatureCollection.",
......
......@@ -10,9 +10,6 @@
"description": "The well's original location as AnyCrsFeatureCollection - a structure similar to but distinct from GeoJSON.",
"title": "Original CRS Location",
"$ref": "os:wks:anyCrsFeatureCollection.1.0.json"
},
"allOf": {
"$ref": "#/definition/os:wks:well.1.0"
},
"locationWGS84": {
"description": "The well's location as GeoJSON FeatureCollection.",
......
......@@ -10,9 +10,6 @@
"description": "The well's original location as AnyCrsFeatureCollection - a structure similar to but distinct from GeoJSON.",
"title": "Original CRS Location",
"$ref": "os:wks:anyCrsFeatureCollection.1.0.json"
},
"allOf": {
"$ref": "#/definition/os:wks:well.1.0"
},
"locationWGS84": {
"description": "The well's location as GeoJSON FeatureCollection.",
......
......@@ -9,9 +9,6 @@
"description": "The well's original location as AnyCrsFeatureCollection - a structure similar to but distinct from GeoJSON.",
"title": "Original CRS Location",
"$ref": "os:wks:anyCrsFeatureCollection.1.0"
},
"allOf": {
"$ref": "#/definition/os:wks:well.1.0"
},
"locationWGS84": {
"description": "The well's location as GeoJSON FeatureCollection.",
......
{
"$schema": "http://json-schema.org/draft-07/schema#",
"x-os-lifecycle-state": "published",
"description": "The entity well.",
"title": "Well",
"type": "object",
"properties": {
"person": {
"description": "Information for person",
"title": "Information for person",
"$ref": "#/definitions/person"
},
"locationOriginalCRS": {
"description": "The well's original location as AnyCrsFeatureCollection - a structure similar to but distinct from GeoJSON.",
"title": "Original CRS Location",
"$ref": "os:wks:anyCrsFeatureCollection.1.0"
},
"locationWGS84": {
"description": "The well's location as GeoJSON FeatureCollection.",
"title": "WGS84 Location",
"$ref": "https://geojson.org/schema/FeatureCollection.json",
"example": {
"features": [
{
"geometry": {
"coordinates": [
-92.11569999999999,
29.8823,
153.4779442519685
],
"type": "Point"
},
"type": "Feature",
"properties": {
"name": "Newton 2-31"
}
}
],
"type": "FeatureCollection"
}
}
}
}
......@@ -12,9 +12,6 @@
"$ref": "os:wks:anyCrsFeatureCollection.1.0"
},
"allOf": [
{
"$ref": "#/definition/os:wks:well.1.0"
},
[
{
"$ref": "os:wks:anyCrsFeatureCollection.1.0"
......
......@@ -1062,9 +1062,6 @@
"title": "Original CRS Location",
"$ref": "#/definitions/os:wks:anyCrsFeatureCollection.1.0"
},
"allOf": {
"$ref": "#/definition/os:wks:well.1.0"
},
"locationWGS84": {
"description": "The well's location as GeoJSON FeatureCollection.",
"title": "WGS84 Location",
......
......@@ -1063,9 +1063,6 @@
"$ref": "#/definitions/os:wks:anyCrsFeatureCollection.1.0"
},
"allOf": [
{
"$ref": "#/definition/os:wks:well.1.0"
},
[
{
"$ref": "#/definitions/os:wks:anyCrsFeatureCollection.1.0"
......
......@@ -17,9 +17,18 @@
"description":"Theentitywell.",
"title":"Well",
"type":"object",
"definitions":{
},
"definitions":{
"anyCrsFeatureCollection.1.0": {
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "anyCrsFeatureCollection",
"type": "object"
},
"osdu..wks..well.1.0": {
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "osdu..wks..well",
"type": "object"
}
},
"properties":{
"locationOriginalCRS":{
"description":"Thewell'soriginallocationasAnyCrsFeatureCollection-astructuresimilartobutdistinctfromGeoJSON.",
......@@ -27,7 +36,7 @@
"$ref":"#/definition/anyCrsFeatureCollection.1.0"
},
"allOf":{
"$ref":"#/definition/slb..wks..well.1.0"
"$ref":"#/definition/osdu..wks..well.1.0"
},
"locationWGS84":{
"description":"Thewell'slocationasGeoJSONFeatureCollection.",
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment