From 75e52b9208fc9e72b3cd37f9bb315e515fe40c1b Mon Sep 17 00:00:00 2001 From: Thomas Georgios Giannos Date: Tue, 11 Jun 2024 15:08:08 +0300 Subject: [PATCH] Adding validation error api responses in swagger pages --- .../controllers/DescriptionController.java | 5 +++ .../opencdmp/controllers/DmpController.java | 4 +++ .../controllers/swagger/SwaggerHelpers.java | 18 ++++++++-- .../swagger/annotation/Swagger400.java | 33 +++++++++++++++++++ .../swagger/annotation/Swagger403.java | 2 +- 5 files changed, 59 insertions(+), 3 deletions(-) create mode 100644 backend/web/src/main/java/org/opencdmp/controllers/swagger/annotation/Swagger400.java diff --git a/backend/web/src/main/java/org/opencdmp/controllers/DescriptionController.java b/backend/web/src/main/java/org/opencdmp/controllers/DescriptionController.java index e49571637..bf7dd2b80 100644 --- a/backend/web/src/main/java/org/opencdmp/controllers/DescriptionController.java +++ b/backend/web/src/main/java/org/opencdmp/controllers/DescriptionController.java @@ -25,6 +25,7 @@ import org.opencdmp.commons.enums.DmpAccessType; import org.opencdmp.commons.enums.DmpStatus; import org.opencdmp.commons.enums.IsActive; import org.opencdmp.controllers.swagger.SwaggerHelpers; +import org.opencdmp.controllers.swagger.annotation.Swagger400; import org.opencdmp.controllers.swagger.annotation.Swagger404; import org.opencdmp.controllers.swagger.annotation.SwaggerErrorResponses; import org.opencdmp.convention.ConventionService; @@ -229,6 +230,7 @@ public class DescriptionController { @PostMapping("persist") @Operation(summary = "Create a new or update an existing description") + @Swagger400 @Swagger404 @Transactional @ValidationFilterAnnotation(validator = DescriptionPersist.DescriptionPersistValidator.ValidatorName, argumentName = "model") @@ -251,6 +253,7 @@ public class DescriptionController { @PostMapping("persist-status") @Operation(summary = "Update the status of an existing description") + @Swagger400 @Swagger404 @Transactional @ValidationFilterAnnotation(validator = DescriptionStatusPersist.DescriptionStatusPersistValidator.ValidatorName, argumentName = "model") @@ -331,6 +334,7 @@ public class DescriptionController { @PostMapping("field-file/upload") @Operation(summary = "Upload a file attachment on a field that supports it") + @Swagger400 @Swagger404 @Transactional @ValidationFilterAnnotation(validator = DescriptionFieldFilePersist.PersistValidator.ValidatorName, argumentName = "model") @@ -381,6 +385,7 @@ public class DescriptionController { @PostMapping("update-description-template") @Operation(summary = "Change the template of a description") + @Swagger400 @Swagger404 @Transactional @ValidationFilterAnnotation(validator = UpdateDescriptionTemplatePersist.UpdateDescriptionTemplatePersistValidator.ValidatorName, argumentName = "model") diff --git a/backend/web/src/main/java/org/opencdmp/controllers/DmpController.java b/backend/web/src/main/java/org/opencdmp/controllers/DmpController.java index 3aff64573..15c84dfa5 100644 --- a/backend/web/src/main/java/org/opencdmp/controllers/DmpController.java +++ b/backend/web/src/main/java/org/opencdmp/controllers/DmpController.java @@ -25,6 +25,7 @@ import org.opencdmp.commons.enums.DmpAccessType; import org.opencdmp.commons.enums.DmpStatus; import org.opencdmp.commons.enums.IsActive; import org.opencdmp.controllers.swagger.SwaggerHelpers; +import org.opencdmp.controllers.swagger.annotation.Swagger400; import org.opencdmp.controllers.swagger.annotation.Swagger404; import org.opencdmp.controllers.swagger.annotation.SwaggerErrorResponses; import org.opencdmp.model.DescriptionsToBeFinalized; @@ -217,6 +218,7 @@ public class DmpController { @PostMapping("persist") @Operation(summary = "Create a new or update an existing plan") + @Swagger400 @Swagger404 @Transactional @ValidationFilterAnnotation(validator = DmpPersist.DmpPersistValidator.ValidatorName, argumentName = "model") @@ -310,6 +312,7 @@ public class DmpController { @PostMapping("clone") @Operation(summary = "Create a clone of an existing plan") + @Swagger400 @Swagger404 @Transactional @ValidationFilterAnnotation(validator = CloneDmpPersist.CloneDmpPersistValidator.ValidatorName, argumentName = "model") @@ -333,6 +336,7 @@ public class DmpController { @PostMapping("new-version") @Operation(summary = "Create a new version of an existing plan") + @Swagger400 @Swagger404 @Transactional @ValidationFilterAnnotation(validator = NewVersionDmpPersist.NewVersionDmpPersistValidator.ValidatorName, argumentName = "model") diff --git a/backend/web/src/main/java/org/opencdmp/controllers/swagger/SwaggerHelpers.java b/backend/web/src/main/java/org/opencdmp/controllers/swagger/SwaggerHelpers.java index 6f12eb981..718532cfd 100644 --- a/backend/web/src/main/java/org/opencdmp/controllers/swagger/SwaggerHelpers.java +++ b/backend/web/src/main/java/org/opencdmp/controllers/swagger/SwaggerHelpers.java @@ -12,11 +12,25 @@ public class SwaggerHelpers { public static class Errors { + public static final String message_400 = + """ + { + "code": 400, + "message" "validation error", + "errors": { + "key": "name", + "message": [ + "name is required" + ] + } + } + """; + public static final String message_403 = """ { - "code": 403 - "message" null + "code": 403, + "message": null } """; diff --git a/backend/web/src/main/java/org/opencdmp/controllers/swagger/annotation/Swagger400.java b/backend/web/src/main/java/org/opencdmp/controllers/swagger/annotation/Swagger400.java new file mode 100644 index 000000000..23f81df6d --- /dev/null +++ b/backend/web/src/main/java/org/opencdmp/controllers/swagger/annotation/Swagger400.java @@ -0,0 +1,33 @@ +package org.opencdmp.controllers.swagger.annotation; + +import io.swagger.v3.oas.annotations.media.Content; +import io.swagger.v3.oas.annotations.media.ExampleObject; +import io.swagger.v3.oas.annotations.responses.ApiResponse; + +import java.lang.annotation.*; + +import static org.opencdmp.controllers.swagger.SwaggerHelpers.Errors.message_400; + +@Documented +@Retention(RetentionPolicy.RUNTIME) +@Target({ElementType.TYPE, ElementType.FIELD, ElementType.METHOD}) +@ApiResponse( + description = "This is generally the response you should expect when you don't provide correct information and a validation error occurs.", + responseCode = "400", + content = { + @Content( + examples = { + @ExampleObject( + name = "400 example response", + description = "This could be a response in case of a 400 bad request error. " + + "It should contain a list of all the data requirements that are not fulfilled. " + + "In our example, the property 'name' is mandatory and was not provided in the request body, resulting in the validation error above.", + value = message_400 + ) + } + ) + } +) +public @interface Swagger400 { + +} diff --git a/backend/web/src/main/java/org/opencdmp/controllers/swagger/annotation/Swagger403.java b/backend/web/src/main/java/org/opencdmp/controllers/swagger/annotation/Swagger403.java index d021d7182..0b919f10b 100644 --- a/backend/web/src/main/java/org/opencdmp/controllers/swagger/annotation/Swagger403.java +++ b/backend/web/src/main/java/org/opencdmp/controllers/swagger/annotation/Swagger403.java @@ -18,7 +18,7 @@ import static org.opencdmp.controllers.swagger.SwaggerHelpers.Errors.message_403 @Content( examples = { @ExampleObject( - name = "404 response", + name = "403 response", description = "This is the response in case of a 403 error.", value = message_403 )