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

[Java] Remove bean validation annotations on builder #19580

Merged
Show file tree
Hide file tree
Changes from 7 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 @@ -54,6 +54,8 @@ public class CodegenProperty implements Cloneable, IJsonSchemaValidationProperti
@Getter @Setter
public String datatypeWithEnum;
@Getter @Setter
public String rawDatatypeWithEnum;
@Getter @Setter
public String dataFormat;
/**
* The name of this property in the OpenAPI schema.
Expand Down Expand Up @@ -972,6 +974,7 @@ public String toString() {
sb.append(", description='").append(description).append('\'');
sb.append(", dataType='").append(dataType).append('\'');
sb.append(", datatypeWithEnum='").append(datatypeWithEnum).append('\'');
sb.append(", rawDatatypeWithEnum='").append(rawDatatypeWithEnum).append('\'');
sb.append(", dataFormat='").append(dataFormat).append('\'');
sb.append(", name='").append(name).append('\'');
sb.append(", min='").append(min).append('\'');
Expand Down Expand Up @@ -1161,6 +1164,7 @@ public boolean equals(Object o) {
Objects.equals(description, that.description) &&
Objects.equals(dataType, that.dataType) &&
Objects.equals(datatypeWithEnum, that.datatypeWithEnum) &&
Objects.equals(rawDatatypeWithEnum, that.rawDatatypeWithEnum) &&
Objects.equals(dataFormat, that.dataFormat) &&
Objects.equals(name, that.name) &&
Objects.equals(min, that.min) &&
Expand Down Expand Up @@ -1204,7 +1208,7 @@ public boolean equals(Object o) {
public int hashCode() {

return Objects.hash(openApiType, baseName, complexType, getter, setter, description,
dataType, datatypeWithEnum, dataFormat, name, min, max, defaultValue,
dataType, datatypeWithEnum, rawDatatypeWithEnum, dataFormat, name, min, max, defaultValue,
defaultValueWithParam, baseType, containerType, containerTypeMapped, title, unescapedDescription,
maxLength, minLength, pattern, example, jsonSchema, minimum, maximum,
exclusiveMinimum, exclusiveMaximum, required, deprecated,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4176,6 +4176,8 @@ public CodegenProperty fromProperty(String name, Schema p, boolean required, boo
property.defaultValue = toDefaultValue(property, p);
property.defaultValueWithParam = toDefaultValueWithParam(name, p);

property.rawDatatypeWithEnum = property.datatypeWithEnum;

LOGGER.debug("debugging from property return: {}", property);
schemaCodegenPropertyCache.put(ns, property);
return property;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -642,7 +642,7 @@ public void processOpts() {
convertPropertyToBooleanAndWriteBack(CONTAINER_DEFAULT_TO_NULL, this::setContainerDefaultToNull);

additionalProperties.put("sanitizeGeneric", (Mustache.Lambda) (fragment, writer) -> {
String content = removeAnnotations(fragment.execute());
String content = removeBeanValidationAnnotations(fragment.execute());
for (final String s: List.of("<", ">", ",", " ")) {
content = content.replace(s, "");
}
Expand Down Expand Up @@ -1785,6 +1785,12 @@ public void postProcessModelProperty(CodegenModel model, CodegenProperty propert
if (property.dataType != null && property.dataType.equals(property.name) && property.dataType.toUpperCase(Locale.ROOT).equals(property.name)) {
property.name = property.name.toLowerCase(Locale.ROOT);
}

if (useBeanValidation) {
property.rawDatatypeWithEnum = removeBeanValidationAnnotations(property.datatypeWithEnum);
} else {
property.rawDatatypeWithEnum = property.datatypeWithEnum;
}
}

@Override
Expand All @@ -1793,18 +1799,16 @@ public void postProcessResponseWithProperty(CodegenResponse response, CodegenPro
return;
}

// the response data types should not contain a bean validation annotation.
if (property.dataType.contains("@")) {
property.dataType = removeAnnotations(property.dataType);
}
// the response data types should not contain a bean validation annotation.
if (response.dataType.contains("@")) {
response.dataType = removeAnnotations(response.dataType);
}
// the response data types should not contain bean validation annotations.
property.dataType = removeBeanValidationAnnotations(property.dataType);
response.dataType = removeBeanValidationAnnotations(response.dataType);
}

private String removeAnnotations(String type) {
return type.replaceAll("(?:(?i)@[a-z0-9]*+([(].*[)]|\\s*))*+", "");
private String removeBeanValidationAnnotations(String dataType) {
if (dataType != null && dataType.contains("@")) {
return dataType.replaceAll("(?:(?i)@[a-z0-9]*+([(].*[)]|\\s*))*+", "");
}
return dataType;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1020,6 +1020,7 @@ public ModelsMap postProcessModels(ModelsMap objs) {
if (Boolean.TRUE.equals(var.getVendorExtensions().get("x-enum-as-string"))) {
// treat enum string as just string
var.datatypeWithEnum = var.dataType;
var.rawDatatypeWithEnum = var.dataType;

if (StringUtils.isNotEmpty(var.defaultValue)) { // has default value
String defaultValue = var.defaultValue.substring(var.defaultValue.lastIndexOf('.') + 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,7 @@ public ModelsMap postProcessModels(ModelsMap objs) {
if (Boolean.TRUE.equals(var.getVendorExtensions().get("x-enum-as-string"))) {
// treat enum string as just string
var.datatypeWithEnum = var.dataType;
var.rawDatatypeWithEnum = var.datatypeWithEnum;

if (StringUtils.isNotEmpty(var.defaultValue)) { // has default value
String defaultValue = var.defaultValue.substring(var.defaultValue.lastIndexOf('.') + 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,30 @@ public static class Builder {{#parentModel}}extends {{classname}}.Builder {{/par
}

{{#vars}}
public {{classname}}.Builder {{name}}({{{datatypeWithEnum}}} {{name}}) {
public {{classname}}.Builder {{name}}({{{rawDatatypeWithEnum}}} {{name}}) {
{{#vendorExtensions.x-is-jackson-optional-nullable}}
this.instance.{{name}} = JsonNullable.<{{{datatypeWithEnum}}}>of({{name}});
this.instance.{{name}} = JsonNullable.<{{{rawDatatypeWithEnum}}}>of({{name}});
{{/vendorExtensions.x-is-jackson-optional-nullable}}
{{^vendorExtensions.x-is-jackson-optional-nullable}}
this.instance.{{name}} = {{name}};
{{/vendorExtensions.x-is-jackson-optional-nullable}}
return this;
}
{{#vendorExtensions.x-is-jackson-optional-nullable}}
public {{classname}}.Builder {{name}}(JsonNullable<{{{datatypeWithEnum}}}> {{name}}) {
public {{classname}}.Builder {{name}}(JsonNullable<{{{rawDatatypeWithEnum}}}> {{name}}) {
this.instance.{{name}} = {{name}};
return this;
}
{{/vendorExtensions.x-is-jackson-optional-nullable}}
{{/vars}}

{{#parentVars}}
public {{classname}}.Builder {{name}}({{{datatypeWithEnum}}} {{name}}) { // inherited: {{isInherited}}
public {{classname}}.Builder {{name}}({{{rawDatatypeWithEnum}}} {{name}}) { // inherited: {{isInherited}}
super.{{name}}({{name}});
return this;
}
{{#vendorExtensions.x-is-jackson-optional-nullable}}
public {{classname}}.Builder {{name}}(JsonNullable<{{{datatypeWithEnum}}}> {{name}}) {
public {{classname}}.Builder {{name}}(JsonNullable<{{{rawDatatypeWithEnum}}}> {{name}}) {
this.instance.{{name}} = {{name}};
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ public class {{classname}} {{#parent}}extends {{{.}}}{{/parent}} {{#vendorExtens
return new {{classname}}BuilderImpl();
}

private static class {{classname}}BuilderImpl extends {{classname}}Builder<{{classname}}, {{classname}}BuilderImpl> {
private static final class {{classname}}BuilderImpl extends {{classname}}Builder<{{classname}}, {{classname}}BuilderImpl> {

@Override
protected {{classname}}BuilderImpl self() {
Expand All @@ -203,7 +203,7 @@ public class {{classname}} {{#parent}}extends {{{.}}}{{/parent}} {{#vendorExtens

public static abstract class {{classname}}Builder<C extends {{classname}}, B extends {{classname}}Builder<C, B>> {{#parent}}extends {{{.}}}Builder<C, B>{{/parent}} {
{{#vars}}
private {{{datatypeWithEnum}}} {{name}}{{#defaultValue}} = {{{.}}}{{/defaultValue}};
private {{{rawDatatypeWithEnum}}} {{name}}{{#defaultValue}} = {{{.}}}{{/defaultValue}};
{{/vars}}
{{^parent}}
protected abstract B self();
Expand All @@ -212,7 +212,7 @@ public class {{classname}} {{#parent}}extends {{{.}}}{{/parent}} {{#vendorExtens
{{/parent}}

{{#vars}}
public B {{name}}({{{datatypeWithEnum}}} {{name}}) {
public B {{name}}({{{rawDatatypeWithEnum}}} {{name}}) {
this.{{name}} = {{name}};
return self();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,25 +24,25 @@
{{#deprecated}}
@Deprecated
{{/deprecated}}
public {{classname}}.Builder {{name}}({{{datatypeWithEnum}}} {{name}}) {
public {{classname}}.Builder {{name}}({{{rawDatatypeWithEnum}}} {{name}}) {
this.instance.{{name}}({{name}});
return this;
}
{{#openApiNullable}}{{#isNullable}}
public {{classname}}.Builder {{name}}(JsonNullable<{{{datatypeWithEnum}}}> {{name}}) {
public {{classname}}.Builder {{name}}(JsonNullable<{{{rawDatatypeWithEnum}}}> {{name}}) {
this.instance.{{name}} = {{name}};
return this;
}
{{/isNullable}}{{/openApiNullable}}
{{/vars}}
{{#parentVars}}
@Override
public {{classname}}.Builder {{name}}({{{datatypeWithEnum}}} {{name}}) {
public {{classname}}.Builder {{name}}({{{rawDatatypeWithEnum}}} {{name}}) {
this.instance.{{name}}({{name}});
return this;
}
{{#openApiNullable}}{{#isNullable}}
public {{classname}}.Builder {{name}}(JsonNullable<{{{datatypeWithEnum}}}> {{name}}) {
public {{classname}}.Builder {{name}}(JsonNullable<{{{rawDatatypeWithEnum}}}> {{name}}) {
this.instance.{{setter}}({{name}});
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ public Pet.Builder photoUrls(List<String> photoUrls) {
return this;
}

public Pet.Builder tags(List<@Valid Tag> tags) {
public Pet.Builder tags(List<Tag> tags) {
this.instance.tags(tags);
return this;
}
Expand Down
Loading
Loading