Skip to content

Commit

Permalink
Fix generating model with discriminator.
Browse files Browse the repository at this point in the history
Remove discriminator from required properties constructor.

Fixed micronaut-projects#1163
Fixed micronaut-projects#1168
  • Loading branch information
altro3 committed Aug 16, 2023
1 parent c82d466 commit 50d07e1
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -786,14 +786,46 @@ public Map<String, ModelsMap> postProcessAllModels(Map<String, ModelsMap> 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<CodegenProperty>();
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<CodegenProperty>();
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<CodegenProperty> 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}}
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 50d07e1

Please sign in to comment.