Skip to content

Commit

Permalink
Adding NotNull annotation for required fields (#810)
Browse files Browse the repository at this point in the history
* NotNull annotation added for required field (even when the field has no other constraint).

* Update BeanValidationTest.java

getFields method returns only `public` fields and the generated class consists only of private ones.

It makes ValidatedObject.class.getFields() always returning an empty array.

Following assertion has no chance to fail
assertThat(Arrays.stream(UnvalidatedObject.class.getFields())
                .noneMatch(f -> f.isAnnotationPresent(NotNull.class))).isTrue();
  • Loading branch information
michalkolenda authored Sep 30, 2024
1 parent 246fa51 commit 9521d8d
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public class {m.classname} {#if m.parent}extends {m.parent}{/if}{#if m.serializa
* {v.description}
**/
{/if}
{#if v.hasValidation}{#include beanValidation.qute p=v/}{/if}
{#if v.hasValidation || v.required}{#include beanValidation.qute p=v/}{/if}
{#if v.isContainer}
private {v.datatypeWithEnum} {v.name}{#if v.required && v.defaultValue} = {v.defaultValue}{/if};
{#else}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ components:
required:
- id
- name
- secondName
- size
properties:
id:
Expand All @@ -58,6 +59,8 @@ components:
pattern: "[a-zA-Z]*"
minLength: 1
maxLength: 10
secondName:
type: string
size:
type: number
minimum: 1.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.stream.Stream;

import jakarta.validation.Valid;
import jakarta.validation.constraints.DecimalMax;
Expand Down Expand Up @@ -43,9 +44,10 @@ void testValidationAnnotationsAreInPlaceApi() {
void testValidationAnnotationsAreInPlaceModel() throws Exception {
Field id = ValidatedObject.class.getDeclaredField("id");
Field name = ValidatedObject.class.getDeclaredField("name");
Field secondName = ValidatedObject.class.getDeclaredField("secondName");
Field size = ValidatedObject.class.getDeclaredField("size");

assertThat(Arrays.stream(ValidatedObject.class.getFields())
assertThat(Stream.of(id, name, secondName, size)
.allMatch(f -> f.isAnnotationPresent(NotNull.class)))
.isTrue();

Expand Down Expand Up @@ -86,7 +88,7 @@ void testValidationAnnotationsAreSkippedModel() throws Exception {
Field name = UnvalidatedObject.class.getDeclaredField("name");
Field size = UnvalidatedObject.class.getDeclaredField("size");

assertThat(Arrays.stream(UnvalidatedObject.class.getFields())
assertThat(Stream.of(id, name, size)
.noneMatch(f -> f.isAnnotationPresent(NotNull.class))).isTrue();
assertThat(id.isAnnotationPresent(Min.class)).isFalse();
assertThat(id.isAnnotationPresent(Max.class)).isFalse();
Expand All @@ -95,4 +97,4 @@ void testValidationAnnotationsAreSkippedModel() throws Exception {
assertThat(size.isAnnotationPresent(DecimalMin.class)).isFalse();
assertThat(size.isAnnotationPresent(DecimalMax.class)).isFalse();
}
}
}

0 comments on commit 9521d8d

Please sign in to comment.