diff --git a/openapi-generator/src/main/java/io/micronaut/openapi/generator/AbstractMicronautJavaCodegen.java b/openapi-generator/src/main/java/io/micronaut/openapi/generator/AbstractMicronautJavaCodegen.java index d40fb845fc..c1af4f2173 100644 --- a/openapi-generator/src/main/java/io/micronaut/openapi/generator/AbstractMicronautJavaCodegen.java +++ b/openapi-generator/src/main/java/io/micronaut/openapi/generator/AbstractMicronautJavaCodegen.java @@ -1889,6 +1889,7 @@ public Map postProcessAllModels(Map objs) } } + model.vendorExtensions.put("hasOwnVars", !model.vars.isEmpty()); model.vendorExtensions.put("withMultipleVars", model.vars.size() > 1); if (!requiredParentVarsWithoutDiscriminator.isEmpty()) { model.vendorExtensions.put("requiredParentVarsWithoutDiscriminator", requiredParentVarsWithoutDiscriminator); @@ -1929,6 +1930,7 @@ public Map postProcessAllModels(Map objs) // just for tests if (System.getProperty("micronaut.test.no-vars") != null) { model.hasVars = false; + model.vendorExtensions.put("hasOwnVars", false); model.vendorExtensions.put("requiredVarsWithoutDiscriminator", Collections.emptyList()); model.vendorExtensions.put("optionalVars", Collections.emptyList()); model.vendorExtensions.put("requiredVars", Collections.emptyList()); diff --git a/openapi-generator/src/main/java/io/micronaut/openapi/generator/AbstractMicronautKotlinCodegen.java b/openapi-generator/src/main/java/io/micronaut/openapi/generator/AbstractMicronautKotlinCodegen.java index 7cf96e0592..604328bde9 100644 --- a/openapi-generator/src/main/java/io/micronaut/openapi/generator/AbstractMicronautKotlinCodegen.java +++ b/openapi-generator/src/main/java/io/micronaut/openapi/generator/AbstractMicronautKotlinCodegen.java @@ -1826,6 +1826,7 @@ public Map postProcessAllModels(Map objs) } } + model.vendorExtensions.put("hasOwnVars", !model.vars.isEmpty()); model.vendorExtensions.put("withMultipleVars", model.vars.size() > 1); if (!requiredParentVarsWithoutDiscriminator.isEmpty()) { model.vendorExtensions.put("requiredParentVarsWithoutDiscriminator", requiredParentVarsWithoutDiscriminator); @@ -1857,6 +1858,7 @@ public Map postProcessAllModels(Map objs) // just for tests if (System.getProperty("micronaut.test.no-vars") != null) { model.hasVars = false; + model.vendorExtensions.put("hasOwnVars", false); model.vendorExtensions.put("requiredVarsWithoutDiscriminator", Collections.emptyList()); model.vendorExtensions.put("optionalVars", Collections.emptyList()); model.vendorExtensions.put("requiredVars", Collections.emptyList()); diff --git a/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java b/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java index 8426666222..69a88f2fd1 100644 --- a/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java +++ b/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java @@ -274,9 +274,9 @@ apiTemplateFiles are for API outputs only (controllers/handlers). // sort operations by default protected boolean skipSortingOperations = false; - protected final static Pattern XML_MIME_PATTERN = Pattern.compile("(?i)application/(.*)[+]?xml(;.*)?"); - protected final static Pattern JSON_MIME_PATTERN = Pattern.compile("(?i)application/json(;.*)?"); - protected final static Pattern JSON_VENDOR_MIME_PATTERN = Pattern.compile("(?i)application/vnd.(.*)+json(;.*)?"); + protected static final Pattern XML_MIME_PATTERN = Pattern.compile("(?i)application/(.*)[+]?xml(;.*)?"); + protected static final Pattern JSON_MIME_PATTERN = Pattern.compile("(?i)application/json(;.*)?"); + protected static final Pattern JSON_VENDOR_MIME_PATTERN = Pattern.compile("(?i)application/vnd.(.*)+json(;.*)?"); private static final Pattern COMMON_PREFIX_ENUM_NAME = Pattern.compile("[a-zA-Z0-9]+\\z"); /** diff --git a/openapi-generator/src/main/resources/templates/java-micronaut/common/model/enum.mustache b/openapi-generator/src/main/resources/templates/java-micronaut/common/model/enum.mustache index 24df02090e..d5d307b1d8 100644 --- a/openapi-generator/src/main/resources/templates/java-micronaut/common/model/enum.mustache +++ b/openapi-generator/src/main/resources/templates/java-micronaut/common/model/enum.mustache @@ -45,7 +45,7 @@ {{/enumVars}} {{/allowableValues}} - {{#formatSingleLine}}public final static Map<{{{dataType}}}, {{>common/model/enumName}}> VALUE_MAPPING = Map.copyOf(Arrays.stream(values()){{/formatSingleLine}} + {{#formatSingleLine}}public static final Map<{{{dataType}}}, {{>common/model/enumName}}> VALUE_MAPPING = Map.copyOf(Arrays.stream(values()){{/formatSingleLine}} .collect(Collectors.toMap(v -> v.value{{#isString}}{{#useEnumCaseInsensitive}}.toLowerCase(){{/useEnumCaseInsensitive}}{{/isString}}, Function.identity()))); private final {{{vendorExtensions.baseType}}} value; diff --git a/openapi-generator/src/main/resources/templates/java-micronaut/common/model/pojo.mustache b/openapi-generator/src/main/resources/templates/java-micronaut/common/model/pojo.mustache index 9aef4eb790..6d18691fb2 100644 --- a/openapi-generator/src/main/resources/templates/java-micronaut/common/model/pojo.mustache +++ b/openapi-generator/src/main/resources/templates/java-micronaut/common/model/pojo.mustache @@ -45,7 +45,7 @@ @Serdeable {{/micronaut_serde_jackson}} {{#jackson}} - {{#hasVars}} + {{#vendorExtensions.hasOwnVars}} {{#vendorExtensions.withMultipleVars}} @JsonPropertyOrder({{openbrace}} {{#vars}} @@ -56,7 +56,7 @@ {{^vendorExtensions.withMultipleVars}} @JsonPropertyOrder({{#vars}}{{classname}}.JSON_PROPERTY_{{nameInSnakeCase}}{{/vars}}) {{/vendorExtensions.withMultipleVars}} - {{/hasVars}} + {{/vendorExtensions.hasOwnVars}} {{/jackson}} {{#additionalModelTypeAnnotations}} {{{.}}} @@ -447,15 +447,15 @@ public class {{classname}} {{#parent}}extends {{{parent}}} {{/parent}}{{#vendorE if (o == null || getClass() != o.getClass()) { return false; } - {{#hasVars}} - {{classname}} {{classVarName}} = ({{classname}}) o; - return {{#vars}}{{#isByteArray}}Arrays{{/isByteArray}}{{^isByteArray}}Objects{{/isByteArray}}.equals({{name}}, {{classVarName}}.{{name}}){{^-last}} && - {{/-last}}{{/vars}}{{#parent}} && - super.equals(o){{/parent}}; - {{/hasVars}} - {{^hasVars}} + {{#vendorExtensions.hasOwnVars}} + var {{classVarName}} = ({{classname}}) o; + return {{#vars}}{{#isByteArray}}Arrays{{/isByteArray}}{{^isByteArray}}Objects{{/isByteArray}}.equals({{name}}, {{classVarName}}.{{name}}){{^-last}} + && {{/-last}}{{/vars}}{{#parent}} + && super.equals(o){{/parent}}; + {{/vendorExtensions.hasOwnVars}} + {{^vendorExtensions.hasOwnVars}} return {{#parent}}super.equals(o){{/parent}}{{^parent}}true{{/parent}}; - {{/hasVars}} + {{/vendorExtensions.hasOwnVars}} {{/useReflectionEqualsHashCode}} } @@ -465,7 +465,7 @@ public class {{classname}} {{#parent}}extends {{{parent}}} {{/parent}}{{#vendorE return HashCodeBuilder.reflectionHashCode(this); {{/useReflectionEqualsHashCode}} {{^useReflectionEqualsHashCode}} - return Objects.hash({{#vars}}{{^isByteArray}}{{name}}{{/isByteArray}}{{#isByteArray}}Arrays.hashCode({{name}}){{/isByteArray}}{{^-last}}, {{/-last}}{{/vars}}{{#parent}}{{#hasVars}}, {{/hasVars}}super.hashCode(){{/parent}}); + return Objects.hash({{#vars}}{{^isByteArray}}{{name}}{{/isByteArray}}{{#isByteArray}}Arrays.hashCode({{name}}){{/isByteArray}}{{^-last}}, {{/-last}}{{/vars}}{{#parent}}{{#vendorExtensions.hasOwnVars}}, {{/vendorExtensions.hasOwnVars}}super.hashCode(){{/parent}}); {{/useReflectionEqualsHashCode}} } diff --git a/openapi-generator/src/main/resources/templates/kotlin-micronaut/common/model/enum.mustache b/openapi-generator/src/main/resources/templates/kotlin-micronaut/common/model/enum.mustache index 50824bfa06..054432b630 100644 --- a/openapi-generator/src/main/resources/templates/kotlin-micronaut/common/model/enum.mustache +++ b/openapi-generator/src/main/resources/templates/kotlin-micronaut/common/model/enum.mustache @@ -24,7 +24,7 @@ {{{.}}} {{/additionalEnumTypeAnnotations}} {{#nonPublicApi}}internal {{/nonPublicApi}}{{#formatSingleLine}}enum class {{>common/model/enumName}}{{/formatSingleLine}}( - @get:JsonValue val value: {{{dataType}}} + @get:JsonValue val value: {{{dataType}}}, ) { {{#allowableValues}} @@ -41,13 +41,12 @@ @Deprecated("") {{/deprecated}} @JsonProperty({{{strValue}}}) - {{{name}}}({{{value}}}){{^-last}},{{/-last}}{{#-last}};{{/-last}} + {{{name}}}({{{value}}}),{{#-last}} + ;{{/-last}} {{/enumVars}} {{/allowableValues}} - override fun toString(): String { - return {{#isString}}value{{/isString}}{{^isString}}"$value"{{/isString}} - } + override fun toString(): String = {{#isString}}value{{/isString}}{{^isString}}"$value"{{/isString}} companion object { diff --git a/openapi-generator/src/main/resources/templates/kotlin-micronaut/common/model/model.mustache b/openapi-generator/src/main/resources/templates/kotlin-micronaut/common/model/model.mustache index ef64183873..4772db1f8d 100644 --- a/openapi-generator/src/main/resources/templates/kotlin-micronaut/common/model/model.mustache +++ b/openapi-generator/src/main/resources/templates/kotlin-micronaut/common/model/model.mustache @@ -38,6 +38,7 @@ import {{javaxPackage}}.annotation.Generated {{#generateSwagger2Annotations}} import io.swagger.v3.oas.annotations.media.Schema {{/generateSwagger2Annotations}} +import java.util.* {{/formatNoEmptyLines}} {{#formatOneEmptyLine}} {{#models}} diff --git a/openapi-generator/src/main/resources/templates/kotlin-micronaut/common/model/oneof_interface.mustache b/openapi-generator/src/main/resources/templates/kotlin-micronaut/common/model/oneof_interface.mustache index 6a6344319e..89fa1d42f4 100644 --- a/openapi-generator/src/main/resources/templates/kotlin-micronaut/common/model/oneof_interface.mustache +++ b/openapi-generator/src/main/resources/templates/kotlin-micronaut/common/model/oneof_interface.mustache @@ -6,10 +6,8 @@ {{>common/generatedAnnotation}} {{/generatedAnnotation}} {{>common/model/typeInfoAnnotation}} -interface {{classname}}{{#vendorExtensions.x-implements}}{{#-first}} : {{{.}}}{{/-first}}{{^-first}}, {{{.}}}{{/-first}}{{/vendorExtensions.x-implements}} { -{{/formatNoEmptyLines}} +interface {{classname}}{{#vendorExtensions.x-implements}}{{#-first}} : {{{.}}}{{/-first}}{{^-first}}, {{{.}}}{{/-first}}{{/vendorExtensions.x-implements}}{{/formatNoEmptyLines}}{{#discriminator}} { -{{#discriminator}} val {{propertyName}}: {{propertyType}} -{{/discriminator}} } +{{/discriminator}} diff --git a/openapi-generator/src/main/resources/templates/kotlin-micronaut/common/model/pojo.mustache b/openapi-generator/src/main/resources/templates/kotlin-micronaut/common/model/pojo.mustache index 86218ccb05..9253c624ee 100644 --- a/openapi-generator/src/main/resources/templates/kotlin-micronaut/common/model/pojo.mustache +++ b/openapi-generator/src/main/resources/templates/kotlin-micronaut/common/model/pojo.mustache @@ -19,13 +19,13 @@ @Serdeable {{/micronaut_serde_jackson}} {{#jackson}} - {{#hasVars}} + {{#vendorExtensions.hasOwnVars}} @JsonPropertyOrder( {{#vars}} - {{classname}}.JSON_PROPERTY_{{nameInSnakeCase}}{{^-last}},{{/-last}} + {{classname}}.JSON_PROPERTY_{{nameInSnakeCase}}, {{/vars}} ) - {{/hasVars}} + {{/vendorExtensions.hasOwnVars}} {{/jackson}} {{#additionalModelTypeAnnotations}} {{{.}}} @@ -94,50 +94,38 @@ {{/formatNoEmptyLines}} {{/vendorExtensions.optionalVars}} {{/vendorExtensions.withInheritance}} -{{#hasVars}}){{/hasVars}}{{#parent}}: {{{parent}}}({{#vendorExtensions.requiredParentVarsWithoutDiscriminator}}{{{name}}}{{^-last}}, {{/-last}}{{/vendorExtensions.requiredParentVarsWithoutDiscriminator}}) {{/parent}}{{#vendorExtensions.x-implements}}{{#parent}}, {{/parent}}{{^parent}}: {{/parent}}{{^-first}}, {{/-first}}{{{.}}}{{/vendorExtensions.x-implements}} {{openbrace}} +{{#hasVars}}){{/hasVars}}{{#parent}} : {{{parent}}}({{#vendorExtensions.requiredParentVarsWithoutDiscriminator}}{{{name}}}{{^-last}}, {{/-last}}{{/vendorExtensions.requiredParentVarsWithoutDiscriminator}}){{/parent}}{{#vendorExtensions.x-implements}}{{#parent}}, {{/parent}}{{^parent}}: {{/parent}}{{^-first}}, {{/-first}}{{{.}}}{{/vendorExtensions.x-implements}} {{openbrace}} {{#vendorExtensions.withInheritance}} override fun equals(other: Any?): Boolean { - if (this === other) return true - if (javaClass != other?.javaClass) return false - {{#parent}} - if (!super.equals(other)) return false - {{/parent}} + if (this === other) { + return true + } + if (javaClass != other?.javaClass) { + return false + } other as {{classname}} - {{#hasVars}} - {{#vars}} - if ({{{name}}} != other.{{{name}}}) return false - {{/vars}} - {{/hasVars}} - return true + {{#vendorExtensions.hasOwnVars}} + return {{#vars}}{{^-first}} + && {{/-first}}{{{name}}} == other.{{{name}}}{{/vars}}{{#parent}} + && super.equals(other){{/parent}} + {{/vendorExtensions.hasOwnVars}} + {{^vendorExtensions.hasOwnVars}} + return {{#parent}}super.equals(other){{/parent}}{{^parent}}true{{/parent}} + {{/vendorExtensions.hasOwnVars}} } - override fun hashCode(): Int { - var result = {{#parent}}super.hashCode(){{/parent}}{{^parent}}{{#vars.0}}{{{name}}}{{#isNullable}}?{{/isNullable}}.hashCode(){{#isNullable}} ?: 0{{/isNullable}}{{/vars.0}}{{/parent}} - {{#hasVars}} - {{#vars}} - {{#parent}} - result = 31 * result + {{#isNullable}}({{/isNullable}}{{{name}}}{{#isNullable}}?{{/isNullable}}.hashCode(){{#isNullable}} ?: 0){{/isNullable}} - {{/parent}} - {{^parent}} - {{^-first}} - result = {{#-first}}{{#parent}}31 * result + {{/parent}}{{/-first}}{{^-first}}31 * result + {{/-first}}{{#isNullable}}({{/isNullable}}{{{name}}}{{#isNullable}}?{{/isNullable}}.hashCode(){{#isNullable}} ?: 0){{/isNullable}} - {{/-first}} - {{/parent}} - {{/vars}} - {{/hasVars}} - return result - } + override fun hashCode(): Int = + Objects.hash({{#vars}}{{^isByteArray}}{{name}}{{/isByteArray}}{{#isByteArray}}Arrays.hashCode({{name}}){{/isByteArray}}{{^-last}}, {{/-last}}{{/vars}}{{#parent}}{{#vendorExtensions.hasOwnVars}}, {{/vendorExtensions.hasOwnVars}}super.hashCode(){{/parent}}) - override fun toString(): String { + override fun toString(): String = {{#hasVars}} - return "{{classname}}({{#allVars}}{{{vendorExtensions.realName}}}='${{{name}}}'{{^-last}}, {{/-last}}{{/allVars}})" + "{{classname}}({{#allVars}}{{{vendorExtensions.realName}}}='${{{name}}}'{{^-last}}, {{/-last}}{{/allVars}})" {{/hasVars}} {{^hasVars}} - return "{{classname}}()" + "{{classname}}()" {{/hasVars}} - } {{/vendorExtensions.withInheritance}} {{#vars}} @@ -158,6 +146,7 @@ {{/vars}} + {{#vendorExtensions.hasOwnVars}} companion object { {{#serializableModel}} @@ -170,4 +159,15 @@ const val JSON_PROPERTY_{{nameInSnakeCase}} = "{{baseName}}" {{/vars}} } + {{/vendorExtensions.hasOwnVars}} + {{^vendorExtensions.hasOwnVars}} + {{#serializableModel}} + companion object { + + @Serial + @JvmStatic + private val serialVersionUID = {{{vendorExtensions.serialId}}}L + } + {{/serializableModel}} + {{/vendorExtensions.hasOwnVars}} {{closebrace}} diff --git a/openapi-generator/src/main/resources/templates/kotlin-micronaut/common/model/typeInfoAnnotation.mustache b/openapi-generator/src/main/resources/templates/kotlin-micronaut/common/model/typeInfoAnnotation.mustache index 65826ce7ab..28c0d42172 100644 --- a/openapi-generator/src/main/resources/templates/kotlin-micronaut/common/model/typeInfoAnnotation.mustache +++ b/openapi-generator/src/main/resources/templates/kotlin-micronaut/common/model/typeInfoAnnotation.mustache @@ -2,13 +2,13 @@ {{#discriminator.propertyBaseName}} {{#jackson}} @JsonIgnoreProperties( - value = ["{{{discriminator.propertyBaseName}}}"], // ignore manually set {{{discriminator.propertyBaseName}}}, it will be automatically generated by Jackson during serialization - allowSetters = true // allows the {{{discriminator.propertyBaseName}}} to be set during deserialization + value = ["{{{discriminator.propertyBaseName}}}"], // ignore manually set {{{discriminator.propertyBaseName}}}, it will be automatically generated by Jackson during serialization + allowSetters = true // allows the {{{discriminator.propertyBaseName}}} to be set during deserialization ) @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "{{{discriminator.propertyBaseName}}}", visible = true) {{#vendorExtensions.hasMappedModels}} @JsonSubTypes({{#mappedModels}} - JsonSubTypes.Type(value = {{modelName}}::class, name = "{{^vendorExtensions.x-discriminator-value}}{{mappingName}}{{/vendorExtensions.x-discriminator-value}}{{#vendorExtensions.x-discriminator-value}}{{{vendorExtensions.x-discriminator-value}}}{{/vendorExtensions.x-discriminator-value}}"){{^-last}},{{/-last}} + JsonSubTypes.Type(value = {{modelName}}::class, name = "{{^vendorExtensions.x-discriminator-value}}{{mappingName}}{{/vendorExtensions.x-discriminator-value}}{{#vendorExtensions.x-discriminator-value}}{{{vendorExtensions.x-discriminator-value}}}{{/vendorExtensions.x-discriminator-value}}"), {{/mappedModels}}) {{/vendorExtensions.hasMappedModels}} {{/jackson}} diff --git a/openapi-generator/src/test/java/io/micronaut/openapi/generator/JavaMicronautClientCodegenTest.java b/openapi-generator/src/test/java/io/micronaut/openapi/generator/JavaMicronautClientCodegenTest.java index ade48ab6a4..e4604ea909 100644 --- a/openapi-generator/src/test/java/io/micronaut/openapi/generator/JavaMicronautClientCodegenTest.java +++ b/openapi-generator/src/test/java/io/micronaut/openapi/generator/JavaMicronautClientCodegenTest.java @@ -381,7 +381,7 @@ void testEnumsWithNonStringTypeValue() { assertFileContains(modelPath + "StringEnum.java", "@JsonProperty(\"starting\")", "STARTING(\"starting\"),", """ - public final static Map VALUE_MAPPING = Map.copyOf(Arrays.stream(values()) + public static final Map VALUE_MAPPING = Map.copyOf(Arrays.stream(values()) .collect(Collectors.toMap(v -> v.value, Function.identity()))); """, """ @@ -804,7 +804,7 @@ void testUseEnumCaseInsensitive() { assertFileContains(path + "model/StringEnum.java", """ - public final static Map VALUE_MAPPING = Map.copyOf(Arrays.stream(values()) + public static final Map VALUE_MAPPING = Map.copyOf(Arrays.stream(values()) .collect(Collectors.toMap(v -> v.value.toLowerCase(), Function.identity()))); """, """ @@ -1363,4 +1363,32 @@ void testSwaggerAnnotations() { ); """); } + + @Test + void testEquals() { + + var codegen = new JavaMicronautClientCodegen(); + codegen.setGenerateSwaggerAnnotations(true); + String outputPath = generateFiles(codegen, "src/test/resources/3_0/check-equals.yml", CodegenConstants.MODELS); + String path = outputPath + "src/main/java/org/openapitools/"; + + assertFileNotContains(path + "model/SalesInvoiceCreateDto.java", "@JsonPropertyOrder"); + assertFileContains(path + "model/SalesInvoiceCreateDto.java", """ + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + return super.equals(o); + } + + @Override + public int hashCode() { + return Objects.hash(super.hashCode()); + } + """); + } } diff --git a/openapi-generator/src/test/java/io/micronaut/openapi/generator/KotlinMicronautClientCodegenTest.java b/openapi-generator/src/test/java/io/micronaut/openapi/generator/KotlinMicronautClientCodegenTest.java index dfea93571b..3b11e46580 100644 --- a/openapi-generator/src/test/java/io/micronaut/openapi/generator/KotlinMicronautClientCodegenTest.java +++ b/openapi-generator/src/test/java/io/micronaut/openapi/generator/KotlinMicronautClientCodegenTest.java @@ -349,7 +349,7 @@ data class ExtendedBookInfo( @field:JsonProperty(JSON_PROPERTY_REQUIRED_READ_ONLY) @field:JsonInclude(JsonInclude.Include.USE_DEFAULTS) override var requiredReadOnly: String?, - ): BookInfo(name, requiredReadOnly) { + ) : BookInfo(name, requiredReadOnly) { """); } @@ -414,7 +414,7 @@ open class BasicBookInfo( @field:NotNull @field:JsonProperty(JSON_PROPERTY_NAME) override var name: String, - ): BookInfo(name) { + ) : BookInfo(name) { """); assertFileContains(apiPath + "DetailedBookInfo.kt", """ @@ -430,7 +430,7 @@ data class DetailedBookInfo( @field:NotNull @field:JsonProperty(JSON_PROPERTY_NAME) override var name: String, - ): BasicBookInfo(author, name) + ) : BasicBookInfo(author, name) """); } @@ -702,7 +702,7 @@ data class OpAdd( @field:NotNull @field:JsonProperty(JSON_PROPERTY_OP) override var op: String, - ): JsonOp(path, op) { + ) : JsonOp(path, op) { """ ); } @@ -931,7 +931,8 @@ void testEnumsExtensions() { """ @Deprecated("") @JsonProperty("34.1") - NUMBER_34_DOT_1(BigDecimal("34.1")); + NUMBER_34_DOT_1(BigDecimal("34.1")), + ; """); assertFileContains(modelPath + "ByteEnum.kt", @@ -1295,10 +1296,6 @@ void testNoVars() { @Generated("io.micronaut.openapi.generator.KotlinMicronautClientCodegen") @Serializable class Book { - - companion object { - - } } """); @@ -1340,4 +1337,30 @@ fun findPetsByStatus( ): Mono> """); } + + @Test + void testEquals() { + + var codegen = new KotlinMicronautClientCodegen(); + codegen.setGenerateSwaggerAnnotations(true); + String outputPath = generateFiles(codegen, "src/test/resources/3_0/check-equals.yml", CodegenConstants.MODELS); + String path = outputPath + "src/main/kotlin/org/openapitools/"; + + assertFileNotContains(path + "model/SalesInvoiceCreateDto.kt", "@JsonPropertyOrder"); + assertFileContains(path + "model/SalesInvoiceCreateDto.kt", """ + override fun equals(other: Any?): Boolean { + if (this === other) { + return true + } + if (javaClass != other?.javaClass) { + return false + } + other as SalesInvoiceCreateDto + return super.equals(other) + } + + override fun hashCode(): Int = + Objects.hash(super.hashCode()) + """); + } } diff --git a/openapi-generator/src/test/java/io/micronaut/openapi/generator/KotlinMicronautServerCodegenTest.java b/openapi-generator/src/test/java/io/micronaut/openapi/generator/KotlinMicronautServerCodegenTest.java index fc2e6b4254..946f690845 100644 --- a/openapi-generator/src/test/java/io/micronaut/openapi/generator/KotlinMicronautServerCodegenTest.java +++ b/openapi-generator/src/test/java/io/micronaut/openapi/generator/KotlinMicronautServerCodegenTest.java @@ -388,7 +388,7 @@ data class ExtendedBookInfo( @field:JsonProperty(JSON_PROPERTY_REQUIRED_READ_ONLY) @field:JsonInclude(JsonInclude.Include.USE_DEFAULTS) override var requiredReadOnly: String? = null, - ): BookInfo(name, requiredReadOnly) { + ) : BookInfo(name, requiredReadOnly) { """); } @@ -424,7 +424,7 @@ open class BasicBookInfo( @field:Schema(name = "name", requiredMode = Schema.RequiredMode.REQUIRED) @field:JsonProperty(JSON_PROPERTY_NAME) override var name: String, - ): BookInfo(name) { + ) : BookInfo(name) { """); assertFileContains(apiPath + "DetailedBookInfo.kt", """ @@ -443,7 +443,7 @@ data class DetailedBookInfo( @field:Schema(name = "name", requiredMode = Schema.RequiredMode.REQUIRED) @field:JsonProperty(JSON_PROPERTY_NAME) override var name: String, - ): BasicBookInfo(author, name) { + ) : BasicBookInfo(author, name) { """); } diff --git a/openapi-generator/src/test/resources/3_0/check-equals.yml b/openapi-generator/src/test/resources/3_0/check-equals.yml new file mode 100644 index 0000000000..8e74cde24f --- /dev/null +++ b/openapi-generator/src/test/resources/3_0/check-equals.yml @@ -0,0 +1,144 @@ +openapi: 3.0.3 +info: + title: Sample App API + version: 1.0.0 +tags: + - name: Invoice +paths: + /api/invoice: + post: + tags: + - Invoice + operationId: createInvoice + summary: Creates new sale invoice + requestBody: + required: true + content: + application/json: + schema: + oneOf: + - $ref: '#/components/schemas/SalesInvoiceCreateDto' + - $ref: '#/components/schemas/CurrencyInvoiceCreateDto' + responses: + 201: + description: Invoice created correctly + content: + application/json: + schema: + $ref: '#/components/schemas/InvoiceIdDto' + 403: + description: No access to cost center with Manager rights + content: + application/json: + schema: + $ref: '#/components/schemas/ErrorDTO' + /api/invoice/{id}: + get: + tags: + - Invoice + parameters: + - in: path + name: id + schema: + type: string + format: uuid + operationId: loadInvoice + summary: load invoice + responses: + 200: + description: Sales or Currency invoice + content: + application/json: + schema: + oneOf: + - $ref: '#/components/schemas/SalesInvoiceDto' + - $ref: '#/components/schemas/CurrencyInvoiceDto' + 403: + description: No access to cost center with Manager rights + content: + application/json: + schema: + $ref: '#/components/schemas/ErrorDTO' +components: + schemas: + DocType: + type: string + enum: [ FS, FW, FK, FP ] + default: "FS" + BaseInvoiceDto: + type: object + required: + - sellerVatId + - docType + properties: + docType: + $ref: '#/components/schemas/DocType' + sellerVatId: + type: string + maxLength: 10 + discriminator: + propertyName: docType + mapping: + FS: "#/components/schemas/SalesInvoiceCreateDto" + FW: "#/components/schemas/CurrencyInvoiceCreateDto" + SalesInvoiceCreateDto: + allOf: + - $ref: '#/components/schemas/BaseInvoiceDto' + SalesInvoiceDto: + allOf: + - $ref: '#/components/schemas/BaseInvoiceDto' + - $ref: '#/components/schemas/InvoiceCommonFields' + CurrencyInvoiceCreateDto: + allOf: + - $ref: '#/components/schemas/SalesInvoiceCreateDto' + - type: object + properties: + currency: + type: string + description: "Currency of the invoice" + default: "PLN" + exchangeRate: + type: number + description: "Exchange rate for the currency" + exchangeDate: + type: string + format: date + required: + - currency + - exchangeRate + - exchangeDate + CurrencyInvoiceDto: + allOf: + - $ref: '#/components/schemas/CurrencyInvoiceCreateDto' + - $ref: '#/components/schemas/InvoiceCommonFields' + InvoiceCommonFields: + properties: + issuedUser: + type: string + issueDate: + type: string + format: date + InvoiceIdDto: + type: object + properties: + id: + type: string + format: uuid + docNumber: + type: string + ErrorDTO: + type: object + properties: + message: + type: string + code: + type: integer + retryable: + type: boolean + severity: + type: string + enum: [ WARN, ERROR, FATAL ] + params: + type: object + additionalProperties: + type: string