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

View File

@ -216,10 +216,18 @@ public class TsClassElement {
} }
public String optionalTypeZod(final FieldProperty field) { 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()) { if (field.model().getOriginClasses() == null || field.model().getOriginClasses().isPrimitive()) {
return ""; return "";
} }
if (field.notNull()) { if (field.columnNotNull()) {
return ""; return "";
} }
return ".optional()"; return ".optional()";

View File

@ -54,18 +54,20 @@ public class ClassObjectModel extends ClassModel {
ClassModel model, ClassModel model,
String comment, String comment,
int limitSize, int limitSize,
boolean readOnly, Boolean readOnly,
boolean notNull, Boolean notNull,
boolean nullable) { Boolean columnNotNull,
Boolean nullable) {
public FieldProperty(final String name, final ClassModel model, final String comment, final int limitSize, 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.name = name;
this.model = model; this.model = model;
this.comment = comment; this.comment = comment;
this.limitSize = limitSize; this.limitSize = limitSize;
this.readOnly = readOnly; this.readOnly = readOnly;
this.notNull = notNull; this.notNull = notNull;
this.columnNotNull = columnNotNull;
this.nullable = nullable; this.nullable = nullable;
} }
@ -77,7 +79,8 @@ public class ClassObjectModel extends ClassModel {
AnnotationTools.getLimitSize(field), // AnnotationTools.getLimitSize(field), //
AnnotationTools.getSchemaReadOnly(field), // AnnotationTools.getSchemaReadOnly(field), //
AnnotationTools.getConstraintsNotNull(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 org.kar.archidata.annotation.DataNotRead;
import io.swagger.v3.oas.annotations.media.Schema; import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.annotation.Nullable;
import jakarta.persistence.Column; import jakarta.persistence.Column;
import jakarta.ws.rs.DefaultValue; import jakarta.ws.rs.DefaultValue;
@ -13,5 +14,6 @@ public class GenericDataSoftDelete extends GenericData {
@DefaultValue("'0'") @DefaultValue("'0'")
@DataDeleted @DataDeleted
@Schema(description = "Deleted state", hidden = true, required = false, readOnly = true) @Schema(description = "Deleted state", hidden = true, required = false, readOnly = true)
@Nullable
public Boolean deleted = null; public Boolean deleted = null;
} }

View File

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

View File

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