[Java] fix beanvalidation compilation failed when items type in array… (#18379)

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

* [Java] add annotations container bean validation test
This commit is contained in:
weirdo
2024-04-16 12:30:39 +08:00
committed by GitHub
parent 1471e7a0d0
commit c7b33c4762
2 changed files with 108 additions and 0 deletions

View File

@@ -999,6 +999,10 @@ public abstract class AbstractJavaCodegen extends DefaultCodegen implements Code
return getNumberBeanValidation(items);
}
if (ModelUtils.isLongSchema(items)) {
return getLongBeanValidation(items);
}
if (ModelUtils.isIntegerSchema(items)) {
return getIntegerBeanValidation(items);
}
@@ -1021,6 +1025,21 @@ public abstract class AbstractJavaCodegen extends DefaultCodegen implements Code
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)",

View File

@@ -24,6 +24,8 @@ import io.swagger.v3.oas.models.media.*;
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.*;
@@ -945,6 +947,93 @@ public class AbstractJavaCodegenTest {
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"))