diff --git a/src/main/java/edu/isi/oba/MapperObjectProperty.java b/src/main/java/edu/isi/oba/MapperObjectProperty.java index 264aeec..52bbaf5 100644 --- a/src/main/java/edu/isi/oba/MapperObjectProperty.java +++ b/src/main/java/edu/isi/oba/MapperObjectProperty.java @@ -25,7 +25,7 @@ public MapperObjectProperty(String name, String description, Boolean isFunctiona this.ref = ref; this.array = true; this.nullable = true; - this.isFunctional=isFunctional; + this.isFunctional = isFunctional; this.restrictions = restrictions; } @@ -35,7 +35,7 @@ public MapperObjectProperty(String name, String description, Boolean isFunction this.ref = ref; this.array = array; this.nullable = nullable; - this.isFunctional=isFunctional; + this.isFunctional = isFunctional; this.restrictions = restrictions; } @@ -51,24 +51,52 @@ public Schema getSchemaByObjectProperty() { } } - private Schema getObjectPropertiesByRef(String ref, boolean array, boolean nullable) { - ArraySchema objects = new ArraySchema(); - objects.setDescription(this.description); - - if (array) { + private Schema getObjectPropertiesByRef(String ref, boolean isArray, boolean isNullable) { + if (this.restrictions.isEmpty()) { Schema object = new ObjectSchema(); - object.setDescription(this.description); object.set$ref(ref); - if (this.isFunctional) { + if (isArray) { + ArraySchema objects = new ArraySchema(); + objects.setDescription(this.description); + objects.setNullable(isNullable); + objects.setItems(object); + + if (this.isFunctional) { objects.setMaxItems(1); + } + + return objects; + } else { + object.setDescription(this.description); + object.setNullable(isNullable); + object.setType("object"); + + if (this.isFunctional) { + object.setMaxItems(1); + } + + return object; } - - for (String restriction: this.restrictions.keySet()) { - String value = this.restrictions.get(restriction); - switch (restriction) { + } + + ArraySchema objects = new ArraySchema(); + objects.setType("array"); + + // For OpenAPI v3.0, "$ref" cannot be used as a sibling with "default". + // see: https://swagger.io/docs/specification/using-ref/ + // see: https://stackoverflow.com/a/77189463 + // A workaround is to include both within "items" and as separate entries under "allOf". + // TODO: version check here (and elsewhere?) to differentiate the schema structure. For v3.1+, it can support "$ref" and "default" as siblings. + ArraySchema allOfItems = new ArraySchema(); + allOfItems.setType(null); + + for (String restriction: this.restrictions.keySet()) { + String value = this.restrictions.get(restriction); + + switch (restriction) { case "maxCardinality": - objects.setMaxItems(Integer.parseInt(value)); + objects.setMaxItems(Integer.parseInt(value)); break; case "minCardinality": objects.setMinItems(Integer.parseInt(value)); @@ -78,56 +106,45 @@ private Schema getObjectPropertiesByRef(String ref, boolean array, boolean nulla objects.setMinItems(Integer.parseInt(value)); break; case "someValuesFrom": - nullable=false; + isNullable = false; break; case "allValuesFrom": //nothing to do in the Schema break; - default: + case "objectHasReference": { + Schema object = new ObjectSchema(); + object.set$ref(value); + object.setType(null); + allOfItems.addAllOfItem(object); break; } - } - objects.setNullable(nullable); - objects.setItems(object); - return objects; - } else { - if (this.restrictions.isEmpty()) { - Schema object = new ObjectSchema(); - object.setDescription(this.description); - object.setType("object"); - return object; - } else { - // For OpenAPI v3.0, "$ref" cannot be used as a sibling with "default". - // see: https://swagger.io/docs/specification/using-ref/ - // see: https://stackoverflow.com/a/77189463 - // A workaround is to include both within "items" and as separate entries under "allOf". - // TODO: version check here (and elsewhere?) to differentiate the schema structure. For v3.1+, it can support "$ref" and "default" as siblings. - ArraySchema tempObjects = new ArraySchema(); - tempObjects.setType(null); - - for (String restriction: this.restrictions.keySet()) { + case "objectHasValue": { Schema object = new ObjectSchema(); - String value = this.restrictions.get(restriction); - - if ("objectHasReference".equals(restriction)) { - object.set$ref(value); - object.setType(null); - } - - if ("objectHasValue".equals(restriction)) { - object.setDefault(value); - object.setType("string"); - } - - tempObjects.addAllOfItem(object); + object.setDefault(value); + object.setType("string"); + allOfItems.addAllOfItem(object); + break; } + default: + break; + } + } - objects.items(tempObjects); - objects.setType("array"); + if (allOfItems.getAllOf() == null || allOfItems.getAllOf().isEmpty()) { + Schema object = new ObjectSchema(); + object.set$ref(ref); + objects.items(object); + } else { + objects.items(allOfItems); + } - return objects; - } + if (this.isFunctional) { + objects.setMaxItems(1); } + + objects.setNullable(isNullable); + + return objects; } private Schema getComposedSchemaObject(List refs, boolean array, boolean nullable) { @@ -195,7 +212,7 @@ private Schema getComposedSchemaObject(List refs, boolean array, boolean //if the property range is complex it will be omitted logger.warning("omitted complex restriction"); objects.setItems(object); - } + } } }