[FEAT] manage @nullable contrainst on list and Map too.

This commit is contained in:
Edouard DUPIN 2024-05-27 17:00:31 +02:00
parent 2a2b6b8f3b
commit 187ffba188
6 changed files with 33 additions and 6 deletions

View File

@ -11,6 +11,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.annotation.Nullable;
import jakarta.persistence.Column;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
@ -258,6 +259,14 @@ public class AnnotationTools {
return !((Column) annotation[0]).nullable();
}
public static boolean getNullable(final Field element) throws Exception {
final Annotation[] annotation = element.getDeclaredAnnotationsByType(Nullable.class);
if (annotation.length == 0) {
return false;
}
return true;
}
public static boolean getConstraintsNotNull(final Field element) throws Exception {
final Annotation[] annotation = element.getDeclaredAnnotationsByType(NotNull.class);
if (annotation.length == 0) {

View File

@ -216,10 +216,18 @@ public class TsClassElement {
}
public String optionalTypeZod(final FieldProperty field) {
// Common checking element (apply to List, Map, ...)
if (field.nullable()) {
return ".optional()";
}
if (field.notNull()) {
return "";
}
// Other object:
if (field.model().getOriginClasses() == null || field.model().getOriginClasses().isPrimitive()) {
return "";
}
if (field.notNull()) {
if (field.columnNotNull()) {
return "";
}
return ".optional()";

View File

@ -54,18 +54,20 @@ public class ClassObjectModel extends ClassModel {
ClassModel model,
String comment,
int limitSize,
boolean readOnly,
boolean notNull,
boolean nullable) {
Boolean readOnly,
Boolean notNull,
Boolean columnNotNull,
Boolean nullable) {
public FieldProperty(final String name, final ClassModel model, final String comment, final int limitSize,
final boolean readOnly, final boolean notNull, final boolean nullable) {
final Boolean readOnly, final Boolean notNull, final Boolean columnNotNull, final Boolean nullable) {
this.name = name;
this.model = model;
this.comment = comment;
this.limitSize = limitSize;
this.readOnly = readOnly;
this.notNull = notNull;
this.columnNotNull = columnNotNull;
this.nullable = nullable;
}
@ -77,7 +79,8 @@ public class ClassObjectModel extends ClassModel {
AnnotationTools.getLimitSize(field), //
AnnotationTools.getSchemaReadOnly(field), //
AnnotationTools.getConstraintsNotNull(field), //
AnnotationTools.getColumnNotNull(field));
AnnotationTools.getColumnNotNull(field), //
AnnotationTools.getNullable(field));
}
}

View File

@ -4,6 +4,7 @@ import org.kar.archidata.annotation.DataDeleted;
import org.kar.archidata.annotation.DataNotRead;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.annotation.Nullable;
import jakarta.persistence.Column;
import jakarta.ws.rs.DefaultValue;
@ -13,5 +14,6 @@ public class GenericDataSoftDelete extends GenericData {
@DefaultValue("'0'")
@DataDeleted
@Schema(description = "Deleted state", hidden = true, required = false, readOnly = true)
@Nullable
public Boolean deleted = null;
}

View File

@ -9,6 +9,7 @@ import org.kar.archidata.annotation.UpdateTimestamp;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.annotation.Nullable;
import jakarta.persistence.Column;
public class GenericTiming {
@ -17,6 +18,7 @@ public class GenericTiming {
@Column(nullable = false)
@Schema(description = "Create time of the object", required = false, example = "2000-01-23T01:23:45.678+01:00", readOnly = true)
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX")
@Nullable
public Date createdAt = null;
@DataNotRead
@UpdateTimestamp
@ -24,5 +26,6 @@ public class GenericTiming {
@Schema(description = "When update the object", required = false, example = "2000-01-23T00:23:45.678Z", readOnly = true)
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX")
// public Instant updatedAt = null;
@Nullable
public Date updatedAt = null;
}

View File

@ -4,6 +4,7 @@ import org.kar.archidata.annotation.DataDeleted;
import org.kar.archidata.annotation.DataNotRead;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.annotation.Nullable;
import jakarta.persistence.Column;
import jakarta.ws.rs.DefaultValue;
@ -13,5 +14,6 @@ public class UUIDGenericDataSoftDelete extends UUIDGenericData {
@DefaultValue("'0'")
@DataDeleted
@Schema(description = "Deleted state", hidden = true, required = false, readOnly = true)
@Nullable
public Boolean deleted = null;
}