diff --git a/src/org/kar/archidata/annotation/apiGenerator/ApiNotNull.java b/src/org/kar/archidata/annotation/apiGenerator/ApiNotNull.java new file mode 100644 index 0000000..7f443ef --- /dev/null +++ b/src/org/kar/archidata/annotation/apiGenerator/ApiNotNull.java @@ -0,0 +1,45 @@ +package org.kar.archidata.annotation.apiGenerator; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * Annotation to explicitly define the nullability of a parameter in an API. + * + * This annotation allows marking a parameter as required (non-null) or optional (nullable), + * overriding any other nullability considerations. It is useful in API generation frameworks + * to ensure precise validation and documentation of method parameters. + * + *
Usage: + * - Target: This annotation can be applied to parameters. + * - Retention: The annotation is retained at runtime, allowing it to be + * processed by frameworks or libraries that handle code generation logic. + * + *
Behavior: + * - When applied to a parameter, it explicitly marks it as optional or required in the API. + * This annotation overrides all other considerations regarding nullability. + * + *
Example: + *
{@code + * public class User { + * @ReadOnlyField + * @ApiNotNull + * public String username; + * public String email; + * } + * }+ * + * In this example, the `username` field in the `User` class is explicitly marked as non-null in the generated API. + */ +@Target({ ElementType.PARAMETER }) +@Retention(RetentionPolicy.RUNTIME) +public @interface ApiNotNull { + /** + * (Optional) Specifies whether the API element can be null. + * If set to `true`, the element is required (non-null). + * If set to `false`, the element is optional (nullable). + */ + boolean value() default true; +} \ No newline at end of file diff --git a/src/org/kar/archidata/externalRestApi/model/ClassObjectModel.java b/src/org/kar/archidata/externalRestApi/model/ClassObjectModel.java index 79dd2a5..c1abd05 100644 --- a/src/org/kar/archidata/externalRestApi/model/ClassObjectModel.java +++ b/src/org/kar/archidata/externalRestApi/model/ClassObjectModel.java @@ -13,6 +13,7 @@ import java.util.Set; import org.kar.archidata.annotation.AnnotationTools; import org.kar.archidata.annotation.apiGenerator.ApiAccessLimitation; import org.kar.archidata.annotation.apiGenerator.ApiGenerationMode; +import org.kar.archidata.annotation.apiGenerator.ApiNotNull; import org.kar.archidata.exception.DataAccessException; import org.kar.archidata.tools.AnnotationCreator; import org.slf4j.Logger; @@ -83,13 +84,26 @@ public class ClassObjectModel extends ClassModel { Email email, ApiAccessLimitation accessLimitation, Boolean notNull, + ApiNotNull apiNotNull, Boolean columnNotNull, Boolean nullable) { - public FieldProperty(final String name, final ClassModel model, final ClassModel linkClass, - final String comment, final Size stringSize, final Min min, final Max max, final DecimalMin decimalMin, - final DecimalMax decimalMax, final Pattern pattern, final Email email, - final ApiAccessLimitation accessLimitation, final Boolean notNull, final Boolean columnNotNull, + public FieldProperty(// + final String name, // + final ClassModel model, // + final ClassModel linkClass, // + final String comment, // + final Size stringSize, // + final Min min, // + final Max max, // + final DecimalMin decimalMin, // + final DecimalMax decimalMax, // + final Pattern pattern, // + final Email email, // + final ApiAccessLimitation accessLimitation, // + final Boolean notNull, // + final ApiNotNull apiNotNull, // + final Boolean columnNotNull, // final Boolean nullable) { this.name = name; this.model = model; @@ -108,6 +122,7 @@ public class ClassObjectModel extends ClassModel { this.accessLimitation = accessLimitation; } this.notNull = notNull; + this.apiNotNull = apiNotNull; this.columnNotNull = columnNotNull; this.nullable = nullable; @@ -160,6 +175,7 @@ public class ClassObjectModel extends ClassModel { AnnotationTools.getConstraintsEmail(field), // AnnotationTools.get(field, ApiAccessLimitation.class), // AnnotationTools.getConstraintsNotNull(field), // + AnnotationTools.get(field, ApiNotNull.class), // AnnotationTools.getColumnNotNull(field), // AnnotationTools.getNullable(field)); } diff --git a/src/org/kar/archidata/externalRestApi/typescript/TsClassElement.java b/src/org/kar/archidata/externalRestApi/typescript/TsClassElement.java index 416a384..8029df7 100644 --- a/src/org/kar/archidata/externalRestApi/typescript/TsClassElement.java +++ b/src/org/kar/archidata/externalRestApi/typescript/TsClassElement.java @@ -237,6 +237,9 @@ public class TsClassElement { } public boolean isOptionalTypeZod(final FieldProperty field) { + if (field.apiNotNull() != null) { + return field.apiNotNull().value(); + } // Common checking element (apply to List, Map, ...) if (field.nullable()) { return true; @@ -287,13 +290,13 @@ public class TsClassElement { builder.append(")"); } } - /*Must be tested before + /* Must be tested before if (field.pattern() != null) { builder.append(".regex(("); builder.append(field.pattern().regexp()); builder.append(")"); }*/ - /*Must be tested before + /* Must be tested before if (field.email() != null) { builder.append(".regex(("); builder.append(field.email().regexp());