Skip to content
GitLab
Menu
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
Open Subsurface Data Universe Software
Platform
System
Schema
Commits
6839a055
Commit
6839a055
authored
May 14, 2021
by
Devesh
Browse files
add check for invalid ref
parent
4de76a9a
Pipeline
#39793
failed with stages
in 2 minutes and 54 seconds
Changes
14
Pipelines
1
Expand all
Hide whitespace changes
Inline
Side-by-side
schema-core/src/main/java/org/opengroup/osdu/schema/util/SchemaResolver.java
View file @
6839a055
...
...
@@ -59,13 +59,19 @@ public class SchemaResolver {
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
);
if
(
entry
.
getValue
()
==
null
)
{
if
(!
definitionMap
.
containsKey
(
entry
.
getKey
()))
{
throw
new
BadRequestException
(
String
.
format
(
"Invalid input, %s no definition but provided as 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
);
...
...
schema-core/src/test/java/org/opengroup/osdu/schema/util/SchemaResolverTest.java
View file @
6839a055
...
...
@@ -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, person no definition but provided as 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
schema-core/src/test/resources/test_schema/originalSchema.json
View file @
6839a055
...
...
@@ -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-core/src/test/resources/test_schema/originalSchemaRefWithDefinition.json
0 → 100644
View file @
6839a055
{
"$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"
}
}
}
}
schema-core/src/test/resources/test_schema/originalSchemaWithInvalidExternalPath.json
View file @
6839a055
...
...
@@ -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."
,
...
...
schema-core/src/test/resources/test_schema/originalSchemaWithInvalidExternalPath2.json
View file @
6839a055
...
...
@@ -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."
,
...
...
schema-core/src/test/resources/test_schema/originalSchemaWithInvalidExternalPath3.json
View file @
6839a055
...
...
@@ -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."
,
...
...
schema-core/src/test/resources/test_schema/originalSchemaWithNoDefinitionBlock.json
View file @
6839a055
...
...
@@ -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-core/src/test/resources/test_schema/originalSchemaWithRefButNoDefinition.json
0 → 100644
View file @
6839a055
{
"$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"
}
}
}
}
schema-core/src/test/resources/test_schema/originalSchemaWithRef_AttributeInJsonArray.json
View file @
6839a055
...
...
@@ -12,9 +12,6 @@
"$ref"
:
"os:wks:anyCrsFeatureCollection.1.0"
},
"allOf"
:
[
{
"$ref"
:
"#/definition/os:wks:well.1.0"
},
[
{
"$ref"
:
"os:wks:anyCrsFeatureCollection.1.0"
...
...
schema-core/src/test/resources/test_schema/resolvedSchema.json
View file @
6839a055
...
...
@@ -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"
,
...
...
schema-core/src/test/resources/test_schema/resolvedSchemaWithDefinition.json
0 → 100644
View file @
6839a055
This diff is collapsed.
Click to expand it.
schema-core/src/test/resources/test_schema/resolvedSchema_Ref_AttributeInJsonArray.json
View file @
6839a055
...
...
@@ -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"
...
...
testing/schema-test-core/src/test/resources/input_payloads/postSchemaServiceWithRef_positiveScenario.json
View file @
6839a055
...
...
@@ -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"
},
"slb..wks..well.1.0"
:
{
"$schema"
:
"http://json-schema.org/draft-07/schema#"
,
"title"
:
"slb..wks..well"
,
"type"
:
"object"
}
},
"properties"
:{
"locationOriginalCRS"
:{
"description"
:
"Thewell'soriginallocationasAnyCrsFeatureCollection-astructuresimilartobutdistinctfromGeoJSON."
,
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment