Commit 732998bb authored by Orsu Akhil's avatar Orsu Akhil Committed by ethiraj krishnamanaidu
Browse files

Fix related to schema resolve logic

parent c325c74d
......@@ -11,6 +11,7 @@ import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.opengroup.osdu.core.common.logging.JaxRsDpsLog;
......@@ -95,11 +96,25 @@ public class SchemaResolver {
jsonObject.put(key, value);
} else if (jsonObject.get(key) instanceof JSONObject) {
findAndResolveRef((JSONObject) jsonObject.get(key), refSchemas);
} else if (jsonObject.get(key) instanceof JSONArray) {
findAndResolveRef(jsonObject.getJSONArray(key), refSchemas);
}
}
return jsonObject;
}
private JSONArray findAndResolveRef(JSONArray jsonArray, Map<String, String> refSchemas)
throws BadRequestException, ApplicationException {
for (Object object : jsonArray) {
if (object instanceof JSONObject) {
findAndResolveRef((JSONObject) object, refSchemas);
} else if (object instanceof JSONArray) {
findAndResolveRef((JSONArray) object, refSchemas);
}
}
return jsonArray;
}
private String resolveRef(Object object, Map<String, String> refSchemas)
throws BadRequestException, ApplicationException {
String value = String.valueOf(object);
......@@ -180,4 +195,4 @@ public class SchemaResolver {
}
return true;
}
}
\ No newline at end of file
}
......@@ -55,6 +55,16 @@ public class SchemaResolverTest {
JSONAssert.assertEquals(resolvedSchema, schemaResolver.resolveSchema(orginalSchema), JSONCompareMode.LENIENT);
}
@Test
public void testResolveSchema_ResolveRefForAttributeInJSONArray()
throws JSONException, BadRequestException, ApplicationException, NotFoundException, IOException {
String resolvedSchema = new FileUtils().read("/test_schema/resolvedSchema_Ref_AttributeInJsonArray.json");
String orginalSchema = new FileUtils().read("/test_schema/originalSchemaWithRef_AttributeInJsonArray.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(orginalSchema), JSONCompareMode.LENIENT);
}
@Test
public void testResolveSchema_no_definitionblock()
throws JSONException, BadRequestException, ApplicationException, NotFoundException, IOException {
......
{
"$schema": "http://json-schema.org/draft-07/schema#",
"x-os-lifecycle-state": "published",
"description": "The entity well.",
"title": "Well",
"type": "object",
"definitions": {},
"properties": {
"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"
},
"allOf": [
{
"$ref": "#/definition/os:wks:well.1.0"
},
[
{
"$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"
}
}
}
}
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