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 98e79da8c5..efbcc6c87e 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 @@ -786,14 +786,46 @@ public Map postProcessAllModels(Map objs) for (ModelsMap models : objs.values()) { CodegenModel model = models.getModels().get(0).getModel(); - if (model.getParentModel() != null) { - model.vendorExtensions.put("requiredParentVars", model.getParentModel().requiredVars); + + var hasParent = model.getParentModel() != null; + var requiredVarsWithoutDiscriminator = new ArrayList(); + for (var v : model.requiredVars) { + boolean isDiscriminator = false; + if (hasParent) { + for (var pv : model.getParentModel().getAllVars()) { + if (pv.required && pv.getName().equals(v.getName())) { + isDiscriminator = pv.isDiscriminator; + break; + } + } + } else { + isDiscriminator = v.isDiscriminator; + } + if (!isDiscriminator) { + requiredVarsWithoutDiscriminator.add(v); + } + } + + if (hasParent) { + var parentRequiredVarsWithoutDiscriminator = new ArrayList(); + for (var v : model.getParentModel().vars) { + if (v.required && !v.isDiscriminator) { + parentRequiredVarsWithoutDiscriminator.add(v); + } + } + model.vendorExtensions.put("requiredParentVarsWithoutDiscriminator", parentRequiredVarsWithoutDiscriminator); model.parentVars = model.getParentModel().allVars; } List requiredVars = model.vars.stream().filter(v -> v.required).collect(Collectors.toList()); + + model.vendorExtensions.put("requiredVarsWithoutDiscriminator", requiredVarsWithoutDiscriminator); model.vendorExtensions.put("requiredVars", requiredVars); - model.vendorExtensions.put("areRequiredVarsAndReadOnlyVars", !requiredVars.isEmpty() && !model.readOnlyVars.isEmpty()); + model.vendorExtensions.put("areRequiredVarsAndReadOnlyVars", !requiredVarsWithoutDiscriminator.isEmpty() && !model.readOnlyVars.isEmpty()); + if (model.discriminator != null) { + model.vendorExtensions.put("hasMappedModels", !model.discriminator.getMappedModels().isEmpty()); + model.vendorExtensions.put("hasMultipleMappedModels", model.discriminator.getMappedModels().size() > 1); + } } return objs; 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 6bed58f24e..0b5e758bb9 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 @@ -113,16 +113,17 @@ public class {{classname}} {{#parent}}extends {{{parent}}} {{/parent}}{{#vendorE {{/vars}} {{#requiredPropertiesInConstructor}} - public {{classname}}({{#requiredVars}}{{{datatypeWithEnum}}} {{name}}{{^-last}}, {{/-last}}{{/requiredVars}}{{#vendorExtensions.areRequiredVarsAndReadOnlyVars}}, {{/vendorExtensions.areRequiredVarsAndReadOnlyVars}}{{#readOnlyVars}}{{{datatypeWithEnum}}} {{name}}{{^-last}}, {{/-last}}{{/readOnlyVars}}) { + public {{classname}}({{#vendorExtensions.requiredVarsWithoutDiscriminator}}{{{datatypeWithEnum}}} {{name}}{{^-last}}, {{/-last}}{{/vendorExtensions.requiredVarsWithoutDiscriminator}}{{#vendorExtensions.areRequiredVarsAndReadOnlyVars}}, {{/vendorExtensions.areRequiredVarsAndReadOnlyVars}}{{#readOnlyVars}}{{{datatypeWithEnum}}} {{name}}{{^-last}}, {{/-last}}{{/readOnlyVars}}) { {{#parent}} - super({{#vendorExtensions.requiredParentVars}}{{name}}{{^-last}}, {{/-last}}{{/vendorExtensions.requiredParentVars}}); + super({{#vendorExtensions.requiredParentVarsWithoutDiscriminator}}{{name}}{{^-last}}, {{/-last}}{{/vendorExtensions.requiredParentVarsWithoutDiscriminator}}); {{/parent}} {{#vendorExtensions.requiredVars}} + {{^isDiscriminator}} this.{{name}} = {{name}}; + {{/isDiscriminator}} {{/vendorExtensions.requiredVars}} } {{/requiredPropertiesInConstructor}} - {{^requiredPropertiesInConstructor}} public {{classname}}() { {{#parent}} diff --git a/openapi-generator/src/main/resources/templates/java-micronaut/common/model/typeInfoAnnotation.mustache b/openapi-generator/src/main/resources/templates/java-micronaut/common/model/typeInfoAnnotation.mustache index 7d26adf444..c0232e5ce5 100644 --- a/openapi-generator/src/main/resources/templates/java-micronaut/common/model/typeInfoAnnotation.mustache +++ b/openapi-generator/src/main/resources/templates/java-micronaut/common/model/typeInfoAnnotation.mustache @@ -3,12 +3,17 @@ 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, include = JsonTypeInfo.As.PROPERTY, property = "{{{discriminator.propertyBaseName}}}"{{^micronaut_serde_jackson}}, visible = true{{/micronaut_serde_jackson}}) +@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "{{{discriminator.propertyBaseName}}}"{{^micronaut_serde_jackson}}, visible = true{{/micronaut_serde_jackson}}) {{#discriminator}} - {{#mappedModels.0}} -@JsonSubTypes({ {{#mappedModels}} + {{#vendorExtensions.hasMappedModels}} + {{#vendorExtensions.hasMultipleMappedModels}} +@JsonSubTypes({{openbrace}}{{#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}} -{{/mappedModels}}{{closebarace}}) - {{/mappedModels.0}} +{{/mappedModels}}{{closebrace}}) + {{/vendorExtensions.hasMultipleMappedModels}} + {{^vendorExtensions.hasMultipleMappedModels}} +@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}}"){{/mappedModels}}) + {{/vendorExtensions.hasMultipleMappedModels}} + {{/vendorExtensions.hasMappedModels}} {{/discriminator}} {{/jackson}}