Skip to content

Commit 9521d8d

Browse files
Adding NotNull annotation for required fields (#810)
* 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();
1 parent 246fa51 commit 9521d8d

File tree

3 files changed

+9
-4
lines changed

3 files changed

+9
-4
lines changed

client/deployment/src/main/resources/templates/libraries/microprofile/pojo.qute

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public class {m.classname} {#if m.parent}extends {m.parent}{/if}{#if m.serializa
3939
* {v.description}
4040
**/
4141
{/if}
42-
{#if v.hasValidation}{#include beanValidation.qute p=v/}{/if}
42+
{#if v.hasValidation || v.required}{#include beanValidation.qute p=v/}{/if}
4343
{#if v.isContainer}
4444
private {v.datatypeWithEnum} {v.name}{#if v.required && v.defaultValue} = {v.defaultValue}{/if};
4545
{#else}

client/integration-tests/bean-validation/src/main/openapi/bean-validation-true.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ components:
4747
required:
4848
- id
4949
- name
50+
- secondName
5051
- size
5152
properties:
5253
id:
@@ -58,6 +59,8 @@ components:
5859
pattern: "[a-zA-Z]*"
5960
minLength: 1
6061
maxLength: 10
62+
secondName:
63+
type: string
6164
size:
6265
type: number
6366
minimum: 1.0

client/integration-tests/bean-validation/src/test/java/io/quarkiverse/openapi/generator/it/BeanValidationTest.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import java.lang.reflect.Field;
77
import java.lang.reflect.Method;
88
import java.util.Arrays;
9+
import java.util.stream.Stream;
910

1011
import jakarta.validation.Valid;
1112
import jakarta.validation.constraints.DecimalMax;
@@ -43,9 +44,10 @@ void testValidationAnnotationsAreInPlaceApi() {
4344
void testValidationAnnotationsAreInPlaceModel() throws Exception {
4445
Field id = ValidatedObject.class.getDeclaredField("id");
4546
Field name = ValidatedObject.class.getDeclaredField("name");
47+
Field secondName = ValidatedObject.class.getDeclaredField("secondName");
4648
Field size = ValidatedObject.class.getDeclaredField("size");
4749

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

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

89-
assertThat(Arrays.stream(UnvalidatedObject.class.getFields())
91+
assertThat(Stream.of(id, name, size)
9092
.noneMatch(f -> f.isAnnotationPresent(NotNull.class))).isTrue();
9193
assertThat(id.isAnnotationPresent(Min.class)).isFalse();
9294
assertThat(id.isAnnotationPresent(Max.class)).isFalse();
@@ -95,4 +97,4 @@ void testValidationAnnotationsAreSkippedModel() throws Exception {
9597
assertThat(size.isAnnotationPresent(DecimalMin.class)).isFalse();
9698
assertThat(size.isAnnotationPresent(DecimalMax.class)).isFalse();
9799
}
98-
}
100+
}

0 commit comments

Comments
 (0)