[DEV] update with openAPI

This commit is contained in:
Edouard DUPIN 2023-12-27 11:08:10 +01:00
parent 1c82fb1a86
commit dfc7c77f47
9 changed files with 94 additions and 43 deletions

View File

@ -25,7 +25,7 @@
<attribute name="optional" value="true"/> <attribute name="optional" value="true"/>
</attributes> </attributes>
</classpathentry> </classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"> <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-21">
<attributes> <attributes>
<attribute name="maven.pomderived" value="true"/> <attribute name="maven.pomderived" value="true"/>
</attributes> </attributes>

View File

@ -142,6 +142,12 @@
<artifactId>jakarta.persistence-api</artifactId> <artifactId>jakarta.persistence-api</artifactId>
<version>3.2.0-M1</version> <version>3.2.0-M1</version>
</dependency> </dependency>
<!-- Swagger dependencies -->
<dependency>
<groupId>io.swagger.core.v3</groupId>
<artifactId>swagger-jaxrs2-jakarta</artifactId>
<version>2.1.10</version>
</dependency>
<!-- <!--
************************************************************ ************************************************************
** TEST dependency ** ** TEST dependency **

View File

@ -10,6 +10,7 @@ import org.kar.archidata.dataAccess.options.OverrideTableName;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.persistence.Column; import jakarta.persistence.Column;
import jakarta.persistence.GeneratedValue; import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType; import jakarta.persistence.GenerationType;
@ -51,10 +52,21 @@ public class AnnotationTools {
return tmp; return tmp;
} }
public static String getSchemedescription(final Field element) throws Exception {
final Annotation[] annotation = element.getDeclaredAnnotationsByType(Schema.class);
if (annotation.length == 0) {
return null;
}
if (annotation.length > 1) {
throw new Exception("Must not have more than 1 element @Schema on " + element.getClass().getCanonicalName());
}
return ((Schema) annotation[0]).description();
}
public static String getComment(final Field element) throws Exception { public static String getComment(final Field element) throws Exception {
final Annotation[] annotation = element.getDeclaredAnnotationsByType(DataComment.class); final Annotation[] annotation = element.getDeclaredAnnotationsByType(DataComment.class);
if (annotation.length == 0) { if (annotation.length == 0) {
return null; return getSchemedescription(element);
} }
if (annotation.length > 1) { if (annotation.length > 1) {
throw new Exception("Must not have more than 1 element @DataComment on " + element.getClass().getCanonicalName()); throw new Exception("Must not have more than 1 element @DataComment on " + element.getClass().getCanonicalName());

View File

@ -7,8 +7,7 @@ import java.lang.annotation.Target;
@Target({ ElementType.TYPE, ElementType.FIELD }) @Target({ ElementType.TYPE, ElementType.FIELD })
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)
@Deprecated(since = "0.5.2")
public @interface DataComment { public @interface DataComment {
String value(); String value();
} }

View File

@ -0,0 +1,34 @@
package org.kar.archidata.api;
import io.swagger.v3.jaxrs2.integration.resources.BaseOpenApiResource;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.annotation.security.PermitAll;
import jakarta.servlet.ServletConfig;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.Application;
import jakarta.ws.rs.core.Context;
import jakarta.ws.rs.core.HttpHeaders;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.core.UriInfo;
@Path("/openapi")
public class openApiResource extends BaseOpenApiResource {
@Context
ServletConfig config;
@Context
Application app;
@GET
@Produces({ MediaType.APPLICATION_JSON })
@Operation(hidden = true)
@PermitAll
public Response getDescription(@Context final HttpHeaders headers, @Context final UriInfo uriInfo) throws Exception {
return getOpenApi(headers, this.config, this.app, uriInfo, "json");
}
}

View File

@ -471,12 +471,12 @@ public class DataAccess {
public static <T> T insert(final T data, final QueryOption... option) throws Exception { public static <T> T insert(final T data, final QueryOption... option) throws Exception {
final Class<?> clazz = data.getClass(); final Class<?> clazz = data.getClass();
QueryOptions options = new QueryOptions(option); final QueryOptions options = new QueryOptions(option);
// External checker of data: // External checker of data:
final CheckFunction check = options.get(CheckFunction.class); final CheckFunction check = options.get(CheckFunction.class);
if (check != null) { if (check != null) {
check.getChecker().check(data, AnnotationTools.getFieldsNames(clazz)); check.getChecker().check("", data, AnnotationTools.getFieldsNames(clazz));
} }
DBEntry entry = DBEntry.createInterface(GlobalConfiguration.dbConfig); DBEntry entry = DBEntry.createInterface(GlobalConfiguration.dbConfig);
@ -638,7 +638,7 @@ public class DataAccess {
} }
// check the compatibility of the id and the declared ID // check the compatibility of the id and the declared ID
final Class<?> typeClass = idField.getType(); final Class<?> typeClass = idField.getType();
if (idKey.getClass() == typeClass) { if (idKey.getClass() != typeClass) {
throw new DataAccessException("Request update with the wrong type ..."); throw new DataAccessException("Request update with the wrong type ...");
} }
return new QueryCondition(AnnotationTools.getFieldName(idField), "=", idKey); return new QueryCondition(AnnotationTools.getFieldName(idField), "=", idKey);
@ -654,8 +654,8 @@ public class DataAccess {
* @return the number of object updated * @return the number of object updated
* @throws Exception */ * @throws Exception */
public static <T, ID_TYPE> int updateWithJson(final Class<T> clazz, final ID_TYPE id, final String jsonData, final QueryOption... option) throws Exception { public static <T, ID_TYPE> int updateWithJson(final Class<T> clazz, final ID_TYPE id, final String jsonData, final QueryOption... option) throws Exception {
QueryOptions options = new QueryOptions(option); final QueryOptions options = new QueryOptions(option);
Condition condition = options.get(Condition.class); final Condition condition = options.get(Condition.class);
if (condition != null) { if (condition != null) {
throw new DataAccessException("request a updateWithJson with a condition"); throw new DataAccessException("request a updateWithJson with a condition");
} }
@ -664,8 +664,8 @@ public class DataAccess {
} }
public static <T> int updateWhereWithJson(final Class<T> clazz, final String jsonData, final QueryOption... option) throws Exception { public static <T> int updateWhereWithJson(final Class<T> clazz, final String jsonData, final QueryOption... option) throws Exception {
QueryOptions options = new QueryOptions(option); final QueryOptions options = new QueryOptions(option);
Condition condition = options.get(Condition.class); final Condition condition = options.get(Condition.class);
if (condition == null) { if (condition == null) {
throw new DataAccessException("request a updateWhereWithJson without any condition"); throw new DataAccessException("request a updateWhereWithJson without any condition");
} }
@ -692,19 +692,19 @@ public class DataAccess {
* @return the affected rows. * @return the affected rows.
* @throws Exception */ * @throws Exception */
public static <T, ID_TYPE> int update(final T data, final ID_TYPE id, final List<String> updateColomn) throws Exception { public static <T, ID_TYPE> int update(final T data, final ID_TYPE id, final List<String> updateColomn) throws Exception {
QueryOptions options = new QueryOptions(new Condition(getTableIdCondition(data.getClass(), id)), new FilterValue(updateColomn)); final QueryOptions options = new QueryOptions(new Condition(getTableIdCondition(data.getClass(), id)), new FilterValue(updateColomn));
return updateWhere(data, options.getAllArray()); return updateWhere(data, options.getAllArray());
} }
// il y avait: final List<String> filterValue // il y avait: final List<String> filterValue
public static <T> int updateWhere(final T data, final QueryOption... option) throws Exception { public static <T> int updateWhere(final T data, final QueryOption... option) throws Exception {
final Class<?> clazz = data.getClass(); final Class<?> clazz = data.getClass();
QueryOptions options = new QueryOptions(option); final QueryOptions options = new QueryOptions(option);
Condition condition = options.get(Condition.class); final Condition condition = options.get(Condition.class);
if (condition == null) { if (condition == null) {
throw new DataAccessException("request a gets without any condition"); throw new DataAccessException("request a gets without any condition");
} }
FilterValue filter = options.get(FilterValue.class); final FilterValue filter = options.get(FilterValue.class);
if (filter == null) { if (filter == null) {
throw new DataAccessException("request a gets without any filter values"); throw new DataAccessException("request a gets without any filter values");
} }
@ -715,7 +715,7 @@ public class DataAccess {
if (options != null) { if (options != null) {
final CheckFunction check = options.get(CheckFunction.class); final CheckFunction check = options.get(CheckFunction.class);
if (check != null) { if (check != null) {
check.getChecker().check(data, filter.getValues()); check.getChecker().check("", data, filter.getValues());
} }
} }
@ -877,8 +877,8 @@ public class DataAccess {
return executeQuerry(query, false); return executeQuerry(query, false);
} }
public static <T> T getWhere(final Class<T> clazz, QueryOption... option) throws Exception { public static <T> T getWhere(final Class<T> clazz, final QueryOption... option) throws Exception {
QueryOptions options = new QueryOptions(option); final QueryOptions options = new QueryOptions(option);
options.add(new Limit(1)); options.add(new Limit(1));
final List<T> values = getsWhere(clazz, options); final List<T> values = getsWhere(clazz, options);
if (values.size() == 0) { if (values.size() == 0) {
@ -926,13 +926,13 @@ public class DataAccess {
} }
public static <T> List<T> getsWhere(final Class<T> clazz, final QueryOption... option) throws Exception { public static <T> List<T> getsWhere(final Class<T> clazz, final QueryOption... option) throws Exception {
QueryOptions options = new QueryOptions(option); final QueryOptions options = new QueryOptions(option);
return getsWhere(clazz, options); return getsWhere(clazz, options);
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public static <T> List<T> getsWhere(final Class<T> clazz, final QueryOptions options) throws Exception { public static <T> List<T> getsWhere(final Class<T> clazz, final QueryOptions options) throws Exception {
Condition condition = options.get(Condition.class); final Condition condition = options.get(Condition.class);
final List<LazyGetter> lazyCall = new ArrayList<>(); final List<LazyGetter> lazyCall = new ArrayList<>();
final String deletedFieldName = AnnotationTools.getDeletedFieldName(clazz); final String deletedFieldName = AnnotationTools.getDeletedFieldName(clazz);
DBEntry entry = DBEntry.createInterface(GlobalConfiguration.dbConfig); DBEntry entry = DBEntry.createInterface(GlobalConfiguration.dbConfig);
@ -1029,14 +1029,14 @@ public class DataAccess {
} }
public static long countWhere(final Class<?> clazz, final QueryOption... option) throws Exception { public static long countWhere(final Class<?> clazz, final QueryOption... option) throws Exception {
QueryOptions options = new QueryOptions(option); final QueryOptions options = new QueryOptions(option);
Condition condition = options.get(Condition.class); final Condition condition = options.get(Condition.class);
final String deletedFieldName = AnnotationTools.getDeletedFieldName(clazz); final String deletedFieldName = AnnotationTools.getDeletedFieldName(clazz);
DBEntry entry = DBEntry.createInterface(GlobalConfiguration.dbConfig); DBEntry entry = DBEntry.createInterface(GlobalConfiguration.dbConfig);
long count = 0; long count = 0;
// real add in the BDD: // real add in the BDD:
try { try {
StringBuilder query = new StringBuilder(); final StringBuilder query = new StringBuilder();
final String tableName = AnnotationTools.getTableName(clazz, options); final String tableName = AnnotationTools.getTableName(clazz, options);
query.append("SELECT COUNT(*) FROM `"); query.append("SELECT COUNT(*) FROM `");
query.append(tableName); query.append(tableName);
@ -1076,7 +1076,7 @@ public class DataAccess {
} }
public static <T, ID_TYPE> T get(final Class<T> clazz, final ID_TYPE id, final QueryOption... option) throws Exception { public static <T, ID_TYPE> T get(final Class<T> clazz, final ID_TYPE id, final QueryOption... option) throws Exception {
QueryOptions options = new QueryOptions(option); final QueryOptions options = new QueryOptions(option);
options.add(new Condition(getTableIdCondition(clazz, id))); options.add(new Condition(getTableIdCondition(clazz, id)));
return DataAccess.getWhere(clazz, options.getAllArray()); return DataAccess.getWhere(clazz, options.getAllArray());
} }
@ -1128,14 +1128,14 @@ public class DataAccess {
} }
public static <ID_TYPE> int deleteHard(final Class<?> clazz, final ID_TYPE id, final QueryOption... option) throws Exception { public static <ID_TYPE> int deleteHard(final Class<?> clazz, final ID_TYPE id, final QueryOption... option) throws Exception {
QueryOptions options = new QueryOptions(option); final QueryOptions options = new QueryOptions(option);
options.add(new Condition(getTableIdCondition(clazz, id))); options.add(new Condition(getTableIdCondition(clazz, id)));
return deleteHardWhere(clazz, options.getAllArray()); return deleteHardWhere(clazz, options.getAllArray());
} }
public static int deleteHardWhere(final Class<?> clazz, final QueryOption... option) throws Exception { public static int deleteHardWhere(final Class<?> clazz, final QueryOption... option) throws Exception {
QueryOptions options = new QueryOptions(option); final QueryOptions options = new QueryOptions(option);
Condition condition = options.get(Condition.class); final Condition condition = options.get(Condition.class);
if (condition == null) { if (condition == null) {
throw new DataAccessException("request a gets without any condition"); throw new DataAccessException("request a gets without any condition");
} }
@ -1162,14 +1162,14 @@ public class DataAccess {
} }
private static <ID_TYPE> int deleteSoft(final Class<?> clazz, final ID_TYPE id, final QueryOption... option) throws Exception { private static <ID_TYPE> int deleteSoft(final Class<?> clazz, final ID_TYPE id, final QueryOption... option) throws Exception {
QueryOptions options = new QueryOptions(option); final QueryOptions options = new QueryOptions(option);
options.add(new Condition(getTableIdCondition(clazz, id))); options.add(new Condition(getTableIdCondition(clazz, id)));
return deleteSoftWhere(clazz, options.getAllArray()); return deleteSoftWhere(clazz, options.getAllArray());
} }
public static int deleteSoftWhere(final Class<?> clazz, final QueryOption... option) throws Exception { public static int deleteSoftWhere(final Class<?> clazz, final QueryOption... option) throws Exception {
QueryOptions options = new QueryOptions(option); final QueryOptions options = new QueryOptions(option);
Condition condition = options.get(Condition.class); final Condition condition = options.get(Condition.class);
if (condition == null) { if (condition == null) {
throw new DataAccessException("request a gets without any condition"); throw new DataAccessException("request a gets without any condition");
} }
@ -1205,14 +1205,14 @@ public class DataAccess {
} }
public static <ID_TYPE> int unsetDelete(final Class<?> clazz, final ID_TYPE id, final QueryOption... option) throws Exception { public static <ID_TYPE> int unsetDelete(final Class<?> clazz, final ID_TYPE id, final QueryOption... option) throws Exception {
QueryOptions options = new QueryOptions(option); final QueryOptions options = new QueryOptions(option);
options.add(new Condition(getTableIdCondition(clazz, id))); options.add(new Condition(getTableIdCondition(clazz, id)));
return unsetDeleteWhere(clazz, options.getAllArray()); return unsetDeleteWhere(clazz, options.getAllArray());
} }
public static int unsetDeleteWhere(final Class<?> clazz, final QueryOption... option) throws Exception { public static int unsetDeleteWhere(final Class<?> clazz, final QueryOption... option) throws Exception {
QueryOptions options = new QueryOptions(option); final QueryOptions options = new QueryOptions(option);
Condition condition = options.get(Condition.class); final Condition condition = options.get(Condition.class);
if (condition == null) { if (condition == null) {
throw new DataAccessException("request a gets without any condition"); throw new DataAccessException("request a gets without any condition");
} }

View File

@ -1,10 +1,10 @@
package org.kar.archidata.model; package org.kar.archidata.model;
import org.kar.archidata.annotation.DataComment;
import org.kar.archidata.annotation.DataIfNotExists; import org.kar.archidata.annotation.DataIfNotExists;
import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonInclude;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.persistence.Column; import jakarta.persistence.Column;
import jakarta.persistence.Table; import jakarta.persistence.Table;
@ -14,12 +14,12 @@ import jakarta.persistence.Table;
public class Data extends GenericDataSoftDelete { public class Data extends GenericDataSoftDelete {
@Column(length = 128, nullable = false) @Column(length = 128, nullable = false)
@DataComment("Sha512 of the data") @Schema(description = "Sha512 of the data")
public String sha512; public String sha512;
@Column(length = 128, nullable = false) @Column(length = 128, nullable = false)
@DataComment("Mime -type of the media") @Schema(description = "Mime -type of the media")
public String mimeType; public String mimeType;
@Column(nullable = false) @Column(nullable = false)
@DataComment("Size in Byte of the data") @Schema(description = "Size in Byte of the data")
public Long size; public Long size;
} }

View File

@ -3,10 +3,10 @@ package org.kar.archidata.model;
import java.util.Date; import java.util.Date;
import org.kar.archidata.annotation.CreationTimestamp; import org.kar.archidata.annotation.CreationTimestamp;
import org.kar.archidata.annotation.DataComment;
import org.kar.archidata.annotation.DataNotRead; import org.kar.archidata.annotation.DataNotRead;
import org.kar.archidata.annotation.UpdateTimestamp; import org.kar.archidata.annotation.UpdateTimestamp;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.persistence.Column; import jakarta.persistence.Column;
import jakarta.persistence.GeneratedValue; import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType; import jakarta.persistence.GenerationType;
@ -19,20 +19,20 @@ public class GenericData {
@Id @Id
@GeneratedValue(strategy = GenerationType.IDENTITY) @GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(nullable = false, unique = true) @Column(nullable = false, unique = true)
@DataComment("Primary key of the base") @Schema(description = "Unique Id of the object", required = false, readOnly = true)
public Long id = null; public Long id = null;
@DataNotRead @DataNotRead
@CreationTimestamp @CreationTimestamp
@Column(nullable = false) @Column(nullable = false)
@Temporal(TemporalType.TIMESTAMP) @Temporal(TemporalType.TIMESTAMP)
@DataComment("Create time of the object")
@NotNull @NotNull
@Schema(description = "Create time of the object", required = false, example = "2000-01-23T01:23:45.678+01:00", readOnly = true)
public Date createdAt = null; public Date createdAt = null;
@DataNotRead @DataNotRead
@UpdateTimestamp @UpdateTimestamp
@Column(nullable = false) @Column(nullable = false)
@Temporal(TemporalType.TIMESTAMP) @Temporal(TemporalType.TIMESTAMP)
@DataComment("When update the object")
@NotNull @NotNull
@Schema(description = "When update the object", required = false, example = "2000-01-23T00:23:45.678Z", readOnly = true)
public Date updatedAt = null; public Date updatedAt = null;
} }

View File

@ -1,10 +1,10 @@
package org.kar.archidata.model; package org.kar.archidata.model;
import org.kar.archidata.annotation.DataComment;
import org.kar.archidata.annotation.DataDefault; import org.kar.archidata.annotation.DataDefault;
import org.kar.archidata.annotation.DataDeleted; 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 jakarta.persistence.Column; import jakarta.persistence.Column;
import jakarta.validation.constraints.NotNull; import jakarta.validation.constraints.NotNull;
@ -13,7 +13,7 @@ public class GenericDataSoftDelete extends GenericData {
@Column(nullable = false) @Column(nullable = false)
@DataDefault("'0'") @DataDefault("'0'")
@DataDeleted @DataDeleted
@DataComment("When delete, they are not removed, they are just set in a deleted state")
@NotNull @NotNull
@Schema(description = "Deleted state", hidden = true, required = false, readOnly = true)
public Boolean deleted = null; public Boolean deleted = null;
} }