diff --git a/oap-formats/oap-json/oap-json-schema/src/main/java/oap/json/schema/ResourceSchemaStorage.java b/oap-formats/oap-json/oap-json-schema/src/main/java/oap/json/schema/ResourceSchemaStorage.java index 715f76ad07..35a1c5a154 100644 --- a/oap-formats/oap-json/oap-json-schema/src/main/java/oap/json/schema/ResourceSchemaStorage.java +++ b/oap-formats/oap-json/oap-json-schema/src/main/java/oap/json/schema/ResourceSchemaStorage.java @@ -28,12 +28,15 @@ import oap.io.content.ContentReader; import oap.json.Binder; import oap.util.Lists; +import oap.util.Pair; import org.apache.commons.io.FilenameUtils; import java.util.ArrayList; import java.util.List; import java.util.Map; +import static oap.util.Pair.__; + public final class ResourceSchemaStorage implements SchemaStorage { public static final SchemaStorage INSTANCE = new ResourceSchemaStorage(); @@ -42,14 +45,18 @@ private ResourceSchemaStorage() { @Override public String get( String name ) { - String ext = FilenameUtils.getExtension( name ); String prefix = FilenameUtils.removeExtension( name ); String fileName = FilenameUtils.removeExtension( FilenameUtils.getName( name ) ); - String conf = Resources.readOrThrow( getClass(), name, ContentReader.ofString() ); - if( "yaml".equalsIgnoreCase( ext ) ) { - conf = Binder.json.marshal( Binder.yaml.unmarshal( Map.class, conf ) ); - } + Pair origConf = + Resources.read( getClass(), name + ".conf", ContentReader.ofString() ).map( str -> __( str, Binder.hoconWithoutSystemProperties ) ) + .or( () -> Resources.read( getClass(), name + ".yaml", ContentReader.ofString() ).map( str -> __( str, Binder.yaml ) ) ) + .or( () -> Resources.read( getClass(), name + ".json", ContentReader.ofString() ).map( str -> __( str, Binder.json ) ) ) + + .or( () -> Resources.read( getClass(), name, ContentReader.ofString() ).map( str -> __( str, Binder.Format.of( name, false ).binder ) ) ) + .orElseThrow( () -> new JsonSchemaException( "resource not found " + name + "[|.conf|.json|.yaml] for context class " + getClass() ) ); + + String conf = Binder.json.marshal( origConf._2.unmarshal( Map.class, origConf._1 ) ); List extConf = Resources.readStrings( getClass(), prefix + "/" + fileName + ".conf" ); List extJson = Resources.readStrings( getClass(), prefix + "/" + fileName + ".json" ); diff --git a/oap-formats/oap-json/oap-json-schema/src/test/java/oap/json/schema/ResourceSchemaStorageTest.java b/oap-formats/oap-json/oap-json-schema/src/test/java/oap/json/schema/ResourceSchemaStorageTest.java index b83c39c021..437b382558 100644 --- a/oap-formats/oap-json/oap-json-schema/src/test/java/oap/json/schema/ResourceSchemaStorageTest.java +++ b/oap-formats/oap-json/oap-json-schema/src/test/java/oap/json/schema/ResourceSchemaStorageTest.java @@ -33,41 +33,48 @@ public class ResourceSchemaStorageTest { @Test - public void get() { - var schema = ResourceSchemaStorage.INSTANCE.get( "/schema/test-schema.conf" ); + public void testGet() { + String schema = ResourceSchemaStorage.INSTANCE.get( "/schema/test-schema.conf" ); + String schema2 = ResourceSchemaStorage.INSTANCE.get( "/schema/test-schema" ); + String schema3 = ResourceSchemaStorage.INSTANCE.get( "/schema/test-schema2" ); - assertJson( Binder.json.marshal( Binder.hoconWithoutSystemProperties.unmarshal( Map.class, schema ) ) ) - .isEqualTo( "{" - + "\"type\": \"object\"," - + " \"properties\": {" - + " \"a\": {" - + " \"type\": \"string\"" - + " }" - + " }" - + "}" ); + String expected = """ + { + "type": "object", + "properties": { + "a": { + "type": "string" + } + } + }"""; + + assertJson( Binder.json.marshal( Binder.hoconWithoutSystemProperties.unmarshal( Map.class, schema ) ) ).isEqualTo( expected ); + assertJson( Binder.json.marshal( Binder.hoconWithoutSystemProperties.unmarshal( Map.class, schema2 ) ) ).isEqualTo( expected ); + assertJson( Binder.json.marshal( Binder.hoconWithoutSystemProperties.unmarshal( Map.class, schema3 ) ) ).isEqualTo( expected ); } @Test - public void getWithExtends() { - var schema = ResourceSchemaStorage.INSTANCE.get( "/schema/test-schema-1.conf" ); + public void testGetWithExtends() { + String schema = ResourceSchemaStorage.INSTANCE.get( "/schema/test-schema-1" ); assertJson( Binder.json.marshal( Binder.hoconWithoutSystemProperties.unmarshal( Map.class, schema ) ) ) - .isEqualTo( "{" - + " \"type\": \"object\"," - + " \"properties\": {" - + " \"a\": {" - + " \"type\": \"string\"" - + " }," - + " \"b\": {" - + " \"type\": \"integer\"" - + " }," - + " \"j\": {" - + " \"type\": \"integer\"" - + " }," - + " \"y\": {" - + " \"type\": \"integer\"" - + " }" - + " }" - + "}" ); + .isEqualTo( """ + { + "type": "object", + "properties": { + "a": { + "type": "string" + }, + "b": { + "type": "integer" + }, + "j": { + "type": "integer" + }, + "y": { + "type": "integer" + } + } + }""" ); } } diff --git a/oap-formats/oap-json/oap-json-schema/src/test/java/oap/json/schema/SchemaRefTest.java b/oap-formats/oap-json/oap-json-schema/src/test/java/oap/json/schema/SchemaRefTest.java index 0ed7c6d9a6..e6f14a8b3f 100644 --- a/oap-formats/oap-json/oap-json-schema/src/test/java/oap/json/schema/SchemaRefTest.java +++ b/oap-formats/oap-json/oap-json-schema/src/test/java/oap/json/schema/SchemaRefTest.java @@ -49,9 +49,41 @@ public void extendsSchema() { } }"""; - assertOk( schema, "{'field1': {'a': 'test'}}", url -> schema2, false ); + assertOk( schema, "{'field1': {'a': 'test'}}", _ -> schema2, false ); assertFailure( schema, "{'field1': {'a': 1}}", _ -> schema2, "/field1/a: instance type is number, but allowed type is string" ); } + + @Test + public void extendsSchemaArray() { + String schema = """ + { + type = object + additionalProperties = false + properties { + list { + type = array + additionalProperties = false + items { "$ref" = "/schema/test2" } + } + } + }"""; + + String schema2 = """ + { + type = object + additionalProperties = false + properties { + a { + type = string + } + } + }"""; + + assertOk( schema, "{'list': [{'a': 'test'}]}", _ -> schema2, false ); + assertFailure( schema, "{'list': [{'a': 1}]}", + _ -> schema2, "/list/0/a: instance type is number, but allowed type is string" + ); + } } diff --git a/oap-formats/oap-json/oap-json-schema/src/test/resources/schema/test-schema2.yaml b/oap-formats/oap-json/oap-json-schema/src/test/resources/schema/test-schema2.yaml new file mode 100644 index 0000000000..fa630d8663 --- /dev/null +++ b/oap-formats/oap-json/oap-json-schema/src/test/resources/schema/test-schema2.yaml @@ -0,0 +1,4 @@ +type: object +properties: + a: + type: string diff --git a/pom.xml b/pom.xml index be10e00e88..7970ee39fc 100644 --- a/pom.xml +++ b/pom.xml @@ -70,7 +70,7 @@ - 22.4.11 + 22.4.12 21.0.0 21.0.1