Skip to content
Snippets Groups Projects
Commit 13ca3d3f authored by Sviatoslav Nekhaienko's avatar Sviatoslav Nekhaienko
Browse files

add oneOf and anyOf

parent 24f5206c
No related branches found
No related tags found
1 merge request!78Support of oneOf and anyOf tags
Pipeline #23860 failed
Showing
with 236 additions and 8 deletions
...@@ -56,8 +56,10 @@ public class PropertiesProcessor { ...@@ -56,8 +56,10 @@ public class PropertiesProcessor {
public Stream<Map<String, Object>> processItem(AllOfItem allOfItem) { public Stream<Map<String, Object>> processItem(AllOfItem allOfItem) {
Preconditions.checkNotNull(allOfItem, "allOfItem cannot be null"); Preconditions.checkNotNull(allOfItem, "allOfItem cannot be null");
if (Objects.nonNull(allOfItem.getAllOf())) { Stream<Map<String, Object>> ofItems = processOfItems(allOfItem.getAllOf(), allOfItem.getAnyOf(), allOfItem.getOneOf());
return allOfItem.getAllOf().stream().flatMap(this::processItem);
if (Objects.nonNull(ofItems)) {
return ofItems;
} }
String ref = allOfItem.getRef(); String ref = allOfItem.getRef();
...@@ -89,13 +91,34 @@ public class PropertiesProcessor { ...@@ -89,13 +91,34 @@ public class PropertiesProcessor {
new AppException(HttpStatus.SC_NOT_FOUND, "Failed to find definition:" + definitionSubRef, new AppException(HttpStatus.SC_NOT_FOUND, "Failed to find definition:" + definitionSubRef,
"Unknown definition:" + definitionSubRef)); "Unknown definition:" + definitionSubRef));
if (Objects.nonNull(definition.getAllOf())) { Stream<Map<String, Object>> ofItems =
return definition.getAllOf().stream().flatMap(this::processItem); processOfItems(definition.getAllOf(), definition.getAnyOf(), definition.getOneOf());
if (Objects.nonNull(ofItems)) {
return ofItems;
} }
return processProperties(definition.getProperties()); return processProperties(definition.getProperties());
} }
private Stream<Map<String, Object>> processOfItems(List<AllOfItem> allOf, List<AllOfItem> anyOf, List<AllOfItem> oneOf) {
Stream<Map<String, Object>> ofItems = null;
if (Objects.nonNull(allOf)) {
ofItems = allOf.stream().flatMap(this::processItem);
}
if (Objects.nonNull(anyOf)) {
ofItems = Stream.concat(Optional.ofNullable(ofItems).orElseGet(Stream::empty), anyOf.stream().flatMap(this::processItem));
}
if (Objects.nonNull(oneOf)) {
ofItems = Stream.concat(Optional.ofNullable(ofItems).orElseGet(Stream::empty), oneOf.stream().flatMap(this::processItem));
}
return ofItems;
}
public Stream<Map<String, Object>> processProperties(Map<String, TypeProperty> properties){ public Stream<Map<String, Object>> processProperties(Map<String, TypeProperty> properties){
return properties.entrySet().stream().flatMap(this::processPropertyEntry); return properties.entrySet().stream().flatMap(this::processPropertyEntry);
} }
...@@ -118,11 +141,10 @@ public class PropertiesProcessor { ...@@ -118,11 +141,10 @@ public class PropertiesProcessor {
return Stream.empty(); return Stream.empty();
} }
if (Objects.nonNull(entry.getValue().getAllOf())) { Stream<Map<String, Object>> ofItems = processOfItems(entry);
PropertiesProcessor propertiesProcessor = new PropertiesProcessor(definitions, pathPrefixWithDot + entry.getKey()
, log, new SchemaConverterPropertiesConfig());
return entry.getValue().getAllOf().stream().flatMap(propertiesProcessor::processItem); if (Objects.nonNull(ofItems)) {
return ofItems;
} }
if (Objects.nonNull(entry.getValue().getProperties())) { if (Objects.nonNull(entry.getValue().getProperties())) {
...@@ -139,6 +161,35 @@ public class PropertiesProcessor { ...@@ -139,6 +161,35 @@ public class PropertiesProcessor {
return storageSchemaEntry(getTypeByDefinitionProperty(entry.getValue()), pathPrefixWithDot + entry.getKey()); return storageSchemaEntry(getTypeByDefinitionProperty(entry.getValue()), pathPrefixWithDot + entry.getKey());
} }
private Stream<Map<String, Object>> processOfItems(Map.Entry<String, TypeProperty> entry) {
Stream<Map<String, Object>> ofItems = null;
if (Objects.nonNull(entry.getValue().getAllOf())) {
PropertiesProcessor propertiesProcessor = new PropertiesProcessor(definitions, pathPrefixWithDot + entry.getKey()
, log, new SchemaConverterPropertiesConfig());
ofItems = entry.getValue().getAllOf().stream().flatMap(propertiesProcessor::processItem);
}
if (Objects.nonNull(entry.getValue().getAnyOf())) {
PropertiesProcessor propertiesProcessor = new PropertiesProcessor(definitions, pathPrefixWithDot + entry.getKey()
, log, new SchemaConverterPropertiesConfig());
ofItems = Stream.concat(Optional.ofNullable(ofItems).orElseGet(Stream::empty),
entry.getValue().getAnyOf().stream().flatMap(propertiesProcessor::processItem));
}
if (Objects.nonNull(entry.getValue().getOneOf())) {
PropertiesProcessor propertiesProcessor = new PropertiesProcessor(definitions, pathPrefixWithDot + entry.getKey()
, log, new SchemaConverterPropertiesConfig());
ofItems = Stream.concat(Optional.ofNullable(ofItems).orElseGet(Stream::empty),
entry.getValue().getOneOf().stream().flatMap(propertiesProcessor::processItem));
}
return ofItems;
}
private Stream<Map<String, Object>> storageSchemaEntry(String kind, String path) { private Stream<Map<String, Object>> storageSchemaEntry(String kind, String path) {
Preconditions.checkNotNullOrEmpty(kind, "kind cannot be null or empty"); Preconditions.checkNotNullOrEmpty(kind, "kind cannot be null or empty");
Preconditions.checkNotNullOrEmpty(path, "path cannot be null or empty"); Preconditions.checkNotNullOrEmpty(path, "path cannot be null or empty");
......
...@@ -27,4 +27,6 @@ public class AllOfItem { ...@@ -27,4 +27,6 @@ public class AllOfItem {
private String type; private String type;
private Map<String, TypeProperty> properties; private Map<String, TypeProperty> properties;
private List<AllOfItem> allOf; private List<AllOfItem> allOf;
private List<AllOfItem> oneOf;
private List<AllOfItem> anyOf;
} }
\ No newline at end of file
...@@ -23,4 +23,6 @@ import java.util.Map; ...@@ -23,4 +23,6 @@ import java.util.Map;
public class Definition { public class Definition {
private Map<String, TypeProperty> properties; private Map<String, TypeProperty> properties;
private List<AllOfItem> allOf; private List<AllOfItem> allOf;
private List<AllOfItem> oneOf;
private List<AllOfItem> anyOf;
} }
...@@ -23,6 +23,8 @@ import java.util.Map; ...@@ -23,6 +23,8 @@ import java.util.Map;
@Data @Data
public class PropertiesData { public class PropertiesData {
private List<AllOfItem> allOf; private List<AllOfItem> allOf;
private List<AllOfItem> oneOf;
private List<AllOfItem> anyOf;
@JsonProperty("$ref") @JsonProperty("$ref")
private String ref; private String ref;
private Map<String, TypeProperty> properties; private Map<String, TypeProperty> properties;
......
...@@ -30,4 +30,6 @@ public class TypeProperty { ...@@ -30,4 +30,6 @@ public class TypeProperty {
private Items items; private Items items;
private Map<String, TypeProperty> properties; private Map<String, TypeProperty> properties;
private List<AllOfItem> allOf; private List<AllOfItem> allOf;
private List<AllOfItem> oneOf;
private List<AllOfItem> anyOf;
} }
...@@ -94,6 +94,21 @@ public class SchemaToStorageFormatImplTest { ...@@ -94,6 +94,21 @@ public class SchemaToStorageFormatImplTest {
testSingleFile("/converter/tags/allOf/indefinitions.json", KIND); testSingleFile("/converter/tags/allOf/indefinitions.json", KIND);
} }
@Test
public void oneOfInDefinitions() {
testSingleFile("/converter/tags/oneOf/indefinitions.json", KIND);
}
@Test
public void anyOfInDefinitions() {
testSingleFile("/converter/tags/anyOf/indefinitions.json", KIND);
}
@Test
public void mixAllAnyOneOf() {
testSingleFile("/converter/tags/mixAllAnyOneOf/mix.json", KIND);
}
@Test @Test
public void folderPassed() throws URISyntaxException, IOException { public void folderPassed() throws URISyntaxException, IOException {
......
{
"definitions": {
"wellboreData1": {
"properties": {
"prop1": {
"type": "string"
}
}
},
"wellboreData2": {
"anyOf": [
{
"$ref": "#/definitions/wellboreData1"
}
]
}
},
"properties": {
"data": {
"type": "object",
"properties": {
"Field": {
"$ref": "#/definitions/wellboreData2"
}
}
}
}
}
{
"kind": "KIND_VAL",
"schema": [
{
"kind": "string",
"path": "Field.prop1"
}
]
}
\ No newline at end of file
{
"definitions": {
"wellboreData1": {
"properties": {
"prop1": {
"type": "string"
}
}
},
"wellboreData2": {
"properties": {
"prop2": {
"type": "string"
}
}
},
"wellboreData3": {
"properties": {
"prop3": {
"type": "string"
}
}
},
"wellboreData4": {
"properties": {
"prop4": {
"type": "string"
}
}
}
},
"properties": {
"data": {
"allOf": [
{
"anyOf": [
{
"$ref": "#/definitions/wellboreData1"
} ],
"oneOf": [
{
"$ref": "#/definitions/wellboreData2"
}
]
},
{
"$ref": "#/definitions/wellboreData3"
},
{
"$ref": "#/definitions/wellboreData4"
}
]
}
}
}
{
"kind": "KIND_VAL",
"schema": [
{
"kind": "string",
"path": "prop1"
},
{
"kind": "string",
"path": "prop2"
},
{
"kind": "string",
"path": "prop3"
},
{
"kind": "string",
"path": "prop4"
}
]
}
\ No newline at end of file
{
"definitions": {
"wellboreData1": {
"properties": {
"prop1": {
"type": "string"
}
}
},
"wellboreData2": {
"anyOf": [
{
"$ref": "#/definitions/wellboreData1"
}
]
}
},
"properties": {
"data": {
"type": "object",
"properties": {
"Field": {
"$ref": "#/definitions/wellboreData2"
}
}
}
}
}
{
"kind": "KIND_VAL",
"schema": [
{
"kind": "string",
"path": "Field.prop1"
}
]
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment