-
Notifications
You must be signed in to change notification settings - Fork 426
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Spring - update nullable and required validation
Co-authored-by: frantuma <[email protected]>
- Loading branch information
Showing
21 changed files
with
470 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
src/main/resources/handlebars/JavaSpring/NotUndefined.mustache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package {{configPackage}}; | ||
|
||
import javax.validation.Constraint; | ||
import javax.validation.Payload; | ||
import java.lang.annotation.*; | ||
|
||
@Target({ElementType.TYPE}) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Documented | ||
@Constraint(validatedBy = NotUndefinedValidator.class) | ||
public @interface NotUndefined { | ||
String message() default "field cannot be undefined"; | ||
Class<?>[] groups() default {}; | ||
Class<? extends Payload>[] payload() default {}; | ||
} |
36 changes: 36 additions & 0 deletions
36
src/main/resources/handlebars/JavaSpring/NotUndefinedValidator.mustache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package {{configPackage}}; | ||
|
||
import org.openapitools.jackson.nullable.JsonNullable; | ||
import javax.validation.ConstraintValidator; | ||
import javax.validation.ConstraintValidatorContext; | ||
import java.lang.reflect.Field; | ||
|
||
public class NotUndefinedValidator implements ConstraintValidator<NotUndefined, Object>{ | ||
@Override | ||
public void initialize(NotUndefined constraintAnnotation) { | ||
} | ||
|
||
@Override | ||
public boolean isValid(Object addressInformation, ConstraintValidatorContext context) { | ||
Class<?> objClass = addressInformation.getClass(); | ||
Field[] fields = objClass.getDeclaredFields(); | ||
for (Field field : fields) { | ||
if (field.getType().equals(JsonNullable.class)){ | ||
field.setAccessible(true); | ||
try { | ||
Object value = field.get(addressInformation); | ||
if(value.equals(JsonNullable.undefined())){ | ||
context.disableDefaultConstraintViolation(); | ||
context.buildConstraintViolationWithTemplate(field.getName() + " cannot be undefined") | ||
.addConstraintViolation(); | ||
return false; | ||
} | ||
} catch (IllegalAccessException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} | ||
return true; | ||
} | ||
} |
4 changes: 2 additions & 2 deletions
4
src/main/resources/handlebars/JavaSpring/application.mustache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,11 @@ | ||
{{#useOas2}} | ||
springfox.documentation.swagger.v2.path=/api-docs | ||
server.contextPath={{^contextPath}}/{{/contextPath}}{{#contextPath}}{{contextPath}}{{/contextPath}} | ||
server.contextPath={{^contextPathWithoutHost}}/{{/contextPathWithoutHost}}{{#contextPathWithoutHost}}{{contextPathWithoutHost}}{{/contextPathWithoutHost}} | ||
{{/useOas2}} | ||
{{^useOas2}} | ||
springdoc.api-docs.path=/api-docs | ||
{{/useOas2}} | ||
server.servlet.contextPath={{^contextPath}}/{{/contextPath}}{{#contextPath}}{{contextPath}}{{/contextPath}} | ||
server.servlet.contextPath={{^contextPathWithoutHost}}/{{/contextPathWithoutHost}}{{#contextPathWithoutHost}}{{contextPathWithoutHost}}{{/contextPathWithoutHost}} | ||
server.port={{serverPort}} | ||
spring.jackson.date-format={{basePackage}}.RFC3339DateFormat | ||
spring.jackson.serialization.WRITE_DATES_AS_TIMESTAMPS=false |
3 changes: 3 additions & 0 deletions
3
src/main/resources/handlebars/JavaSpring/beanValidationFluentSetter.mustache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{{#isLegacyValidation}} public {{classname}} {{name}}({{{datatypeWithEnum}}} {{name}}) { {{/isLegacyValidation}}{{#isLegacyNullableValidation}} public {{classname}} {{name}}({{{datatypeWithEnum}}} {{name}}) { {{/isLegacyNullableValidation}} | ||
{{#isStrictValidation}}{{#required}}{{#nullable}} public {{classname}} {{name}}(JsonNullable<{{{datatypeWithEnum}}}> {{name}}) { {{/nullable}}{{^nullable}} public {{classname}} {{name}}({{{datatypeWithEnum}}} {{name}}) { {{/nullable}}{{/required}}{{^required}} public {{classname}} {{name}}({{{datatypeWithEnum}}} {{name}}) { {{/required}}{{/isStrictValidation}} | ||
{{#isLooseValidation}}{{#required}}{{#nullable}} public {{classname}} {{name}}(JsonNullable<{{{datatypeWithEnum}}}> {{name}}) { {{/nullable}}{{^nullable}} public {{classname}} {{name}}({{{datatypeWithEnum}}} {{name}}) { {{/nullable}}{{/required}}{{^required}} public {{classname}} {{name}}({{{datatypeWithEnum}}} {{name}}) { {{/required}}{{/isLooseValidation}} |
35 changes: 35 additions & 0 deletions
35
src/main/resources/handlebars/JavaSpring/beanValidationGetter.mustache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
{{#isLegacyValidation}}{{#required}}@NotNull{{/required}} {{#isContainer}}{{^isPrimitiveType}}{{^isEnum}} | ||
@Valid{{/isEnum}}{{/isPrimitiveType}}{{/isContainer}}{{#isNotContainer}}{{^isPrimitiveType}} | ||
@Valid{{/isPrimitiveType}}{{/isNotContainer}} | ||
{{>beanValidationCore}} public {{{datatypeWithEnum}}} {{getter}}() { {{/isLegacyValidation}}{{#isLegacyNullableValidation}}{{#required}}{{^useNullableForNotNull}}@NotNull{{/useNullableForNotNull}}{{/required}} | ||
{{#useNullableForNotNull}}{{^nullable}}@NotNull{{/nullable}}{{/useNullableForNotNull}}{{#isContainer}}{{^isPrimitiveType}}{{^isEnum}} | ||
@Valid{{/isEnum}}{{/isPrimitiveType}}{{/isContainer}}{{#isNotContainer}}{{^isPrimitiveType}} | ||
@Valid{{/isPrimitiveType}}{{/isNotContainer}} | ||
{{>beanValidationCore}} public {{{datatypeWithEnum}}} {{getter}}() { {{/isLegacyNullableValidation}}{{#isStrictValidation}}{{#required}}{{#nullable}}{{#isContainer}}{{^isPrimitiveType}}{{^isEnum}} | ||
@Valid{{/isEnum}}{{/isPrimitiveType}}{{/isContainer}}{{#isNotContainer}}{{^isPrimitiveType}} | ||
@Valid{{/isPrimitiveType}}{{/isNotContainer}} | ||
{{>beanValidationCore}} public JsonNullable<{{{datatypeWithEnum}}}> {{getter}}() { {{/nullable}}{{^nullable}}{{#isContainer}}{{^isPrimitiveType}}{{^isEnum}} | ||
@Valid{{/isEnum}}{{/isPrimitiveType}}{{/isContainer}}{{#isNotContainer}}{{^isPrimitiveType}} | ||
@Valid{{/isPrimitiveType}}{{/isNotContainer}} | ||
@NotNull | ||
{{>beanValidationCore}} public {{{datatypeWithEnum}}} {{getter}}() { {{/nullable}}{{/required}}{{^required}}{{#nullable}}{{#isContainer}}{{^isPrimitiveType}}{{^isEnum}} | ||
@Valid{{/isEnum}}{{/isPrimitiveType}}{{/isContainer}}{{#isNotContainer}}{{^isPrimitiveType}} | ||
@Valid{{/isPrimitiveType}}{{/isNotContainer}} | ||
{{>beanValidationCore}} public {{{datatypeWithEnum}}} {{getter}}() { | ||
{{/nullable}}{{^nullable}}{{#isContainer}}{{^isPrimitiveType}}{{^isEnum}} | ||
@Valid{{/isEnum}}{{/isPrimitiveType}}{{/isContainer}}{{#isNotContainer}}{{^isPrimitiveType}} | ||
@Valid{{/isPrimitiveType}}{{/isNotContainer}} | ||
{{>beanValidationCore}} public {{{datatypeWithEnum}}} {{getter}}() { {{/nullable}}{{/required}}{{/isStrictValidation}} {{#isLooseValidation}}{{#required}}{{#nullable}}{{#isContainer}}{{^isPrimitiveType}}{{^isEnum}} | ||
@Valid{{/isEnum}}{{/isPrimitiveType}}{{/isContainer}}{{#isNotContainer}}{{^isPrimitiveType}} | ||
@Valid{{/isPrimitiveType}}{{/isNotContainer}} | ||
{{>beanValidationCore}} public JsonNullable<{{{datatypeWithEnum}}}> {{getter}}() { {{/nullable}}{{^nullable}}{{#isContainer}}{{^isPrimitiveType}}{{^isEnum}} | ||
@Valid{{/isEnum}}{{/isPrimitiveType}}{{/isContainer}}{{#isNotContainer}}{{^isPrimitiveType}} | ||
@Valid{{/isPrimitiveType}}{{/isNotContainer}} | ||
@NotNull | ||
{{>beanValidationCore}} public {{{datatypeWithEnum}}} {{getter}}() { {{/nullable}}{{/required}}{{^required}}{{#nullable}}{{#isContainer}}{{^isPrimitiveType}}{{^isEnum}} | ||
@Valid{{/isEnum}}{{/isPrimitiveType}}{{/isContainer}}{{#isNotContainer}}{{^isPrimitiveType}} | ||
@Valid{{/isPrimitiveType}}{{/isNotContainer}} | ||
{{>beanValidationCore}} public {{{datatypeWithEnum}}} {{getter}}() { {{/nullable}}{{^nullable}}{{#isContainer}}{{^isPrimitiveType}}{{^isEnum}} | ||
@Valid{{/isEnum}}{{/isPrimitiveType}}{{/isContainer}}{{#isNotContainer}}{{^isPrimitiveType}} | ||
@Valid{{/isPrimitiveType}}{{/isNotContainer}} | ||
{{>beanValidationCore}} public {{{datatypeWithEnum}}} {{getter}}() { {{/nullable}}{{/required}}{{/isLooseValidation}} |
9 changes: 9 additions & 0 deletions
9
src/main/resources/handlebars/JavaSpring/beanValidationSetter.mustache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{{#isLegacyValidation}} public void {{setter}}({{{datatypeWithEnum}}} {{name}}){ {{/isLegacyValidation}} | ||
{{#isLegacyNullableValidation}} public void {{setter}}({{{datatypeWithEnum}}} {{name}}){ {{/isLegacyNullableValidation}} | ||
{{#isStrictValidation}} | ||
{{#required}}{{#nullable}} public void {{setter}}(JsonNullable<{{{datatypeWithEnum}}}> {{name}}) { {{/nullable}} | ||
{{^nullable}} public void {{setter}}({{{datatypeWithEnum}}} {{name}}) { {{/nullable}}{{/required}} | ||
{{^required}} public void {{setter}}({{{datatypeWithEnum}}} {{name}}) { {{/required}}{{/isStrictValidation}}{{#isLooseValidation}} | ||
{{#required}}{{#nullable}} public void {{setter}}(JsonNullable<{{{datatypeWithEnum}}}> {{name}}) { {{/nullable}} | ||
{{^nullable}} public void {{setter}}({{{datatypeWithEnum}}} {{name}}) { {{/nullable}}{{/required}} | ||
{{^required}} public void {{setter}}({{{datatypeWithEnum}}} {{name}}) { {{/required}}{{/isLooseValidation}} |
39 changes: 39 additions & 0 deletions
39
src/main/resources/handlebars/JavaSpring/beanValidationVariable.mustache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
{{#isLegacyValidation}} private {{{datatypeWithEnum}}} {{name}} = {{{defaultValue}}};{{/isLegacyValidation}}{{#isLegacyNullableValidation}} private {{{datatypeWithEnum}}} {{name}} = {{{defaultValue}}};{{/isLegacyNullableValidation}} | ||
{{#isStrictValidation}} | ||
{{#required}} | ||
{{#nullable}} | ||
private JsonNullable<{{{datatypeWithEnum}}}> {{name}} = JsonNullable.undefined(); | ||
{{/nullable}} | ||
{{^nullable}} | ||
private {{{datatypeWithEnum}}} {{name}} = {{{defaultValue}}}; | ||
{{/nullable}} | ||
{{/required}} | ||
{{^required}} | ||
{{#nullable}} | ||
private {{{datatypeWithEnum}}} {{name}} = {{{defaultValue}}}; | ||
{{/nullable}} | ||
{{^nullable}} | ||
@JsonInclude(JsonInclude.Include.NON_ABSENT) // Exclude from JSON if absent | ||
@JsonSetter(nulls = Nulls.FAIL) // FAIL setting if the value is null | ||
private {{{datatypeWithEnum}}} {{name}} = {{{defaultValue}}}; | ||
{{/nullable}} | ||
{{/required}} | ||
{{/isStrictValidation}} | ||
{{#isLooseValidation}} | ||
{{#required}} | ||
{{#nullable}} | ||
private JsonNullable<{{{datatypeWithEnum}}}> {{name}} = JsonNullable.undefined(); | ||
{{/nullable}} | ||
{{^nullable}} | ||
private {{{datatypeWithEnum}}} {{name}} = {{{defaultValue}}}; | ||
{{/nullable}} | ||
{{/required}} | ||
{{^required}} | ||
{{#nullable}} | ||
private {{{datatypeWithEnum}}} {{name}} = {{{defaultValue}}}; | ||
{{/nullable}} | ||
{{^nullable}} | ||
private {{{datatypeWithEnum}}} {{name}} = {{{defaultValue}}}; | ||
{{/nullable}} | ||
{{/required}} | ||
{{/isLooseValidation}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.