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

validate request body against openapi schema #104

Merged
merged 3 commits into from
Aug 25, 2020

Conversation

cgardens
Copy link
Contributor

What

The API was not actually validating that the incoming request body matched the schema described in our open api config.

How

  • That needs to be explicitly turned using jersey-bean-validation.
  • Added exception mapper to describe how the request body failed to match the spec.

@Provider
public class InvalidInputExceptionMapper implements ExceptionMapper<JsonMappingException> {
public class InvalidInputExceptionMapper implements ExceptionMapper<ConstraintViolationException> {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

essentially i took the original class of this name and moved it to InvalidJsonInputExceptionMapper.java. the original class is handling exceptions that arise in the jackson json parsing layers.

this new class is handling errors for when the request body does not match the described open api spec.

.put("message", "The received object did not pass validation")
.put("details", e.getOriginalMessage())
.toString())
.put("message", "Invalid JSON")
Copy link
Contributor

Choose a reason for hiding this comment

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

I this the right message?

Copy link
Contributor

Choose a reason for hiding this comment

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

shoudln't it be "The received object did not pass validation"?

.entity(
new ObjectMapper()
.createObjectNode()
.put("message", "The received object did not pass validation")
Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't it be Invalid JSON

@cgardens cgardens merged commit fb33701 into master Aug 25, 2020
@cgardens cgardens deleted the cgardens/validate_request_body branch August 25, 2020 15:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants