From 9521d8de839d1410b9c7c80fa13a754b38ab43c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=C2=A0Kolenda?= Date: Mon, 30 Sep 2024 21:19:36 +0200 Subject: [PATCH] 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(); --- .../resources/templates/libraries/microprofile/pojo.qute | 2 +- .../src/main/openapi/bean-validation-true.yaml | 3 +++ .../openapi/generator/it/BeanValidationTest.java | 8 +++++--- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/client/deployment/src/main/resources/templates/libraries/microprofile/pojo.qute b/client/deployment/src/main/resources/templates/libraries/microprofile/pojo.qute index a3d704f6f..56d3e3714 100644 --- a/client/deployment/src/main/resources/templates/libraries/microprofile/pojo.qute +++ b/client/deployment/src/main/resources/templates/libraries/microprofile/pojo.qute @@ -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} diff --git a/client/integration-tests/bean-validation/src/main/openapi/bean-validation-true.yaml b/client/integration-tests/bean-validation/src/main/openapi/bean-validation-true.yaml index ea7751f9d..68041e319 100644 --- a/client/integration-tests/bean-validation/src/main/openapi/bean-validation-true.yaml +++ b/client/integration-tests/bean-validation/src/main/openapi/bean-validation-true.yaml @@ -47,6 +47,7 @@ components: required: - id - name + - secondName - size properties: id: @@ -58,6 +59,8 @@ components: pattern: "[a-zA-Z]*" minLength: 1 maxLength: 10 + secondName: + type: string size: type: number minimum: 1.0 diff --git a/client/integration-tests/bean-validation/src/test/java/io/quarkiverse/openapi/generator/it/BeanValidationTest.java b/client/integration-tests/bean-validation/src/test/java/io/quarkiverse/openapi/generator/it/BeanValidationTest.java index a05b19ce1..4e5d34a43 100644 --- a/client/integration-tests/bean-validation/src/test/java/io/quarkiverse/openapi/generator/it/BeanValidationTest.java +++ b/client/integration-tests/bean-validation/src/test/java/io/quarkiverse/openapi/generator/it/BeanValidationTest.java @@ -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; @@ -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(); @@ -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(); @@ -95,4 +97,4 @@ void testValidationAnnotationsAreSkippedModel() throws Exception { assertThat(size.isAnnotationPresent(DecimalMin.class)).isFalse(); assertThat(size.isAnnotationPresent(DecimalMax.class)).isFalse(); } -} \ No newline at end of file +}