Skip to content

Commit

Permalink
Fix generating model with discriminator.
Browse files Browse the repository at this point in the history
Fixed #1163
  • Loading branch information
altro3 committed Aug 13, 2023
1 parent c82d466 commit b7bf7ff
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -794,6 +794,10 @@ public Map<String, ModelsMap> postProcessAllModels(Map<String, ModelsMap> objs)
List<CodegenProperty> 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}}

0 comments on commit b7bf7ff

Please sign in to comment.