From af9f34ba53de6d210d89670904bd71d4d86ddf4e Mon Sep 17 00:00:00 2001 From: Martin <2026226+martin-mfg@users.noreply.github.com> Date: Sun, 5 Nov 2023 09:16:48 +0100 Subject: [PATCH] fix oneOf enums --- .../openapitools/codegen/DefaultCodegen.java | 26 ++++++++++++------- .../codegen/DefaultCodegenTest.java | 9 +++++++ pom.xml | 2 +- 3 files changed, 27 insertions(+), 10 deletions(-) diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java index 8ae9935e190c..75f6c77f989d 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java @@ -26,6 +26,7 @@ import com.samskivert.mustache.Mustache; import com.samskivert.mustache.Mustache.Compiler; import com.samskivert.mustache.Mustache.Lambda; +import io.swagger.v3.core.util.AnnotationsUtils; import io.swagger.v3.core.util.Json; import io.swagger.v3.oas.models.OpenAPI; import io.swagger.v3.oas.models.Operation; @@ -924,12 +925,7 @@ public String toEnumVarName(String value, String datatype) { } } - /** - * Set the OpenAPI document. - * This method is invoked when the input OpenAPI document has been parsed and validated. - */ - @Override - public void setOpenAPI(OpenAPI openAPI) { + public boolean specVersionGreaterThanOrEqualTo310(OpenAPI openAPI) { String originalSpecVersion; String xOriginalSwaggerVersion = "x-original-swagger-version"; if (openAPI.getExtensions() != null && !openAPI.getExtensions().isEmpty() && openAPI.getExtensions().containsValue(xOriginalSwaggerVersion)) { @@ -939,8 +935,16 @@ public void setOpenAPI(OpenAPI openAPI) { } Integer specMajorVersion = Integer.parseInt(originalSpecVersion.substring(0, 1)); Integer specMinorVersion = Integer.parseInt(originalSpecVersion.substring(2, 3)); - boolean specVersionGreaterThanOrEqualTo310 = (specMajorVersion == 3 && specMinorVersion >= 1); - if (specVersionGreaterThanOrEqualTo310) { + return specMajorVersion == 3 && specMinorVersion >= 1; + } + + /** + * Set the OpenAPI document. + * This method is invoked when the input OpenAPI document has been parsed and validated. + */ + @Override + public void setOpenAPI(OpenAPI openAPI) { + if (specVersionGreaterThanOrEqualTo310(openAPI)) { LOGGER.warn(UNSUPPORTED_V310_SPEC_MSG); } this.openAPI = openAPI; @@ -2894,7 +2898,11 @@ private void mergeProperties(Map existingProperties, Map + existingProperties.put( + key, + AnnotationsUtils.clone(value, specVersionGreaterThanOrEqualTo310(openAPI)) + )); if (null != existingType && null != newType && null != newType.getEnum() && !newType.getEnum().isEmpty()) { for (Object e : newType.getEnum()) { // ensure all interface enum types are added to schema diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/DefaultCodegenTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/DefaultCodegenTest.java index 477cd09986ac..8dbeeaba4ba3 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/DefaultCodegenTest.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/DefaultCodegenTest.java @@ -671,6 +671,15 @@ public void testOneOfEnum() { } Assert.assertTrue(typeSeen); Assert.assertTrue(typeContainsEnums); + + Assert.assertEquals( + ((StringSchema) openAPI.getComponents().getSchemas().get("Dog").getProperties().get("type")).getEnum().size(), + 1 + ); + Assert.assertEquals( + ((StringSchema) openAPI.getComponents().getSchemas().get("Cat").getProperties().get("type")).getEnum().size(), + 1 + ); } @Test diff --git a/pom.xml b/pom.xml index a3b7afdbd544..5b4422b33e11 100644 --- a/pom.xml +++ b/pom.xml @@ -1477,7 +1477,7 @@ 1.7.36 3.1.12.2 io.swagger.parser.v3 - 2.1.14 + 2.1.18 7.5 1.34 3.4.3