Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix readonly properties for generator. #1165

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.Random;
import java.util.Set;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -445,6 +446,8 @@ public String testFileFolder() {
return (getOutputDir() + "/src/test/java/").replace('/', File.separatorChar);
}

public abstract boolean isServer();

@Override
public String apiTestFileFolder() {
return testFileFolder() + apiPackage().replaceAll("\\.", "/");
Expand Down Expand Up @@ -614,9 +617,7 @@ public CodegenParameter fromParameter(Parameter param, Set<String> imports) {
}

@Override
public CodegenOperation fromOperation(
String path, String httpMethod, Operation operation, List<Server> servers
) {
public CodegenOperation fromOperation(String path, String httpMethod, Operation operation, List<Server> servers) {
CodegenOperation op = super.fromOperation(path, httpMethod, operation, servers);

if (op.isResponseFile) {
Expand Down Expand Up @@ -715,9 +716,7 @@ private void processWithResponseBodyMapping(CodegenOperation op) {
* @param isValidated Whether the wrapper requires validation.
* @param isListWrapper Whether the wrapper should be around list items.
*/
private void wrapOperationReturnType(
CodegenOperation op, String wrapperType, boolean isValidated, boolean isListWrapper
) {
private void wrapOperationReturnType(CodegenOperation op, String wrapperType, boolean isValidated, boolean isListWrapper) {
CodegenProperty newReturnType = new CodegenProperty();
newReturnType.required = true;
newReturnType.isModel = isValidated;
Expand Down Expand Up @@ -784,6 +783,9 @@ private String makeSureImported(String typeName, Set<String> imports) {
public Map<String, ModelsMap> postProcessAllModels(Map<String, ModelsMap> objs) {
objs = super.postProcessAllModels(objs);

var isServer = isServer();
var random = new Random();

for (ModelsMap models : objs.values()) {
CodegenModel model = models.getModels().get(0).getModel();

Expand Down Expand Up @@ -822,10 +824,18 @@ public Map<String, ModelsMap> postProcessAllModels(Map<String, ModelsMap> objs)
model.vendorExtensions.put("requiredVarsWithoutDiscriminator", requiredVarsWithoutDiscriminator);
model.vendorExtensions.put("requiredVars", requiredVars);
model.vendorExtensions.put("areRequiredVarsAndReadOnlyVars", !requiredVarsWithoutDiscriminator.isEmpty() && !model.readOnlyVars.isEmpty());
model.vendorExtensions.put("serialId", random.nextLong());
if (model.discriminator != null) {
model.vendorExtensions.put("hasMappedModels", !model.discriminator.getMappedModels().isEmpty());
model.vendorExtensions.put("hasMultipleMappedModels", model.discriminator.getMappedModels().size() > 1);
}
model.vendorExtensions.put("isServer", isServer);
for (var property : model.vars) {
property.vendorExtensions.put("isServer", isServer);
}
for (var property : model.requiredVars) {
property.vendorExtensions.put("isServer", isServer);
}
}

return objs;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,11 @@ public void processOpts() {
apiDocTemplateFiles.put("client/doc/api_doc.mustache", ".md");
}

@Override
public boolean isServer() {
return false;
}

public void setAdditionalClientTypeAnnotations(final List<String> additionalClientTypeAnnotations) {
this.additionalClientTypeAnnotations = additionalClientTypeAnnotations;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,11 @@ public void processOpts() {
}
}

@Override
public boolean isServer() {
return true;
}

@Override
public String apiTestFileFolder() {
// Set it to the whole output dir, so that validation always passes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,16 @@
@JsonInclude(JsonInclude.Include.USE_DEFAULTS)
{{/required}}{{/items.isNullable}}
{{/isMap}}
{{^isMap}}{{^required}}
{{^isMap}}
{{#required}}
{{#isReadOnly}}
@JsonInclude(JsonInclude.Include.USE_DEFAULTS)
{{/required}}{{/isMap}}
{{/isReadOnly}}
{{/required}}
{{^required}}
@JsonInclude(JsonInclude.Include.USE_DEFAULTS)
{{/required}}
{{/isMap}}
{{#withXml}}
{{^isContainer}}
@JacksonXmlProperty({{#isXmlAttribute}}isAttribute = true, {{/isXmlAttribute}}{{#xmlNamespace}}namespace="{{xmlNamespace}}", {{/xmlNamespace}}localName = "{{#xmlName}}{{xmlName}}{{/xmlName}}{{^xmlName}}{{baseName}}{{/xmlName}}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import java.util.stream.Collectors;
import {{import}};
{{/imports}}
{{#serializableModel}}
import java.io.Serial;
import java.io.Serializable;
{{/serializableModel}}
{{#jackson}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@
public class {{classname}} {{#parent}}extends {{{parent}}} {{/parent}}{{#vendorExtensions.x-implements}}{{#-first}}implements {{{.}}}{{/-first}}{{^-first}}, {{{.}}}{{/-first}}{{#-last}} {{/-last}}{{/vendorExtensions.x-implements}}{
{{/formatNoEmptyLines}}
{{#serializableModel}}
private static final long serialVersionUID = 1L;

@Serial
private static final long serialVersionUID = {{{vendorExtensions.serialId}}}L;
{{/serializableModel}}

{{#vars}}
Expand Down Expand Up @@ -113,13 +115,20 @@ public class {{classname}} {{#parent}}extends {{{parent}}} {{/parent}}{{#vendorE
{{/vars}}

{{#requiredPropertiesInConstructor}}
public {{classname}}({{#vendorExtensions.requiredVarsWithoutDiscriminator}}{{{datatypeWithEnum}}} {{name}}{{^-last}}, {{/-last}}{{/vendorExtensions.requiredVarsWithoutDiscriminator}}{{#vendorExtensions.areRequiredVarsAndReadOnlyVars}}, {{/vendorExtensions.areRequiredVarsAndReadOnlyVars}}{{#readOnlyVars}}{{{datatypeWithEnum}}} {{name}}{{^-last}}, {{/-last}}{{/readOnlyVars}}) {
public {{classname}}({{#vendorExtensions.requiredVarsWithoutDiscriminator}}{{#isReadOnly}}{{#vendorExtensions.isServer}}{{^-first}}, {{/-first}}{{{datatypeWithEnum}}} {{name}}{{/vendorExtensions.isServer}}{{/isReadOnly}}{{^isReadOnly}}{{^-first}}, {{/-first}}{{{datatypeWithEnum}}} {{name}}{{/isReadOnly}}{{/vendorExtensions.requiredVarsWithoutDiscriminator}}) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could make a vendor extension isWrittable instead to not repeat the code twice, but this is fine.

{{#parent}}
super({{#vendorExtensions.requiredParentVarsWithoutDiscriminator}}{{name}}{{^-last}}, {{/-last}}{{/vendorExtensions.requiredParentVarsWithoutDiscriminator}});
{{/parent}}
{{#vendorExtensions.requiredVars}}
{{^isDiscriminator}}
{{#isReadOnly}}
{{#vendorExtensions.isServer}}
this.{{name}} = {{name}};
{{/vendorExtensions.isServer}}
{{/isReadOnly}}
{{^isReadOnly}}
this.{{name}} = {{name}};
{{/isReadOnly}}
{{/isDiscriminator}}
{{/vendorExtensions.requiredVars}}
}
Expand Down Expand Up @@ -185,7 +194,7 @@ public class {{classname}} {{#parent}}extends {{{parent}}} {{/parent}}{{#vendorE
{{#vendorExtensions.x-setter-extra-annotation}}
{{{vendorExtensions.x-setter-extra-annotation}}}
{{/vendorExtensions.x-setter-extra-annotation}}
{{^isReadOnly}}public {{/isReadOnly}}{{#isReadOnly}}{{^micronaut_serde_jersey}}private {{/micronaut_serde_jersey}}{{/isReadOnly}}void {{setter}}({{{datatypeWithEnum}}} {{name}}) {
public void {{setter}}({{{datatypeWithEnum}}} {{name}}) {
{{#vendorExtensions.x-is-jackson-optional-nullable}}
this.{{name}} = JsonNullable.<{{{datatypeWithEnum}}}>of({{name}});
{{/vendorExtensions.x-is-jackson-optional-nullable}}
Expand All @@ -205,7 +214,7 @@ public class {{classname}} {{#parent}}extends {{{parent}}} {{/parent}}{{#vendorE
}

@JsonProperty(JSON_PROPERTY_{{nameInSnakeCase}})
{{#isReadOnly}}private{{/isReadOnly}}{{^isReadOnly}}public{{/isReadOnly}} void {{setter}}_JsonNullable(JsonNullable<{{{datatypeWithEnum}}}> {{name}}) {
public void {{setter}}_JsonNullable(JsonNullable<{{{datatypeWithEnum}}}> {{name}}) {
{{! For getters/setters that have name differing from attribute name, we must include setter (albeit private) for jackson to be able to set the attribute}} this.{{name}} = {{name}};
}
{{/vendorExtensions.x-is-jackson-optional-nullable}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,21 @@
{{/generateHardNullable}}
{{/isNullable}}
{{^isNullable}}
{{#required}}
{{#isReadOnly}}
{{#vendorExtensions.isServer}}
{{#generateHardNullable}}
@HardNullable
{{/generateHardNullable}}
{{^generateHardNullable}}
@Nullable
{{/generateHardNullable}}
{{/vendorExtensions.isServer}}
{{^vendorExtensions.isServer}}
@Nullable
{{/vendorExtensions.isServer}}
{{/isReadOnly}}
{{/required}}
{{^required}}
{{#generateHardNullable}}
@HardNullable
Expand All @@ -21,7 +36,9 @@
{{#useBeanValidation}}
{{^isNullable}}
{{#required}}
{{^isReadOnly}}
@NotNull
{{/isReadOnly}}
{{/required}}
{{/isNullable}}
{{!Validate all pojos and enums}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ private static class TestGenerator extends AbstractMicronautJavaCodegen<TestBuil

private String value;

@Override
public boolean isServer() {
return false;
}

@Override
public TestBuilder optionsBuilder() {
return new TestBuilder() {
Expand Down