Skip to content

Commit

Permalink
OAP-195 json-schema: If the reference to the scheme does not contain …
Browse files Browse the repository at this point in the history
…an extension, then try to load .conf, .yaml or .json
  • Loading branch information
nofateg authored Aug 15, 2024
1 parent 0316c10 commit c2cb6c5
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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<String, Binder> 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<String> extConf = Resources.readStrings( getClass(), prefix + "/" + fileName + ".conf" );
List<String> extJson = Resources.readStrings( getClass(), prefix + "/" + fileName + ".json" );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
}""" );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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"
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
type: object
properties:
a:
type: string
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
</distributionManagement>

<properties>
<oap.project.version>22.4.11</oap.project.version>
<oap.project.version>22.4.12</oap.project.version>

<oap.deps.config.version>21.0.0</oap.deps.config.version>
<oap.deps.oap-teamcity.version>21.0.1</oap.deps.oap-teamcity.version>
Expand Down

0 comments on commit c2cb6c5

Please sign in to comment.