Skip to content
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

Fix bad request handling #179

Merged
merged 20 commits into from
Oct 25, 2024
Merged

Fix bad request handling #179

merged 20 commits into from
Oct 25, 2024

Conversation

HamzaJugon
Copy link
Contributor

Fixes #

  • Added error handling for bad requests with incorrect sort parameters
  • Added automatic pagination handling in the /unreferenced-paths endpoint for improved Swagger documentation.
  • Updated the Maven Central release workflow to run exclusively from the main branch.

Hamza Jugon added 5 commits October 23, 2024 12:35
As used in the metadata endpoint we want Swagger to be  able to interpret and automatically generate pagination parameters like page, size, and sort.
@@ -106,7 +106,7 @@ public ResponseEntity<Page<HousekeepingPathResponse>> getAllPaths(
@Spec(path = "cleanupTimestamp", params = "deleted_after", spec = GreaterThan.class),
@Spec(path = "creationTimestamp", params = "registered_before", spec = LessThan.class),
@Spec(path = "creationTimestamp", params = "registered_after", spec = GreaterThan.class) }) Specification<HousekeepingPath> spec,
Pageable pageable) {
@ParameterObject Pageable pageable) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added @ParameterObject to the Pageable parameter, this allows Swagger to automatically generate and display pagination parameters (page, size, sort) for /unreferenced-paths. This will make it consistent with /metadata and reduces manual parameter definitions.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isn't this for requests params e.g. url/bla?myRequestParam=foo
I thought the Pageable is part of the request body in this API?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Pageable parameter is populated from url query params. The GET request won't have a request body but the json format in swagger is misleading . .

The @ParameterObject will ensure swagger generates better documentation for the query params, we now have separate fields for Page, Size & Sort. It also resolves the Sort filter issue by making it clearer and ensuring it's optional by default, which wasn’t the case in the JSON format.

We already have this in the /metadata endpoint so not sure why it wasn't done for this one, you can see how the layout differs between endpoints in swagger.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cool thanks for explaning!

@@ -16,8 +13,7 @@ jobs:
uses: actions/checkout@v2
with:
fetch-depth: 0
ref: ${{ github.event.inputs.branch }}
# We need a personal access token to be able to push to a protected branch
ref: main # Hardcoded to main branch
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prior it was possible to push to maven central from any branch which can lead to accidental pushes to maven central

errorResponse.setPath(request.getRequestURI());

return new ResponseEntity<>(errorResponse, HttpStatus.BAD_REQUEST);
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Handles PropertyReferenceException (e.g., invalid sort parameters) globally and returns a custom BAD_REQUEST error response with details.

Hamza Jugon and others added 8 commits October 23, 2024 15:44
Testing the request response and the errorResponses
…or/ErrorResponse.java

Co-authored-by: Jay Green-Stevens <[email protected]>
…keeper/integration/api/BeekeeperApiIntegrationTest.java

Co-authored-by: Jay Green-Stevens <[email protected]>
…or/BeekeeperExceptionHandler.java

Co-authored-by: Jay Green-Stevens <[email protected]>
…or/BeekeeperExceptionHandler.java

Co-authored-by: Jay Green-Stevens <[email protected]>
…or/BeekeeperExceptionHandler.java

Co-authored-by: Jay Green-Stevens <[email protected]>
CHANGELOG.md Outdated Show resolved Hide resolved
.timestamp(LocalDateTime.now().toString())
.status(HttpStatus.BAD_REQUEST.value())
.error(HttpStatus.BAD_REQUEST.getReasonPhrase())
.message("Invalid sort parameter: " + exception.getMessage())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be many things right doesn't always have to be invalid sort parameter. maybe we should just set the exception.message()?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, too specific.
I have updated to make it more general .message(exception.getMessage())

Copy link
Contributor

@patduin patduin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice one!

@HamzaJugon HamzaJugon merged commit 17084bd into main Oct 25, 2024
2 checks passed
@HamzaJugon HamzaJugon deleted the fix/incorrect-request-handling branch October 25, 2024 07:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

4 participants