-
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
5
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
5 commits
Select commit
Hold shift + click to select a range
afc09aa
Voeg regel toe voor error structuur bij Bad Requests
TimvdLippe 56ee465
Beheer: update Quarkus example met invalide query parameters
TimvdLippe 0e521e5
Maak Quarkus voorbeeld compleet
TimvdLippe 9e18986
Gebruik hoofdletter RFC notatie
TimvdLippe f727644
Start met Node ExpressJS implementatie
TimvdLippe 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
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
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
84 changes: 84 additions & 0 deletions
84
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,84 @@ | ||
| 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(nullable = true, description = "A locator for the offending value") | ||
| @JsonInclude(value = JsonInclude.Include.NON_NULL) | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Zo zorg je ervoor dat als er geen location is, dat als je |
||
| 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; | ||
|
|
||
| @Schema( | ||
| nullable = true, | ||
| description = "A short, stable machine-readable code as a rule identifier") | ||
| @JsonInclude(value = JsonInclude.Include.NON_NULL) | ||
| public String code; | ||
|
|
||
| private BadRequestDetails( | ||
| BadRequestLocation in, String detail, String location, Integer index, String code) { | ||
| this.in = in; | ||
| this.detail = detail; | ||
| this.location = location; | ||
| this.index = index; | ||
| this.code = code; | ||
| } | ||
|
|
||
| public static BadRequestDetails forSingleQuery( | ||
| String queryParameterName, String detail, String code) { | ||
| return new BadRequestDetails( | ||
| BadRequestLocation.Query, detail, queryParameterName, null, code); | ||
| } | ||
|
|
||
| public static BadRequestDetails forIndexedQuery( | ||
| String queryParameterName, String detail, Integer index, String code) { | ||
| return new BadRequestDetails( | ||
| BadRequestLocation.Query, detail, queryParameterName, index, code); | ||
| } | ||
|
|
||
| public static BadRequestDetails forBody(String location, String detail, String code) { | ||
| return new BadRequestDetails(BadRequestLocation.Body, detail, location, null, code); | ||
| } | ||
| } | ||
|
|
||
| public enum BadRequestLocation { | ||
| @JsonProperty("body") | ||
| Body, | ||
| @JsonProperty("query") | ||
| Query, | ||
| ; | ||
| } | ||
| } | ||
75 changes: 65 additions & 10 deletions
75
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,90 @@ | ||
| 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.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| @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("queryInput") @Separator(",") List<String> queryInputParam, | ||
| PostRequestyBody body) { | ||
| var errors = new ArrayList<BadRequestProblem.BadRequestDetails>(); | ||
| if (queryInputParam.isEmpty()) { | ||
| errors.add( | ||
| BadRequestProblem.BadRequestDetails.forSingleQuery( | ||
| "queryInput", | ||
| "Missing required query parameter", | ||
| "input.query.missing")); | ||
| } | ||
| var queryParams = queryInputParam.iterator(); | ||
| for (var index = 0; queryParams.hasNext(); index++) { | ||
| var param = queryParams.next(); | ||
| if (!param.equals("42")) { | ||
| errors.add( | ||
| BadRequestProblem.BadRequestDetails.forIndexedQuery( | ||
| "queryInput", | ||
| "Value for query parameter is not 42, but was %s".formatted(param), | ||
| index, | ||
| "input.query.invalid")); | ||
| } | ||
| } | ||
| if (!body.field.equals("foo")) { | ||
| errors.add( | ||
| BadRequestProblem.BadRequestDetails.forBody( | ||
| "field", | ||
| "Value for field in body should be foo, but was %s" | ||
| .formatted(body.field), | ||
| "input.body.field.invalid")); | ||
| } | ||
| if (!errors.isEmpty()) { | ||
| throw new BadRequestProblem("Failed to process request input", errors); | ||
| } | ||
| return "Successful response"; | ||
| } | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.