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

Add support validation annotations extensions. #1789

Merged
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 @@ -156,20 +156,34 @@ private static String genericAnnotations(CodegenProperty prop, boolean isGenerat
return result.toString();
}

var patternMsg = (String) prop.vendorExtensions.get("x-pattern-message");
var sizeMsg = (String) prop.vendorExtensions.get("x-size-message");
var notNullMsg = (String) prop.vendorExtensions.get("x-not-null-message");
var minMsg = (String) prop.vendorExtensions.get("x-min-message");
var maxMsg = (String) prop.vendorExtensions.get("x-max-message");

if (StringUtils.isNotEmpty(prop.pattern) && !prop.isDate && !prop.isDateTime) {
if (prop.isEmail) {
if ("email".equals(type) || "email".equalsIgnoreCase(prop.dataFormat) || prop.isEmail) {
result.append("@Email(regexp = \"");
} else {
result.append("@Pattern(regexp = \"");
}
result.append(prop.pattern).append("\") ");
result.append(prop.pattern).append("\"");
if (patternMsg != null) {
result.append(", message = \"").append(patternMsg).append("\"");
}
result.append(") ");
}

var containsNotEmpty = false;

if (prop.minLength != null || prop.maxLength != null) {
if (prop.minLength != null && prop.minLength == 1 && prop.maxLength == null && !prop.isNullable) {
result.append("@NotEmpty ");
result.append("@NotEmpty");
if (sizeMsg != null) {
result.append("(message = \"").append(sizeMsg).append("\")");
}
result.append(' ');
containsNotEmpty = true;
} else {
result.append("@Size(");
Expand All @@ -182,6 +196,9 @@ private static String genericAnnotations(CodegenProperty prop, boolean isGenerat
}
result.append("max = ").append(prop.maxLength);
}
if (sizeMsg != null) {
result.append(", message = \"").append(sizeMsg).append('"');
}
result.append(") ");
}
}
Expand All @@ -201,6 +218,9 @@ private static String genericAnnotations(CodegenProperty prop, boolean isGenerat
}
result.append("max = ").append(prop.maxItems);
}
if (sizeMsg != null) {
result.append(", message = \"").append(sizeMsg).append('"');
}
result.append(") ");
}
}
Expand All @@ -212,7 +232,11 @@ private static String genericAnnotations(CodegenProperty prop, boolean isGenerat
result.append("@Nullable ");
}
} else if (!containsNotEmpty) {
result.append("@NotNull ");
result.append("@NotNull");
if (notNullMsg != null) {
result.append("(message = \"").append(notNullMsg).append("\")");
}
result.append(" ");
}
}
if (StringUtils.isNotEmpty(prop.minimum)) {
Expand All @@ -222,21 +246,43 @@ private static String genericAnnotations(CodegenProperty prop, boolean isGenerat
longNumber++;
}
if (longNumber == 0 && StringUtils.isEmpty(prop.maximum)) {
result.append("@PositiveOrZero ");
result.append("@PositiveOrZero");
if (minMsg != null) {
result.append("(message = \"").append(minMsg).append("\")");
}
result.append(" ");
} else if (longNumber == 1 && StringUtils.isEmpty(prop.maximum)) {
result.append("@Positive ");
result.append("@Positive");
if (minMsg != null) {
result.append("(message = \"").append(minMsg).append("\")");
}
result.append(" ");
} else {
result.append("@Min(").append(longNumber).append(") ");
result.append("@Min(");
if (minMsg != null) {
result.append("value = ");
}
result.append(longNumber);
if (prop.isLong) {
result.append("L");
}
if (minMsg != null) {
result.append(", message = \"").append(minMsg).append("\"");
}
result.append(") ");
}
} catch (Exception e) {
result.append("@DecimalMin(");
if (prop.exclusiveMinimum) {
if (prop.exclusiveMinimum || minMsg != null) {
result.append("value = ");
}
result.append('"').append(prop.minimum).append('"');
if (prop.exclusiveMinimum) {
result.append(", inclusive = false");
}
if (minMsg != null) {
result.append(", message = \"").append(minMsg).append("\"");
}
result.append(") ");
}
}
Expand All @@ -247,21 +293,44 @@ private static String genericAnnotations(CodegenProperty prop, boolean isGenerat
longNumber--;
}
if (longNumber == 0 && StringUtils.isEmpty(prop.minimum)) {
result.append("@NegativeOrZero ");
result.append("@NegativeOrZero");
if (maxMsg != null) {
result.append("(message = \"").append(maxMsg).append("\")");
}
result.append(" ");
} else if (longNumber == -1 && StringUtils.isEmpty(prop.minimum)) {
result.append("@Negative ");
result.append("@Negative");
if (maxMsg != null) {
result.append("(message = \"").append(maxMsg).append("\")");
}
result.append(" ");
} else {
result.append("@Max(").append(longNumber).append(") ");
result.append("@Max(");
if (maxMsg != null) {
result.append("value = ");
}
result.append(longNumber);
if (prop.isLong) {
result.append("L");
}
if (maxMsg != null) {
result.append(", message = \"").append(maxMsg).append("\"");
}
result.append(") ");

}
} catch (Exception e) {
result.append("@DecimalMax(");
if (prop.exclusiveMaximum) {
if (prop.exclusiveMaximum || maxMsg != null) {
result.append("value = ");
}
result.append('"').append(prop.maximum).append('"');
if (prop.exclusiveMaximum) {
result.append(", inclusive = false");
}
if (maxMsg != null) {
result.append(", message = \"").append(maxMsg).append("\"");
}
result.append(") ");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
{{^isNullable}}
{{#required}}
{{^isReadOnly}}
@NotNull
@NotNull{{#vendorExtensions.x-not-null-message}}(message = "{{{.}}}"){{/vendorExtensions.x-not-null-message}}
{{/isReadOnly}}
{{/required}}
{{/isNullable}}
Expand All @@ -51,62 +51,62 @@
{{!Pattern}}
{{#pattern}}
{{^isByteArray}}{{^isDate}}{{^isDateTime}}{{^isEmail}}
@Pattern(regexp = "{{{pattern}}}")
@Pattern(regexp = "{{{pattern}}}"{{#vendorExtensions.x-pattern-message}}, message = "{{{.}}}"{{/vendorExtensions.x-pattern-message}})
{{/isEmail}}{{/isDateTime}}{{/isDate}}{{/isByteArray}}
{{/pattern}}
{{!Min length && max length}}
{{#minLength}}
{{#maxLength}}
@Size(min = {{minLength}}, max = {{maxLength}})
@Size(min = {{minLength}}, max = {{maxLength}}{{#vendorExtensions.x-size-message}}, message = "{{{.}}}"{{/vendorExtensions.x-size-message}})
{{/maxLength}}
{{/minLength}}
{{#minLength}}{{^maxLength}}
@Size(min = {{minLength}})
@Size(min = {{minLength}}{{#vendorExtensions.x-size-message}}, message = "{{{.}}}"{{/vendorExtensions.x-size-message}})
{{/maxLength}}{{/minLength}}
{{^minLength}}{{#maxLength}}
@Size(max = {{maxLength}})
@Size(max = {{maxLength}}{{#vendorExtensions.x-size-message}}, message = "{{{.}}}"{{/vendorExtensions.x-size-message}})
{{/maxLength}}{{/minLength}}
{{!Size}}
{{#minItems}}{{#maxItems}}
@Size(min = {{minItems}}, max = {{maxItems}})
@Size(min = {{minItems}}, max = {{maxItems}}{{#vendorExtensions.x-size-message}}, message = "{{{.}}}"{{/vendorExtensions.x-size-message}})
{{/maxItems}}{{/minItems}}
{{#minItems}}{{^maxItems}}
@Size(min = {{minItems}})
@Size(min = {{minItems}}{{#vendorExtensions.x-size-message}}, message = "{{{.}}}"{{/vendorExtensions.x-size-message}})
{{/maxItems}}{{/minItems}}
{{^minItems}}{{#maxItems}}
@Size(max = {{maxItems}})
@Size(max = {{maxItems}}{{#vendorExtensions.x-size-message}}, message = "{{{.}}}"{{/vendorExtensions.x-size-message}})
{{/maxItems}}{{/minItems}}
{{!Email}}
{{#isEmail}}
@Email{{#pattern}}(pattern = "{{{pattern}}}"){{/pattern}}
@Email{{#pattern}}(regexp = "{{{pattern}}}"{{#vendorExtensions.x-pattern-message}}, message = "{{{.}}}"{{/vendorExtensions.x-pattern-message}}){{/pattern}}{{^pattern}}{{#vendorExtensions.x-pattern-message}}(message = "{{{.}}}"){{/vendorExtensions.x-pattern-message}}{{/pattern}}
{{/isEmail}}
{{!check for integer or long / all others=decimal type with @Decimal isInteger set}}
{{#isInteger}}
{{#minimum}}
@Min({{minimum}})
@Min({{#vendorExtensions.x-min-message}}value = {{/vendorExtensions.x-min-message}}{{{.}}}{{#vendorExtensions.x-min-message}}, message = "{{{.}}}"{{/vendorExtensions.x-min-message}})
{{/minimum}}
{{#maximum}}
@Max({{maximum}})
@Max({{#vendorExtensions.x-max-message}}value = {{/vendorExtensions.x-max-message}}{{{.}}}{{#vendorExtensions.x-max-message}}, message = "{{{.}}}"{{/vendorExtensions.x-max-message}})
{{/maximum}}
{{/isInteger}}
{{!isLong set}}
{{#isLong}}
{{#minimum}}
@Min({{minimum}}L)
@Min({{#vendorExtensions.x-min-message}}value = {{/vendorExtensions.x-min-message}}{{{.}}}L{{#vendorExtensions.x-min-message}}, message = "{{{.}}}"{{/vendorExtensions.x-min-message}})
{{/minimum}}
{{#maximum}}
@Max({{maximum}}L)
@Max({{#vendorExtensions.x-max-message}}value = {{/vendorExtensions.x-max-message}}{{{.}}}L{{#vendorExtensions.x-max-message}}, message = "{{{.}}}"{{/vendorExtensions.x-max-message}})
{{/maximum}}
{{/isLong}}
{{!Not Integer, not Long => we have a decimal value!}}
{{^isInteger}}
{{^isLong}}{{!minimum for decimal value}}
{{#minimum}}
@DecimalMin({{#exclusiveMinimum}}value = {{/exclusiveMinimum}}"{{minimum}}"{{#exclusiveMinimum}}, inclusive = false{{/exclusiveMinimum}})
@DecimalMin({{#exclusiveMinimum}}value = {{/exclusiveMinimum}}{{^exclusiveMinimum}}{{#vendorExtensions.x-min-message}}value = {{/vendorExtensions.x-min-message}}{{/exclusiveMinimum}}"{{minimum}}"{{#exclusiveMinimum}}, inclusive = false{{/exclusiveMinimum}}{{#vendorExtensions.x-min-message}}, message = "{{{.}}}"{{/vendorExtensions.x-min-message}})
{{/minimum}}
{{!maximal for decimal value}}
{{#maximum}}
@DecimalMax({{#exclusiveMaximum}}value = {{/exclusiveMaximum}}"{{maximum}}"{{#exclusiveMaximum}}, inclusive = false{{/exclusiveMaximum}})
@DecimalMax({{#exclusiveMaximum}}value = {{/exclusiveMaximum}}{{^exclusiveMaximum}}{{#vendorExtensions.x-max-message}}value = {{/vendorExtensions.x-max-message}}{{/exclusiveMaximum}}"{{maximum}}"{{#exclusiveMaximum}}, inclusive = false{{/exclusiveMaximum}}{{#vendorExtensions.x-max-message}}, message = "{{{.}}}"{{/vendorExtensions.x-max-message}})
{{/maximum}}
{{/isLong}}
{{/isInteger}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
{{^isNullable}}
{{#required}}
{{^isReadOnly}}
@NotNull
@NotNull{{#vendorExtensions.x-not-null-message}}(message = "{{{.}}}"){{/vendorExtensions.x-not-null-message}}
{{/isReadOnly}}
{{/required}}
{{/isNullable}}
Expand All @@ -30,63 +30,63 @@
{{/isContainer}}
{{!Pattern}}
{{#pattern}}
{{^isByteArray}}{{^isDate}}{{^isDateTime}}
@Pattern(regexp = "{{{pattern}}}")
{{/isDateTime}}{{/isDate}}{{/isByteArray}}
{{^isEmail}}{{^isByteArray}}{{^isDate}}{{^isDateTime}}
@Pattern(regexp = "{{{pattern}}}"{{#vendorExtensions.x-pattern-message}}, message = "{{{.}}}"{{/vendorExtensions.x-pattern-message}})
{{/isDateTime}}{{/isDate}}{{/isByteArray}}{{/isEmail}}
{{/pattern}}
{{!Min length && max length}}
{{#minLength}}
{{#maxLength}}
@Size(min = {{minLength}}, max = {{maxLength}})
@Size(min = {{minLength}}, max = {{maxLength}}{{#vendorExtensions.x-size-message}}, message = "{{{.}}}"{{/vendorExtensions.x-size-message}})
{{/maxLength}}
{{/minLength}}
{{#minLength}}{{^maxLength}}
@Size(min = {{minLength}})
@Size(min = {{minLength}}{{#vendorExtensions.x-size-message}}, message = "{{{.}}}"{{/vendorExtensions.x-size-message}})
{{/maxLength}}{{/minLength}}
{{^minLength}}{{#maxLength}}
@Size(max = {{maxLength}})
@Size(max = {{maxLength}}{{#vendorExtensions.x-size-message}}, message = "{{{.}}}"{{/vendorExtensions.x-size-message}})
{{/maxLength}}{{/minLength}}
{{!Size}}
{{#minItems}}{{#maxItems}}
@Size(min = {{minItems}}, max = {{maxItems}})
@Size(min = {{minItems}}, max = {{maxItems}}{{#vendorExtensions.x-size-message}}, message = "{{{.}}}"{{/vendorExtensions.x-size-message}})
{{/maxItems}}{{/minItems}}
{{#minItems}}{{^maxItems}}
@Size(min = {{minItems}})
@Size(min = {{minItems}}{{#vendorExtensions.x-size-message}}, message = "{{{.}}}"{{/vendorExtensions.x-size-message}})
{{/maxItems}}{{/minItems}}
{{^minItems}}{{#maxItems}}
@Size(max = {{maxItems}})
@Size(max = {{maxItems}}{{#vendorExtensions.x-size-message}}, message = "{{{.}}}"{{/vendorExtensions.x-size-message}})
{{/maxItems}}{{/minItems}}
{{!Email}}
{{#isEmail}}
@Email
@Email{{#pattern}}(regexp = "{{{pattern}}}"{{#vendorExtensions.x-pattern-message}}, message = "{{{.}}}"{{/vendorExtensions.x-pattern-message}}){{/pattern}}{{^pattern}}{{#vendorExtensions.x-pattern-message}}(message = "{{{.}}}"){{/vendorExtensions.x-pattern-message}}{{/pattern}}
{{/isEmail}}
{{!check for integer or long / all others=decimal type with @Decimal isInteger set}}
{{#isInteger}}
{{#minimum}}
@Min({{minimum}})
@Min({{#vendorExtensions.x-min-message}}value = {{/vendorExtensions.x-min-message}}{{{.}}}{{#vendorExtensions.x-min-message}}, message = "{{{.}}}"{{/vendorExtensions.x-min-message}})
{{/minimum}}
{{#maximum}}
@Max({{maximum}})
@Max({{#vendorExtensions.x-max-message}}value = {{/vendorExtensions.x-max-message}}{{{.}}}{{#vendorExtensions.x-max-message}}, message = "{{{.}}}"{{/vendorExtensions.x-max-message}})
{{/maximum}}
{{/isInteger}}
{{!isLong set}}
{{#isLong}}
{{#minimum}}
@Min({{minimum}}L)
@Min({{#vendorExtensions.x-min-message}}value = {{/vendorExtensions.x-min-message}}{{{.}}}L{{#vendorExtensions.x-min-message}}, message = "{{{.}}}"{{/vendorExtensions.x-min-message}})
{{/minimum}}
{{#maximum}}
@Max({{maximum}}L)
@Max({{#vendorExtensions.x-max-message}}value = {{/vendorExtensions.x-max-message}}{{{.}}}L{{#vendorExtensions.x-max-message}}, message = "{{{.}}}"{{/vendorExtensions.x-max-message}})
{{/maximum}}
{{/isLong}}
{{!Not Integer, not Long => we have a decimal value!}}
{{^isInteger}}
{{^isLong}}{{!minimum for decimal value}}
{{#minimum}}
@DecimalMin({{#exclusiveMinimum}}value = {{/exclusiveMinimum}}"{{minimum}}"{{#exclusiveMinimum}}, inclusive = false{{/exclusiveMinimum}})
@DecimalMin({{#exclusiveMinimum}}value = {{/exclusiveMinimum}}{{^exclusiveMinimum}}{{#vendorExtensions.x-min-message}}value = {{/vendorExtensions.x-min-message}}{{/exclusiveMinimum}}"{{minimum}}"{{#exclusiveMinimum}}, inclusive = false{{/exclusiveMinimum}}{{#vendorExtensions.x-min-message}}, message = "{{{.}}}"{{/vendorExtensions.x-min-message}})
{{/minimum}}
{{!maximal for decimal value}}
{{#maximum}}
@DecimalMax({{#exclusiveMaximum}}value = {{/exclusiveMaximum}}"{{maximum}}"{{#exclusiveMaximum}}, inclusive = false{{/exclusiveMaximum}})
@DecimalMax({{#exclusiveMaximum}}value = {{/exclusiveMaximum}}{{^exclusiveMaximum}}{{#vendorExtensions.x-max-message}}value = {{/vendorExtensions.x-max-message}}{{/exclusiveMaximum}}"{{maximum}}"{{#exclusiveMaximum}}, inclusive = false{{/exclusiveMaximum}}{{#vendorExtensions.x-max-message}}, message = "{{{.}}}"{{/vendorExtensions.x-max-message}})
{{/maximum}}
{{/isLong}}
{{/isInteger}}
Expand Down
Loading
Loading