From c76a6ed3f5fdd8292d22ed1c1e9d16e9533cec6b Mon Sep 17 00:00:00 2001 From: Manuel Rafeli Date: Fri, 23 Aug 2024 15:38:53 +0200 Subject: [PATCH] chore: using ProblemError defined in selfcare-common (#180) --- apps/user-ms/src/main/docs/openapi.json | 79 +++++++++++++++++-- apps/user-ms/src/main/docs/openapi.yaml | 51 +++++++++++- .../user/controller/UserController.java | 11 ++- .../exception/handler/ExceptionHandler.java | 24 +----- 4 files changed, 133 insertions(+), 32 deletions(-) diff --git a/apps/user-ms/src/main/docs/openapi.json b/apps/user-ms/src/main/docs/openapi.json index ca54b6f2..cc7b74a0 100644 --- a/apps/user-ms/src/main/docs/openapi.json +++ b/apps/user-ms/src/main/docs/openapi.json @@ -825,17 +825,48 @@ } } ], "responses" : { - "200" : { - "description" : "OK", + "204" : { + "description" : "No Content" + }, + "400" : { + "description" : "Bad Request", "content" : { - "application/json" : { } + "application/json" : { + "schema" : { + "$ref" : "#/components/schemas/Problem" + } + } } }, "401" : { - "description" : "Not Authorized" + "description" : "Not Authorized", + "content" : { + "application/json" : { + "schema" : { + "$ref" : "#/components/schemas/Problem" + } + } + } }, "403" : { - "description" : "Not Allowed" + "description" : "Forbidden", + "content" : { + "application/json" : { + "schema" : { + "$ref" : "#/components/schemas/Problem" + } + } + } + }, + "404" : { + "description" : "Not Found", + "content" : { + "application/json" : { + "schema" : { + "$ref" : "#/components/schemas/Problem" + } + } + } } }, "security" : [ { @@ -1231,6 +1262,17 @@ "enum" : [ "ROOT", "DEV", "COLL", "PROD" ], "type" : "string" }, + "InvalidParam" : { + "type" : "object", + "properties" : { + "name" : { + "type" : "string" + }, + "reason" : { + "type" : "string" + } + } + }, "LocalDateTime" : { "format" : "date-time", "type" : "string", @@ -1316,6 +1358,33 @@ "enum" : [ "ADMIN", "ANY" ], "type" : "string" }, + "Problem" : { + "type" : "object", + "properties" : { + "detail" : { + "type" : "string" + }, + "instance" : { + "type" : "string" + }, + "invalidParams" : { + "type" : "array", + "items" : { + "$ref" : "#/components/schemas/InvalidParam" + } + }, + "status" : { + "format" : "int32", + "type" : "integer" + }, + "title" : { + "type" : "string" + }, + "type" : { + "type" : "string" + } + } + }, "Product" : { "required" : [ "productId", "role", "productRoles" ], "type" : "object", diff --git a/apps/user-ms/src/main/docs/openapi.yaml b/apps/user-ms/src/main/docs/openapi.yaml index 6e0d13a0..bc9baffb 100644 --- a/apps/user-ms/src/main/docs/openapi.yaml +++ b/apps/user-ms/src/main/docs/openapi.yaml @@ -577,14 +577,32 @@ paths: schema: $ref: "#/components/schemas/OnboardedProductState" responses: - "200": - description: OK + "204": + description: No Content + "400": + description: Bad Request content: - application/json: {} + application/json: + schema: + $ref: "#/components/schemas/Problem" "401": description: Not Authorized + content: + application/json: + schema: + $ref: "#/components/schemas/Problem" "403": - description: Not Allowed + description: Forbidden + content: + application/json: + schema: + $ref: "#/components/schemas/Problem" + "404": + description: Not Found + content: + application/json: + schema: + $ref: "#/components/schemas/Problem" security: - SecurityScheme: [] /users/{id}/user-registry: @@ -871,6 +889,13 @@ components: - COLL - PROD type: string + InvalidParam: + type: object + properties: + name: + type: string + reason: + type: string LocalDateTime: format: date-time type: string @@ -943,6 +968,24 @@ components: - ADMIN - ANY type: string + Problem: + type: object + properties: + detail: + type: string + instance: + type: string + invalidParams: + type: array + items: + $ref: "#/components/schemas/InvalidParam" + status: + format: int32 + type: integer + title: + type: string + type: + type: string Product: required: - productId diff --git a/apps/user-ms/src/main/java/it/pagopa/selfcare/user/controller/UserController.java b/apps/user-ms/src/main/java/it/pagopa/selfcare/user/controller/UserController.java index cc1b6392..2ccd21a4 100644 --- a/apps/user-ms/src/main/java/it/pagopa/selfcare/user/controller/UserController.java +++ b/apps/user-ms/src/main/java/it/pagopa/selfcare/user/controller/UserController.java @@ -13,7 +13,6 @@ import it.pagopa.selfcare.user.mapper.UserMapper; import it.pagopa.selfcare.user.model.LoggedUser; import it.pagopa.selfcare.user.model.UpdateUserRequest; -import it.pagopa.selfcare.user.controller.response.UserInstitutionWithActions; import it.pagopa.selfcare.user.model.constants.OnboardedProductState; import it.pagopa.selfcare.user.service.UserRegistryService; import it.pagopa.selfcare.user.service.UserService; @@ -30,11 +29,14 @@ import lombok.extern.slf4j.Slf4j; import org.apache.http.HttpStatus; import org.eclipse.microprofile.openapi.annotations.Operation; +import org.eclipse.microprofile.openapi.annotations.media.Content; +import org.eclipse.microprofile.openapi.annotations.media.Schema; import org.eclipse.microprofile.openapi.annotations.parameters.RequestBody; import org.eclipse.microprofile.openapi.annotations.responses.APIResponse; import org.eclipse.microprofile.openapi.annotations.responses.APIResponses; import org.eclipse.microprofile.openapi.annotations.tags.Tag; import org.jboss.resteasy.reactive.ResponseStatus; +import org.openapi.quarkus.user_registry_json.model.Problem; import java.util.List; @@ -153,6 +155,13 @@ public Uni deleteProducts(@PathParam(value = "userId") String userId, * @return A uni<response> */ @Operation(summary = "Update user status with optional filter for institution, product, role and productRole") + @APIResponses(value = { + @APIResponse(responseCode = "204", description = "No Content"), + @APIResponse(responseCode = "400", description = "Bad Request", content = @Content(schema = @Schema(implementation = Problem.class))), + @APIResponse(responseCode = "401", description = "Not Authorized", content = @Content(schema = @Schema(implementation = Problem.class))), + @APIResponse(responseCode = "403", description = "Forbidden", content = @Content(schema = @Schema(implementation = Problem.class))), + @APIResponse(responseCode = "404", description = "Not Found", content = @Content(schema = @Schema(implementation = Problem.class))) + }) @PUT @Path(value = "/{id}/status") @Produces(MediaType.APPLICATION_JSON) diff --git a/apps/user-ms/src/main/java/it/pagopa/selfcare/user/exception/handler/ExceptionHandler.java b/apps/user-ms/src/main/java/it/pagopa/selfcare/user/exception/handler/ExceptionHandler.java index 1d7a15f3..df438363 100644 --- a/apps/user-ms/src/main/java/it/pagopa/selfcare/user/exception/handler/ExceptionHandler.java +++ b/apps/user-ms/src/main/java/it/pagopa/selfcare/user/exception/handler/ExceptionHandler.java @@ -2,18 +2,14 @@ import it.pagopa.selfcare.user.exception.InvalidRequestException; import it.pagopa.selfcare.user.exception.ResourceNotFoundException; -import it.pagopa.selfcare.user.model.error.Problem; -import it.pagopa.selfcare.user.model.error.ProblemError; import jakarta.ws.rs.core.Response; import org.apache.http.HttpStatus; import org.jboss.resteasy.reactive.RestResponse; import org.jboss.resteasy.reactive.server.ServerExceptionMapper; +import org.openapi.quarkus.user_registry_json.model.Problem; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.util.ArrayList; -import java.util.List; - public class ExceptionHandler { @@ -35,23 +31,7 @@ public RestResponse toResponse(Exception exception) { @ServerExceptionMapper public Response toResponse(ResourceNotFoundException exception) { LOGGER.error("{}: {}", SOMETHING_HAS_GONE_WRONG_IN_THE_SERVER, exception.getMessage()); - Problem problem = createProblem(exception.getMessage(), HttpStatus.SC_NOT_FOUND, exception.getCode()); + Problem problem = new Problem(exception.getMessage(), null, null, HttpStatus.SC_NOT_FOUND, exception.getMessage(), null); return Response.status(Response.Status.NOT_FOUND).entity(problem).build(); } - - private Problem createProblem(String errorMessage, Integer status, String code) { - Problem problem = new Problem(); - problem.setStatus(status); - problem.setErrors(createProblemError(errorMessage,code)); - return problem; - } - - private List createProblemError(String message, String code) { - List list = new ArrayList<>(); - list.add(ProblemError.builder() - .code(code) - .detail(message) - .build()); - return list; - } }