added tests and enabled the schema validation for upload
This commit is contained in:
@@ -5,17 +5,11 @@ import java.lang.annotation.Annotation;
|
||||
import java.lang.reflect.Type;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import jakarta.ws.rs.Consumes;
|
||||
import jakarta.ws.rs.WebApplicationException;
|
||||
import jakarta.ws.rs.core.MediaType;
|
||||
import jakarta.ws.rs.core.MultivaluedMap;
|
||||
import jakarta.ws.rs.ext.MessageBodyReader;
|
||||
import jakarta.ws.rs.ext.Provider;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
@@ -25,6 +19,15 @@ import com.networknt.schema.JsonSchema;
|
||||
import com.networknt.schema.JsonSchemaFactory;
|
||||
import com.networknt.schema.ValidationMessage;
|
||||
|
||||
import jakarta.ws.rs.Consumes;
|
||||
import jakarta.ws.rs.WebApplicationException;
|
||||
import jakarta.ws.rs.core.MediaType;
|
||||
import jakarta.ws.rs.core.MultivaluedMap;
|
||||
import jakarta.ws.rs.core.Response;
|
||||
import jakarta.ws.rs.core.Response.Status;
|
||||
import jakarta.ws.rs.ext.MessageBodyReader;
|
||||
import jakarta.ws.rs.ext.Provider;
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
@@ -65,8 +68,9 @@ public class ValidatedMessageBodyReader implements MessageBodyReader<SchemaValid
|
||||
Optional<JsonSchemaValidate> annotation = Arrays.stream(annotations).filter(a -> a.annotationType() == JsonSchemaValidate.class).map(a -> (JsonSchemaValidate) a).findAny();
|
||||
if (annotation.isPresent()) {
|
||||
ValidationReply reply = validate(annotation.get(), jsonData);
|
||||
if (!reply.success) {
|
||||
throw new WebApplicationException(reply.getErrorResponse());
|
||||
if (!reply.success()) {
|
||||
var response = Response.status(Status.BAD_REQUEST).entity(reply.errors()).build();
|
||||
throw new WebApplicationException(response);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -87,12 +91,12 @@ public class ValidatedMessageBodyReader implements MessageBodyReader<SchemaValid
|
||||
Set<ValidationMessage> errors = schema.validate(node);
|
||||
if (!errors.isEmpty()) {
|
||||
LOG.error("Failed to validate json to schema " + schemaPath);
|
||||
errors.stream().forEach(LOG::error);
|
||||
errors.forEach(LOG::error);
|
||||
}
|
||||
return new ValidationReply.Builder().success(errors.isEmpty()).errors(errors).build();
|
||||
return new ValidationReply(errors.isEmpty(), errors);
|
||||
} catch (IOException e) {
|
||||
LOG.error(e.getMessage(), e);
|
||||
return new ValidationReply.Builder().success(false).build();
|
||||
return new ValidationReply(false, new HashSet<>());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,11 +1,7 @@
|
||||
package marketing.heyday.hartmann.fotodocumentation.rest.jackson;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import jakarta.ws.rs.core.Response;
|
||||
import jakarta.ws.rs.core.Response.Status;
|
||||
|
||||
import com.networknt.schema.ValidationMessage;
|
||||
|
||||
/**
|
||||
@@ -17,35 +13,5 @@ import com.networknt.schema.ValidationMessage;
|
||||
*
|
||||
* created: Feb 14, 2017
|
||||
*/
|
||||
public final class ValidationReply {
|
||||
public final boolean success;
|
||||
public final Set<ValidationMessage> errors = new HashSet<>();
|
||||
|
||||
private ValidationReply(boolean success, Set<ValidationMessage> errors) {
|
||||
this.success = success;
|
||||
this.errors.addAll(errors);
|
||||
}
|
||||
|
||||
public Response getErrorResponse() {
|
||||
return Response.status(Status.BAD_REQUEST).entity(errors).build();
|
||||
}
|
||||
|
||||
public static final class Builder {
|
||||
private boolean success;
|
||||
private Set<ValidationMessage> errors = new HashSet<>();
|
||||
|
||||
public Builder success(boolean success) {
|
||||
this.success = success;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder errors(Set<ValidationMessage> errors) {
|
||||
this.errors = errors;
|
||||
return this;
|
||||
}
|
||||
|
||||
public ValidationReply build() {
|
||||
return new ValidationReply(success, errors);
|
||||
}
|
||||
}
|
||||
public record ValidationReply(boolean success, Set<ValidationMessage> errors) {
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package marketing.heyday.hartmann.fotodocumentation.rest.vo;
|
||||
import java.util.Date;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import marketing.heyday.hartmann.fotodocumentation.rest.jackson.SchemaValidated;
|
||||
|
||||
/**
|
||||
*
|
||||
@@ -14,6 +15,6 @@ import io.swagger.v3.oas.annotations.media.Schema;
|
||||
* created: 19 Jan 2026
|
||||
*/
|
||||
@Schema(name = "CustomerPictureUpload")
|
||||
public record CustomerPictureValue(String username, String pharmacyName, String customerNumber, Date date, String comment, String category, String base64String) {
|
||||
public record CustomerPictureValue(String username, String pharmacyName, String customerNumber, Date date, String comment, String category, String base64String) implements SchemaValidated {
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user