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

[4947][java]: adds support for validation of primitives in arrays #17165

Merged
Merged
Show file tree
Hide file tree
Changes from 3 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 @@ -21,7 +21,6 @@
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.google.common.base.Strings;
import com.google.common.collect.Sets;
import io.swagger.models.Model;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.Operation;
import io.swagger.v3.oas.models.PathItem;
Expand All @@ -35,6 +34,7 @@
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
import org.openapitools.codegen.*;
import org.openapitools.codegen.languages.features.BeanValidationFeatures;
import org.openapitools.codegen.languages.features.DocumentationProviderFeatures;
import org.openapitools.codegen.meta.features.*;
import org.openapitools.codegen.model.ModelMap;
Expand Down Expand Up @@ -930,7 +930,7 @@ public String getTypeDeclaration(Schema p) {
Schema<?> target = ModelUtils.isGenerateAliasAsModel() ? p : schema;
if (ModelUtils.isArraySchema(target)) {
Schema<?> items = getSchemaItems((ArraySchema) schema);
return getSchemaType(target) + "<" + getTypeDeclaration(items) + ">";
return getSchemaType(target) + "<" + getBeanValidation(items) + getTypeDeclaration(items) + ">";
} else if (ModelUtils.isMapSchema(target)) {
// Note: ModelUtils.isMapSchema(p) returns true when p is a composed schema that also defines
// additionalproperties: true
Expand All @@ -945,6 +945,68 @@ public String getTypeDeclaration(Schema p) {
return super.getTypeDeclaration(target);
}

private String getBeanValidation(Schema<?> items) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor suggestion: add a docstring for this method

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

if (Boolean.FALSE.equals(additionalProperties.getOrDefault(BeanValidationFeatures.USE_BEANVALIDATION, Boolean.FALSE))) {
return "";
}

if (ModelUtils.isTypeObjectSchema(items)) {
// prevents generation '@Valid' for Object
return "";
}

// adds '@Valid' also to response...
// if (items.get$ref() != null) {
// return "@Valid ";
// }
Aliaksie marked this conversation as resolved.
Show resolved Hide resolved

if (StringUtils.isNotEmpty(items.getPattern()) && !ModelUtils.isByteArraySchema(items)) {
return String.format(Locale.ROOT, "@Pattern(regexp = \"%s\")", items.getPattern());
Aliaksie marked this conversation as resolved.
Show resolved Hide resolved
}

if (items.getMinLength() != null && items.getMaxLength() != null) {
return String.format(Locale.ROOT, "@Size(min = %d, max = %d)", items.getMinLength(), items.getMaxLength());
}

if (items.getMinLength() != null) {
return String.format(Locale.ROOT, "@Size(min = %d)", items.getMinLength());
}

if (items.getMaxLength() != null) {
return String.format(Locale.ROOT, "@Size(max = %d)", items.getMaxLength());
}

if (ModelUtils.isLongSchema(items) || ModelUtils.isIntegerSchema(items)) {
if (items.getMinimum() != null && items.getMaximum() != null) {
return String.format(Locale.ROOT, "@Min(%s) @Max(%s)", items.getMinimum(), items.getMaximum());
}

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

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

if (items.getMinimum() != null && items.getMaximum() != null) {
return String.format(Locale.ROOT, "@DecimalMin(value = \"%s\", inclusive = false) @DecimalMax(value = \"%s\", inclusive = false)",
items.getMinimum(),
items.getMaximum());
}

if (items.getMinimum() != null) {
return String.format(Locale.ROOT, "@DecimalMin( value = \"%s\", inclusive = false)", items.getMinimum());
}

if (items.getMaximum() != null) {
return String.format(Locale.ROOT, "@DecimalMax( value = \"%s\", inclusive = false)", items.getMaximum());
}

return "";
}

@Override
public String getAlias(String name) {
if (typeAliases != null && typeAliases.containsKey(name)) {
Expand Down Expand Up @@ -2289,7 +2351,7 @@ protected void handleImplicitHeaders(CodegenOperation operation) {
}
operation.hasParams = !operation.allParams.isEmpty();
}

private boolean shouldBeImplicitHeader(CodegenParameter parameter) {
return StringUtils.isNotBlank(implicitHeadersRegex) && parameter.baseName.matches(implicitHeadersRegex);
}
Expand Down
Loading