From 2862cc202ac55acf5a78377a978a674edf9bb565 Mon Sep 17 00:00:00 2001 From: gcornacchia Date: Tue, 3 Dec 2024 18:30:06 +0100 Subject: [PATCH] fix navigazione composed model --- .../impl/DraftV4JsonSchemaGenerator.java | 17 +++++++++ .../updatepetresponse200.json | 37 +++++++++++++++++-- .../petstoreoas3ArrayAllofInline.json | 16 +++++--- 3 files changed, 61 insertions(+), 9 deletions(-) diff --git a/src/main/java/it/imolainformatica/openapi2jsonschema4j/impl/DraftV4JsonSchemaGenerator.java b/src/main/java/it/imolainformatica/openapi2jsonschema4j/impl/DraftV4JsonSchemaGenerator.java index cafd449..3138170 100644 --- a/src/main/java/it/imolainformatica/openapi2jsonschema4j/impl/DraftV4JsonSchemaGenerator.java +++ b/src/main/java/it/imolainformatica/openapi2jsonschema4j/impl/DraftV4JsonSchemaGenerator.java @@ -177,6 +177,14 @@ private void navigateModel(String originalRef, List usedDefinition, Map< Schema s = (Schema) res.get(ITEMS); if (s.get$ref()!=null) { navigateModel(s.get$ref(), usedDefinition, res, null); + } else if (s instanceof ComposedSchema) { + ComposedSchema cm = (ComposedSchema)s; + lookComposedModel(cm.getAllOf(),usedDefinition,res); + lookComposedModel(cm.getAnyOf(),usedDefinition,res); + lookComposedModel(cm.getOneOf(),usedDefinition,res); + if (cm.getNot()!=null) { + navigateModel(cm.getNot().get$ref(), usedDefinition, res, null); + } } } } else if (ob instanceof ComposedSchema) { @@ -239,6 +247,15 @@ private void navigateSchema(String propertyName, Schema p, List usedDefi if (mp.getAdditionalProperties() instanceof Schema ) { navigateSchema(mp.getName(), (Schema)mp.getAdditionalProperties(), usedDefinition, res); } + } else if (p instanceof ComposedSchema){ + ComposedSchema cm = (ComposedSchema)p; + lookComposedModel(cm.getAllOf(),usedDefinition,res); + lookComposedModel(cm.getAnyOf(),usedDefinition,res); + lookComposedModel(cm.getOneOf(),usedDefinition,res); + if (cm.getNot()!=null) { + navigateModel(cm.getNot().get$ref(), usedDefinition, res, null); + } + } else { log.debug(p.getClass() + " - nothing to do!"); } diff --git a/src/test/resources/expectedJsonSchemas/petstoreoas3ArrayAllofInline.json/updatepetresponse200.json b/src/test/resources/expectedJsonSchemas/petstoreoas3ArrayAllofInline.json/updatepetresponse200.json index ed5d00c..e629473 100644 --- a/src/test/resources/expectedJsonSchemas/petstoreoas3ArrayAllofInline.json/updatepetresponse200.json +++ b/src/test/resources/expectedJsonSchemas/petstoreoas3ArrayAllofInline.json/updatepetresponse200.json @@ -1,9 +1,40 @@ { "components" : { - "schemas" : { } + "schemas" : { + "One" : { + "type" : "object", + "properties" : { + "property1" : { + "type" : "string" + } + }, + "additionalProperties" : false + }, + "Two" : { + "type" : "object", + "properties" : { + "property2" : { + "type" : "string" + } + }, + "additionalProperties" : false + } + } }, "$schema" : "http://json-schema.org/draft-04/schema#", + "additionalProperties" : false, "title" : "updatePetresponse200", - "type" : "array", - "items" : { } + "type" : "object", + "properties" : { + "id" : { + "type" : "array", + "items" : { + "oneOf" : [ { + "$ref" : "#/components/schemas/One" + }, { + "$ref" : "#/components/schemas/Two" + } ] + } + } + } } \ No newline at end of file diff --git a/src/test/resources/petstoreoas3ArrayAllofInline.json b/src/test/resources/petstoreoas3ArrayAllofInline.json index 7fa55f1..15cfa5b 100644 --- a/src/test/resources/petstoreoas3ArrayAllofInline.json +++ b/src/test/resources/petstoreoas3ArrayAllofInline.json @@ -146,12 +146,16 @@ "content": { "application/json": { "schema": { - "type": "array", - "items": { - "oneOf": [ - {"$ref": "#/components/schemas/One"}, - {"$ref": "#/components/schemas/Two"} - ] + "properties": { + "id": { + "type": "array", + "items": { + "oneOf": [ + {"$ref": "#/components/schemas/One"}, + {"$ref": "#/components/schemas/Two"} + ] + } + } } } }