-
Notifications
You must be signed in to change notification settings - Fork 12
Voeg regel toe voor error structuur bij Bad Requests #310
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
TimvdLippe
wants to merge
2
commits into
develop
Choose a base branch
from
bad-request-errors
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
examples/quarkus/src/main/java/org/acme/BadRequestProblem.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| package org.acme; | ||
|
|
||
| import com.fasterxml.jackson.annotation.JsonInclude; | ||
| import com.fasterxml.jackson.annotation.JsonProperty; | ||
| import io.quarkiverse.resteasy.problem.HttpProblem; | ||
| import jakarta.ws.rs.core.Response; | ||
| import org.eclipse.microprofile.openapi.annotations.media.Schema; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| public class BadRequestProblem extends HttpProblem { | ||
| protected BadRequestProblem(String message, List<BadRequestDetails> errors) { | ||
| super( | ||
| builder() | ||
| .withTitle("Bad hello request") | ||
| .withStatus(Response.Status.BAD_REQUEST) | ||
| .withDetail(message) | ||
| .with("errors", errors)); | ||
| } | ||
|
|
||
| @Schema(name = "errors", description = "All bad request errors") | ||
| public List<BadRequestDetails> errors; | ||
|
|
||
| @Schema( | ||
| name = "BadRequestDetails", | ||
| description = "Request details according to API Design Rules") | ||
| public static class BadRequestDetails { | ||
| @Schema(description = "Where the error occurs") | ||
| public BadRequestLocation in; | ||
|
|
||
| @Schema(description = "Human-readable message describing the violation") | ||
| public String detail; | ||
|
|
||
| @Schema(description = "A locator for the offending value") | ||
| public String location; | ||
|
|
||
| @Schema( | ||
| nullable = true, | ||
| description = | ||
| "A zero-based index position when multiple query parameters have the same name") | ||
| @JsonInclude(value = JsonInclude.Include.NON_NULL) | ||
| public Integer index; | ||
|
|
||
| private BadRequestDetails( | ||
| BadRequestLocation in, String detail, String location, Integer index) { | ||
| this.in = in; | ||
| this.detail = detail; | ||
| this.location = location; | ||
| this.index = index; | ||
| } | ||
|
|
||
| public static BadRequestDetails forQuery( | ||
| BadRequestLocation in, String detail, Integer index) { | ||
| return new BadRequestDetails(in, detail, "query", index); | ||
| } | ||
| } | ||
|
|
||
| public enum BadRequestLocation { | ||
| @JsonProperty("body") | ||
| Body, | ||
| @JsonProperty("query") | ||
| Query, | ||
| ; | ||
| } | ||
| } | ||
64 changes: 54 additions & 10 deletions
64
examples/quarkus/src/main/java/org/acme/GreetingResource.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,35 +1,79 @@ | ||
| package org.acme; | ||
|
|
||
| import jakarta.ws.rs.GET; | ||
| import jakarta.ws.rs.POST; | ||
| import jakarta.ws.rs.Path; | ||
| import jakarta.ws.rs.Produces; | ||
| import jakarta.ws.rs.core.MediaType; | ||
| import org.eclipse.microprofile.openapi.annotations.Operation; | ||
| import org.eclipse.microprofile.openapi.annotations.headers.Header; | ||
| import org.eclipse.microprofile.openapi.annotations.media.Content; | ||
| import org.eclipse.microprofile.openapi.annotations.media.Schema; | ||
| import org.eclipse.microprofile.openapi.annotations.responses.APIResponse; | ||
| import org.eclipse.microprofile.openapi.annotations.tags.Tag; | ||
| import org.jboss.resteasy.reactive.RestQuery; | ||
| import org.jboss.resteasy.reactive.Separator; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| @Path("/hello") | ||
| @Tag(name = "greeting") | ||
| public class GreetingResource { | ||
|
|
||
| @GET | ||
| @Operation( | ||
| description = "Hello world endpoint" | ||
| ) | ||
| @Operation(description = "Hello world endpoint") | ||
| @APIResponse( | ||
| responseCode = "200", | ||
| content = @Content(mediaType = MediaType.TEXT_PLAIN), | ||
| headers = { | ||
| @Header( | ||
| name = OpenApiConstants.API_VERSION_HEADER_NAME, | ||
| schema = @Schema(implementation = String.class) | ||
| ) | ||
| } | ||
| ) | ||
| @Header( | ||
| name = OpenApiConstants.API_VERSION_HEADER_NAME, | ||
| schema = @Schema(implementation = String.class)) | ||
| }) | ||
| public String hello() { | ||
| return "Hello from Quarkus REST"; | ||
| } | ||
|
|
||
| @POST | ||
| @Operation(description = "Test bad request with query parameter") | ||
| @APIResponse( | ||
| responseCode = "200", | ||
| content = @Content(mediaType = MediaType.TEXT_PLAIN), | ||
| headers = { | ||
| @Header( | ||
| name = OpenApiConstants.API_VERSION_HEADER_NAME, | ||
| schema = @Schema(implementation = String.class)) | ||
| }) | ||
| @APIResponse( | ||
| responseCode = "400", | ||
| description = "Bad request response", | ||
| content = | ||
| @Content( | ||
| mediaType = "application/problem+json", | ||
| schema = @Schema(implementation = BadRequestProblem.class))) | ||
| public String withInput(@RestQuery("query") @Separator(",") List<String> queryParam) { | ||
| if (queryParam.isEmpty()) { | ||
| throw new BadRequestProblem( | ||
| "Missing required query parameter", | ||
| List.of( | ||
| BadRequestProblem.BadRequestDetails.forQuery( | ||
| BadRequestProblem.BadRequestLocation.Query, | ||
| "Query parameter is invalid", | ||
| null))); | ||
| } | ||
| var queryParams = queryParam.iterator(); | ||
| for (var index = 0; queryParams.hasNext(); index++) { | ||
| var param = queryParams.next(); | ||
| if (!param.equals("42")) { | ||
| throw new BadRequestProblem( | ||
| "Missing required query parameter", | ||
| List.of( | ||
| BadRequestProblem.BadRequestDetails.forQuery( | ||
| BadRequestProblem.BadRequestLocation.Query, | ||
| "Query parameter is invalid", | ||
| index))); | ||
| } | ||
| } | ||
| return "Successful response"; | ||
| } | ||
| } |
7 changes: 7 additions & 0 deletions
7
linter/testcases/error-type-bad-invalid-input/expected-output.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
|
|
||
| /testcases/error-type-bad-invalid-input/openapi.json | ||
| 119:29 error nlgov:problem-invalid-input GET and DELETE endpoints that have parameters, and all other endpoints must be able to return a 400 response paths./invalid-response-vereist.get.responses | ||
| 157:29 error nlgov:problem-invalid-input GET and DELETE endpoints that have parameters, and all other endpoints must be able to return a 400 response paths./invalid-response-vereist.put.responses | ||
| 195:29 error nlgov:problem-invalid-input GET and DELETE endpoints that have parameters, and all other endpoints must be able to return a 400 response paths./invalid-response-vereist.post.responses | ||
|
|
||
| ✖ 3 problems (3 errors, 0 warnings, 0 infos, 0 hints) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@kad-sprenr Het is 17:00, dus kwam nog niet verder vandaag, maar deze class is een begin. Naar mijn weten gebruiken zowel Quarkus als Spring Boot dezelfde Jakarta classes. Dus het zou mij verbazen als het in Spring Boot niet ook nog werkt.
Later deze week ga ik hier weer aan verder, maar hopelijk helpt dit qua inspiratie.