Skip to content

Commit

Permalink
[Java] fix beanvalidation compilation failed when items type in array… (
Browse files Browse the repository at this point in the history
#18379)

* [Java] fix beanvalidation compilation failed when items type in array is int64

* [Java] add annotations container bean validation test
  • Loading branch information
fanqiewanzi committed Apr 16, 2024
1 parent 1471e7a commit c7b33c4
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -999,6 +999,10 @@ private String getBeanValidation(Schema<?> items) {
return getNumberBeanValidation(items);
}

if (ModelUtils.isLongSchema(items)) {
return getLongBeanValidation(items);
}

if (ModelUtils.isIntegerSchema(items)) {
return getIntegerBeanValidation(items);
}
Expand All @@ -1021,6 +1025,21 @@ private String getIntegerBeanValidation(Schema<?> items) {
return "";
}

private String getLongBeanValidation(Schema<?> items) {
if (items.getMinimum() != null && items.getMaximum() != null) {
return String.format(Locale.ROOT, "@Min(%sL) @Max(%sL)", items.getMinimum(), items.getMaximum());
}

if (items.getMinimum() != null) {
return String.format(Locale.ROOT, "@Min(%sL)", items.getMinimum());
}

if (items.getMaximum() != null) {
return String.format(Locale.ROOT, "@Max(%sL)", items.getMaximum());
}
return "";
}

private String getNumberBeanValidation(Schema<?> items) {
if (items.getMinimum() != null && items.getMaximum() != null) {
return String.format(Locale.ROOT, "@DecimalMin(value = \"%s\", inclusive = %s) @DecimalMax(value = \"%s\", inclusive = %s)",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@

import io.swagger.v3.oas.models.parameters.Parameter;
import io.swagger.v3.parser.core.models.ParseOptions;

import java.math.BigDecimal;
import java.time.OffsetDateTime;
import java.time.ZonedDateTime;
import java.util.*;
Expand Down Expand Up @@ -945,6 +947,93 @@ public void ignoreBeanValidationAnnotationsContainerTest() {
Assert.assertEquals(defaultValue, "List<File>");
}

@Test
public void AnnotationsContainerTest() {
final P_AbstractJavaCodegen codegen = new P_AbstractJavaCodegen();
codegen.additionalProperties().put("useBeanValidation", true);

// 1. string type
Schema<?> schema = new ArraySchema().items(new Schema<>().type("string").pattern("^[a-z]$").minLength(0).maxLength(36));
String defaultValue = codegen.getTypeDeclaration(schema);
Assert.assertEquals(defaultValue, "List<@Pattern(regexp = \"^[a-z]$\")@Size(min = 0, max = 36)String>");

schema = new ArraySchema().items(new Schema<>().type("string").pattern("^[a-z]$").minLength(0));
defaultValue = codegen.getTypeDeclaration(schema);
Assert.assertEquals(defaultValue, "List<@Pattern(regexp = \"^[a-z]$\")@Size(min = 0)String>");

schema = new ArraySchema().items(new Schema<>().type("string").pattern("^[a-z]$").maxLength(36));
defaultValue = codegen.getTypeDeclaration(schema);
Assert.assertEquals(defaultValue, "List<@Pattern(regexp = \"^[a-z]$\")@Size(max = 36)String>");

schema = new ArraySchema().items(new Schema<>().type("string").format("email"));
defaultValue = codegen.getTypeDeclaration(schema);
Assert.assertEquals(defaultValue, "List<@Email String>");

// 2. string type with number format
schema = new ArraySchema().items(new Schema<>().type("string").format("number").minimum(BigDecimal.ZERO).maximum(BigDecimal.TEN).exclusiveMinimum(Boolean.TRUE).exclusiveMaximum(Boolean.TRUE));
defaultValue = codegen.getTypeDeclaration(schema);
Assert.assertEquals(defaultValue, "List<@DecimalMin(value = \"0\", inclusive = false) @DecimalMax(value = \"10\", inclusive = false)BigDecimal>");

schema = new ArraySchema().items(new Schema<>().type("string").format("number").minimum(BigDecimal.ZERO).exclusiveMinimum(Boolean.TRUE));
defaultValue = codegen.getTypeDeclaration(schema);
Assert.assertEquals(defaultValue, "List<@DecimalMin( value = \"0\", inclusive = false)BigDecimal>");

schema = new ArraySchema().items(new Schema<>().type("string").format("number").maximum(BigDecimal.TEN).exclusiveMaximum(Boolean.TRUE));
defaultValue = codegen.getTypeDeclaration(schema);
Assert.assertEquals(defaultValue, "List<@DecimalMax( value = \"10\", inclusive = false)BigDecimal>");

// 3. number type
schema = new ArraySchema().items(new Schema<>().type("number").minimum(BigDecimal.ZERO).maximum(BigDecimal.TEN).exclusiveMinimum(Boolean.TRUE).exclusiveMaximum(Boolean.TRUE));
defaultValue = codegen.getTypeDeclaration(schema);
Assert.assertEquals(defaultValue, "List<@DecimalMin(value = \"0\", inclusive = false) @DecimalMax(value = \"10\", inclusive = false)BigDecimal>");

schema = new ArraySchema().items(new Schema<>().type("number").minimum(BigDecimal.ZERO).exclusiveMinimum(Boolean.TRUE));
defaultValue = codegen.getTypeDeclaration(schema);
Assert.assertEquals(defaultValue, "List<@DecimalMin( value = \"0\", inclusive = false)BigDecimal>");

schema = new ArraySchema().items(new Schema<>().type("number").maximum(BigDecimal.TEN).exclusiveMaximum(Boolean.TRUE));
defaultValue = codegen.getTypeDeclaration(schema);
Assert.assertEquals(defaultValue, "List<@DecimalMax( value = \"10\", inclusive = false)BigDecimal>");

schema = new ArraySchema().items(new Schema<>().type("number").minimum(BigDecimal.ZERO).maximum(BigDecimal.TEN));
defaultValue = codegen.getTypeDeclaration(schema);
Assert.assertEquals(defaultValue, "List<@DecimalMin(value = \"0\", inclusive = true) @DecimalMax(value = \"10\", inclusive = true)BigDecimal>");

schema = new ArraySchema().items(new Schema<>().type("number").minimum(BigDecimal.ZERO));
defaultValue = codegen.getTypeDeclaration(schema);
Assert.assertEquals(defaultValue, "List<@DecimalMin( value = \"0\", inclusive = true)BigDecimal>");

schema = new ArraySchema().items(new Schema<>().type("number").maximum(BigDecimal.TEN));
defaultValue = codegen.getTypeDeclaration(schema);
Assert.assertEquals(defaultValue, "List<@DecimalMax( value = \"10\", inclusive = true)BigDecimal>");

// 4. integer type with int64 format
schema = new ArraySchema().items(new Schema<>().type("integer").format("int64").minimum(BigDecimal.ZERO).maximum(BigDecimal.TEN));
defaultValue = codegen.getTypeDeclaration(schema);
Assert.assertEquals(defaultValue, "List<@Min(0L) @Max(10L)Long>");

schema = new ArraySchema().items(new Schema<>().type("integer").format("int64").minimum(BigDecimal.ZERO));
defaultValue = codegen.getTypeDeclaration(schema);
Assert.assertEquals(defaultValue, "List<@Min(0L)Long>");

schema = new ArraySchema().items(new Schema<>().type("integer").format("int64").maximum(BigDecimal.TEN));
defaultValue = codegen.getTypeDeclaration(schema);
Assert.assertEquals(defaultValue, "List<@Max(10L)Long>");

// 5. integer type
schema = new ArraySchema().items(new Schema<>().type("integer").minimum(BigDecimal.ZERO).maximum(BigDecimal.TEN));
defaultValue = codegen.getTypeDeclaration(schema);
Assert.assertEquals(defaultValue, "List<@Min(0) @Max(10)Integer>");

schema = new ArraySchema().items(new Schema<>().type("integer").minimum(BigDecimal.ZERO));
defaultValue = codegen.getTypeDeclaration(schema);
Assert.assertEquals(defaultValue, "List<@Min(0)Integer>");

schema = new ArraySchema().items(new Schema<>().type("integer").maximum(BigDecimal.TEN));
defaultValue = codegen.getTypeDeclaration(schema);
Assert.assertEquals(defaultValue, "List<@Max(10)Integer>");
}

private static Schema<?> createObjectSchemaWithMinItems() {
return new ObjectSchema()
.addProperties("id", new IntegerSchema().format("int32"))
Expand Down

0 comments on commit c7b33c4

Please sign in to comment.