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] fix beanvalidation compilation failed when items type in array is int64 #18379

Merged
merged 2 commits into from
Apr 16, 2024
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 @@ -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
Loading