Adding validation error api responses in swagger pages

This commit is contained in:
Thomas Georgios Giannos 2024-06-11 15:08:08 +03:00
parent c522de12ec
commit 75e52b9208
5 changed files with 59 additions and 3 deletions

View File

@ -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")

View File

@ -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")

View File

@ -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
}
""";

View File

@ -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 {
}

View File

@ -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
)