Skip to content

Commit

Permalink
[Java] Remove bean validation annotations on builder (#19580)
Browse files Browse the repository at this point in the history
* Revert #18724

Make JAX-RS spec BuilderImpl class final again

* Remove bean validation annotations from builder

* Also update other java generators

* Introduce removeAnnotations lambda

* Undo import changes to JavaHelidonClientCodegen

* Add javadoc comment, examples and test for AbstractJavaCodegen.removeAnnotations()
  • Loading branch information
jpraet authored Jan 7, 2025
1 parent 00c76de commit eb96380
Show file tree
Hide file tree
Showing 167 changed files with 233 additions and 208 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -657,6 +657,9 @@ public void processOpts() {
}
writer.write(content);
});
additionalProperties.put("removeAnnotations", (Mustache.Lambda) (fragment, writer) -> {
writer.write(removeAnnotations(fragment.execute()));
});
}

/**
Expand Down Expand Up @@ -1802,18 +1805,30 @@ 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 = removeAnnotations(property.dataType);
response.dataType = removeAnnotations(response.dataType);
}

private String removeAnnotations(String type) {
return type.replaceAll("(?:(?i)@[a-z0-9]*+([(].*[)]|\\s*))*+", "");
/**
* Remove annotations from the given data type string.
*
* For example:
* <ul>
* <li>{@code @Min(0) @Max(10)Integer} -> {@code Integer}</li>
* <li>{@code @Pattern(regexp = "^[a-z]$")String>} -> {@code String}</li>
* <li>{@code List<@Pattern(regexp = "^[a-z]$")String>}" -> "{@code List<String>}"</li>
* <li>{@code List<@Valid Pet>}" -> "{@code List<Pet>}"</li>
* </ul>
*
* @param dataType the data type string
* @return the data type string without annotations
*/
public String removeAnnotations(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 @@ -14,30 +14,30 @@ public static class Builder {{#parentModel}}extends {{classname}}.Builder {{/par
}

{{#vars}}
public {{classname}}.Builder {{name}}({{{datatypeWithEnum}}} {{name}}) {
public {{classname}}.Builder {{name}}({{#removeAnnotations}}{{{datatypeWithEnum}}}{{/removeAnnotations}} {{name}}) {
{{#vendorExtensions.x-is-jackson-optional-nullable}}
this.instance.{{name}} = JsonNullable.<{{{datatypeWithEnum}}}>of({{name}});
this.instance.{{name}} = JsonNullable.<{{#removeAnnotations}}{{{datatypeWithEnum}}}{{/removeAnnotations}}>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<{{#removeAnnotations}}{{{datatypeWithEnum}}}{{/removeAnnotations}}> {{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}}({{#removeAnnotations}}{{{datatypeWithEnum}}}{{/removeAnnotations}} {{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<{{#removeAnnotations}}{{{datatypeWithEnum}}}{{/removeAnnotations}}> {{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 {{#removeAnnotations}}{{{datatypeWithEnum}}}{{/removeAnnotations}} {{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}}({{#removeAnnotations}}{{{datatypeWithEnum}}}{{/removeAnnotations}} {{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}}({{#removeAnnotations}}{{{datatypeWithEnum}}}{{/removeAnnotations}} {{name}}) {
this.instance.{{name}}({{name}});
return this;
}
{{#openApiNullable}}{{#isNullable}}
public {{classname}}.Builder {{name}}(JsonNullable<{{{datatypeWithEnum}}}> {{name}}) {
public {{classname}}.Builder {{name}}(JsonNullable<{{#removeAnnotations}}{{{datatypeWithEnum}}}{{/removeAnnotations}}> {{name}}) {
this.instance.{{name}} = {{name}};
return this;
}
{{/isNullable}}{{/openApiNullable}}
{{/vars}}
{{#parentVars}}
@Override
public {{classname}}.Builder {{name}}({{{datatypeWithEnum}}} {{name}}) {
public {{classname}}.Builder {{name}}({{#removeAnnotations}}{{{datatypeWithEnum}}}{{/removeAnnotations}} {{name}}) {
this.instance.{{name}}({{name}});
return this;
}
{{#openApiNullable}}{{#isNullable}}
public {{classname}}.Builder {{name}}(JsonNullable<{{{datatypeWithEnum}}}> {{name}}) {
public {{classname}}.Builder {{name}}(JsonNullable<{{#removeAnnotations}}{{{datatypeWithEnum}}}{{/removeAnnotations}}> {{name}}) {
this.instance.{{setter}}({{name}});
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -955,4 +955,14 @@ public void AnnotationsContainerTest() {
defaultValue = codegen.getTypeDeclaration(schema);
Assert.assertEquals(defaultValue, "List<@Max(10)Integer>");
}

@Test
public void removeAnnotationsTest() {
Assert.assertEquals(codegen.removeAnnotations("@Min(0) @Max(10)Integer"), "Integer");
Assert.assertEquals(codegen.removeAnnotations("@Pattern(regexp = \"^[a-z]$\")String"), "String");
Assert.assertEquals(codegen.removeAnnotations("List<@Min(0) @Max(10)Integer>"), "List<Integer>");
Assert.assertEquals(codegen.removeAnnotations("List<@Pattern(regexp = \"^[a-z]$\")String>"), "List<String>");
Assert.assertEquals(codegen.removeAnnotations("List<@Valid Pet>"), "List<Pet>");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,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
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ private String toIndentedString(Object o) {
return new AdditionalPropertiesClassBuilderImpl();
}

private static class AdditionalPropertiesClassBuilderImpl extends AdditionalPropertiesClassBuilder<AdditionalPropertiesClass, AdditionalPropertiesClassBuilderImpl> {
private static final class AdditionalPropertiesClassBuilderImpl extends AdditionalPropertiesClassBuilder<AdditionalPropertiesClass, AdditionalPropertiesClassBuilderImpl> {

@Override
protected AdditionalPropertiesClassBuilderImpl self() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ private String toIndentedString(Object o) {
return new AnimalBuilderImpl();
}

private static class AnimalBuilderImpl extends AnimalBuilder<Animal, AnimalBuilderImpl> {
private static final class AnimalBuilderImpl extends AnimalBuilder<Animal, AnimalBuilderImpl> {

@Override
protected AnimalBuilderImpl self() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ private String toIndentedString(Object o) {
return new ArrayOfArrayOfNumberOnlyBuilderImpl();
}

private static class ArrayOfArrayOfNumberOnlyBuilderImpl extends ArrayOfArrayOfNumberOnlyBuilder<ArrayOfArrayOfNumberOnly, ArrayOfArrayOfNumberOnlyBuilderImpl> {
private static final class ArrayOfArrayOfNumberOnlyBuilderImpl extends ArrayOfArrayOfNumberOnlyBuilder<ArrayOfArrayOfNumberOnly, ArrayOfArrayOfNumberOnlyBuilderImpl> {

@Override
protected ArrayOfArrayOfNumberOnlyBuilderImpl self() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ private String toIndentedString(Object o) {
return new ArrayOfNumberOnlyBuilderImpl();
}

private static class ArrayOfNumberOnlyBuilderImpl extends ArrayOfNumberOnlyBuilder<ArrayOfNumberOnly, ArrayOfNumberOnlyBuilderImpl> {
private static final class ArrayOfNumberOnlyBuilderImpl extends ArrayOfNumberOnlyBuilder<ArrayOfNumberOnly, ArrayOfNumberOnlyBuilderImpl> {

@Override
protected ArrayOfNumberOnlyBuilderImpl self() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ private String toIndentedString(Object o) {
return new ArrayTestBuilderImpl();
}

private static class ArrayTestBuilderImpl extends ArrayTestBuilder<ArrayTest, ArrayTestBuilderImpl> {
private static final class ArrayTestBuilderImpl extends ArrayTestBuilder<ArrayTest, ArrayTestBuilderImpl> {

@Override
protected ArrayTestBuilderImpl self() {
Expand All @@ -204,7 +204,7 @@ public ArrayTest build() {
public static abstract class ArrayTestBuilder<C extends ArrayTest, B extends ArrayTestBuilder<C, B>> {
private List<String> arrayOfString = new ArrayList<>();
private List<List<Long>> arrayArrayOfInteger = new ArrayList<>();
private List<List<@Valid ReadOnlyFirst>> arrayArrayOfModel = new ArrayList<>();
private List<List<ReadOnlyFirst>> arrayArrayOfModel = new ArrayList<>();
protected abstract B self();

public abstract C build();
Expand All @@ -217,7 +217,7 @@ public B arrayArrayOfInteger(List<List<Long>> arrayArrayOfInteger) {
this.arrayArrayOfInteger = arrayArrayOfInteger;
return self();
}
public B arrayArrayOfModel(List<List<@Valid ReadOnlyFirst>> arrayArrayOfModel) {
public B arrayArrayOfModel(List<List<ReadOnlyFirst>> arrayArrayOfModel) {
this.arrayArrayOfModel = arrayArrayOfModel;
return self();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ private String toIndentedString(Object o) {
return new BigCatBuilderImpl();
}

private static class BigCatBuilderImpl extends BigCatBuilder<BigCat, BigCatBuilderImpl> {
private static final class BigCatBuilderImpl extends BigCatBuilder<BigCat, BigCatBuilderImpl> {

@Override
protected BigCatBuilderImpl self() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ private String toIndentedString(Object o) {
return new CapitalizationBuilderImpl();
}

private static class CapitalizationBuilderImpl extends CapitalizationBuilder<Capitalization, CapitalizationBuilderImpl> {
private static final class CapitalizationBuilderImpl extends CapitalizationBuilder<Capitalization, CapitalizationBuilderImpl> {

@Override
protected CapitalizationBuilderImpl self() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ private String toIndentedString(Object o) {
return new CatBuilderImpl();
}

private static class CatBuilderImpl extends CatBuilder<Cat, CatBuilderImpl> {
private static final class CatBuilderImpl extends CatBuilder<Cat, CatBuilderImpl> {

@Override
protected CatBuilderImpl self() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ private String toIndentedString(Object o) {
return new CategoryBuilderImpl();
}

private static class CategoryBuilderImpl extends CategoryBuilder<Category, CategoryBuilderImpl> {
private static final class CategoryBuilderImpl extends CategoryBuilder<Category, CategoryBuilderImpl> {

@Override
protected CategoryBuilderImpl self() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ private String toIndentedString(Object o) {
return new ClassModelBuilderImpl();
}

private static class ClassModelBuilderImpl extends ClassModelBuilder<ClassModel, ClassModelBuilderImpl> {
private static final class ClassModelBuilderImpl extends ClassModelBuilder<ClassModel, ClassModelBuilderImpl> {

@Override
protected ClassModelBuilderImpl self() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ private String toIndentedString(Object o) {
return new ClientBuilderImpl();
}

private static class ClientBuilderImpl extends ClientBuilder<Client, ClientBuilderImpl> {
private static final class ClientBuilderImpl extends ClientBuilder<Client, ClientBuilderImpl> {

@Override
protected ClientBuilderImpl self() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ private String toIndentedString(Object o) {
return new DogBuilderImpl();
}

private static class DogBuilderImpl extends DogBuilder<Dog, DogBuilderImpl> {
private static final class DogBuilderImpl extends DogBuilder<Dog, DogBuilderImpl> {

@Override
protected DogBuilderImpl self() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ private String toIndentedString(Object o) {
return new EnumArraysBuilderImpl();
}

private static class EnumArraysBuilderImpl extends EnumArraysBuilder<EnumArrays, EnumArraysBuilderImpl> {
private static final class EnumArraysBuilderImpl extends EnumArraysBuilder<EnumArrays, EnumArraysBuilderImpl> {

@Override
protected EnumArraysBuilderImpl self() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ private String toIndentedString(Object o) {
return new EnumTestBuilderImpl();
}

private static class EnumTestBuilderImpl extends EnumTestBuilder<EnumTest, EnumTestBuilderImpl> {
private static final class EnumTestBuilderImpl extends EnumTestBuilder<EnumTest, EnumTestBuilderImpl> {

@Override
protected EnumTestBuilderImpl self() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ private String toIndentedString(Object o) {
return new FileSchemaTestClassBuilderImpl();
}

private static class FileSchemaTestClassBuilderImpl extends FileSchemaTestClassBuilder<FileSchemaTestClass, FileSchemaTestClassBuilderImpl> {
private static final class FileSchemaTestClassBuilderImpl extends FileSchemaTestClassBuilder<FileSchemaTestClass, FileSchemaTestClassBuilderImpl> {

@Override
protected FileSchemaTestClassBuilderImpl self() {
Expand All @@ -148,7 +148,7 @@ public FileSchemaTestClass build() {

public static abstract class FileSchemaTestClassBuilder<C extends FileSchemaTestClass, B extends FileSchemaTestClassBuilder<C, B>> {
private ModelFile _file;
private List<@Valid ModelFile> files = new ArrayList<>();
private List<ModelFile> files = new ArrayList<>();
protected abstract B self();

public abstract C build();
Expand All @@ -157,7 +157,7 @@ public B _file(ModelFile _file) {
this._file = _file;
return self();
}
public B files(List<@Valid ModelFile> files) {
public B files(List<ModelFile> files) {
this.files = files;
return self();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ private String toIndentedString(Object o) {
return new FormatTestBuilderImpl();
}

private static class FormatTestBuilderImpl extends FormatTestBuilder<FormatTest, FormatTestBuilderImpl> {
private static final class FormatTestBuilderImpl extends FormatTestBuilder<FormatTest, FormatTestBuilderImpl> {

@Override
protected FormatTestBuilderImpl self() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ private String toIndentedString(Object o) {
return new HasOnlyReadOnlyBuilderImpl();
}

private static class HasOnlyReadOnlyBuilderImpl extends HasOnlyReadOnlyBuilder<HasOnlyReadOnly, HasOnlyReadOnlyBuilderImpl> {
private static final class HasOnlyReadOnlyBuilderImpl extends HasOnlyReadOnlyBuilder<HasOnlyReadOnly, HasOnlyReadOnlyBuilderImpl> {

@Override
protected HasOnlyReadOnlyBuilderImpl self() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ private String toIndentedString(Object o) {
return new MapTestBuilderImpl();
}

private static class MapTestBuilderImpl extends MapTestBuilder<MapTest, MapTestBuilderImpl> {
private static final class MapTestBuilderImpl extends MapTestBuilder<MapTest, MapTestBuilderImpl> {

@Override
protected MapTestBuilderImpl self() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ private String toIndentedString(Object o) {
return new MixedPropertiesAndAdditionalPropertiesClassBuilderImpl();
}

private static class MixedPropertiesAndAdditionalPropertiesClassBuilderImpl extends MixedPropertiesAndAdditionalPropertiesClassBuilder<MixedPropertiesAndAdditionalPropertiesClass, MixedPropertiesAndAdditionalPropertiesClassBuilderImpl> {
private static final class MixedPropertiesAndAdditionalPropertiesClassBuilderImpl extends MixedPropertiesAndAdditionalPropertiesClassBuilder<MixedPropertiesAndAdditionalPropertiesClass, MixedPropertiesAndAdditionalPropertiesClassBuilderImpl> {

@Override
protected MixedPropertiesAndAdditionalPropertiesClassBuilderImpl self() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ private String toIndentedString(Object o) {
return new Model200ResponseBuilderImpl();
}

private static class Model200ResponseBuilderImpl extends Model200ResponseBuilder<Model200Response, Model200ResponseBuilderImpl> {
private static final class Model200ResponseBuilderImpl extends Model200ResponseBuilder<Model200Response, Model200ResponseBuilderImpl> {

@Override
protected Model200ResponseBuilderImpl self() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ private String toIndentedString(Object o) {
return new ModelApiResponseBuilderImpl();
}

private static class ModelApiResponseBuilderImpl extends ModelApiResponseBuilder<ModelApiResponse, ModelApiResponseBuilderImpl> {
private static final class ModelApiResponseBuilderImpl extends ModelApiResponseBuilder<ModelApiResponse, ModelApiResponseBuilderImpl> {

@Override
protected ModelApiResponseBuilderImpl self() {
Expand Down
Loading

0 comments on commit eb96380

Please sign in to comment.