From dd3ab0a1fab6ca55defa8811f7850fb9e29b7e8e Mon Sep 17 00:00:00 2001 From: William Cheng Date: Tue, 13 Feb 2024 16:54:40 +0800 Subject: [PATCH] Fix attributes in allOf and $ref (#17836) * fix allOf and ref properties * add tests --- .../openapitools/codegen/DefaultCodegen.java | 20 ++++++++++++++++ .../kotlin/KotlinServerCodegenTest.java | 23 ++++++++++++++++++ .../src/test/resources/3_1/issue_17726.yaml | 24 +++++++++++++++++++ 3 files changed, 67 insertions(+) create mode 100644 modules/openapi-generator/src/test/resources/3_1/issue_17726.yaml 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 6de52cc99a3..26f806fb9be 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 @@ -3979,6 +3979,8 @@ public class DefaultCodegen implements CodegenConfig { } else { LOGGER.error("Unknown type in allOf schema. Please report the issue via openapi-generator's Github issue tracker."); } + } else if (p.get$ref() != null) { // it's a ref + original = p; } CodegenProperty property = CodegenModelFactory.newInstance(CodegenModelType.PROPERTY); @@ -4214,6 +4216,24 @@ public class DefaultCodegen implements CodegenConfig { if (original.getDescription() != null) { property.description = p.getDescription(); } + if (original.getMaxLength() != null) { + property.setMaxLength(original.getMaxLength()); + } + if (original.getMinLength() != null) { + property.setMinLength(original.getMinLength()); + } + if (original.getMaxItems() != null) { + property.setMaxItems(original.getMaxItems()); + } + if (original.getMinItems() != null) { + property.setMinItems(original.getMinItems()); + } + if (original.getMaximum() != null) { + property.setMaximum(String.valueOf(original.getMaximum().doubleValue())); + } + if (original.getMinimum() != null) { + property.setMinimum(String.valueOf(original.getMinimum().doubleValue())); + } } // set the default value diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/kotlin/KotlinServerCodegenTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/kotlin/KotlinServerCodegenTest.java index 3e89a210cd9..185785ee08e 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/kotlin/KotlinServerCodegenTest.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/kotlin/KotlinServerCodegenTest.java @@ -5,6 +5,7 @@ import org.openapitools.codegen.ClientOptInput; import org.openapitools.codegen.DefaultGenerator; import org.openapitools.codegen.TestUtils; import org.openapitools.codegen.languages.KotlinServerCodegen; +import org.openapitools.codegen.languages.KotlinSpringServerCodegen; import java.io.File; import java.io.IOException; @@ -164,4 +165,26 @@ public class KotlinServerCodegenTest { "import javax.validation.Valid" ); } + + // to test attributes in the $ref (OpenAPI 3.1 spec) + @Test + public void attributesInRef() throws IOException { + File output = Files.createTempDirectory("test_attributes").toFile().getCanonicalFile(); + output.deleteOnExit(); + + KotlinSpringServerCodegen codegen = new KotlinSpringServerCodegen(); + codegen.setOutputDir(output.getAbsolutePath()); + + new DefaultGenerator().opts(new ClientOptInput() + .openAPI(TestUtils.parseSpec("src/test/resources/3_1/issue_17726.yaml")) + .config(codegen)) + .generate(); + + String outputPath = output.getAbsolutePath() + "src/main/kotlin/org/openapitools"; + Path order = Paths.get(outputPath + "/model/Order.kt"); + assertFileContains( + order, + "@get:Size(max=50)" + ); + } } diff --git a/modules/openapi-generator/src/test/resources/3_1/issue_17726.yaml b/modules/openapi-generator/src/test/resources/3_1/issue_17726.yaml new file mode 100644 index 00000000000..cd2dea2580e --- /dev/null +++ b/modules/openapi-generator/src/test/resources/3_1/issue_17726.yaml @@ -0,0 +1,24 @@ +openapi: 3.1.0 +info: + title: dummy + version: "1.0" +paths: + /users/{id}: + summary: Represents a user +components: + schemas: + Order: + type: object + required: + - name + properties: + name: + type: string + address: + $ref: '#/components/schemas/Address' + maxLength: 50 + Address: + type: object + properties: + firstName: + type: string \ No newline at end of file