From b7bf7ffb75970dab59b85434c1bc193b909c5c8b Mon Sep 17 00:00:00 2001 From: altro3 Date: Sun, 13 Aug 2023 18:46:09 +0700 Subject: [PATCH] Fix generating model with discriminator. Fixed #1163 --- .../generator/AbstractMicronautJavaCodegen.java | 4 ++++ .../common/model/typeInfoAnnotation.mustache | 15 ++++++++++----- 2 files changed, 14 insertions(+), 5 deletions(-) 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..5e998851f6 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 @@ -794,6 +794,10 @@ public Map postProcessAllModels(Map objs) List requiredVars = model.vars.stream().filter(v -> v.required).collect(Collectors.toList()); model.vendorExtensions.put("requiredVars", requiredVars); model.vendorExtensions.put("areRequiredVarsAndReadOnlyVars", !requiredVars.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/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}}