⚡ [REST API] Block execution on XML/JSON parsing errors and report 400 error code#4117
Merged
Coduz merged 14 commits intoeclipse-kapua:developfrom Oct 28, 2024
Merged
Conversation
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## develop #4117 +/- ##
=============================================
- Coverage 16.81% 16.80% -0.01%
Complexity 22 22
=============================================
Files 2019 2021 +2
Lines 52437 52463 +26
Branches 4423 4424 +1
=============================================
Hits 8815 8815
- Misses 43224 43250 +26
Partials 398 398
|
dseurotech
approved these changes
Oct 15, 2024
06b7c08 to
d22f274
Compare
d22f274 to
8b9d8d1
Compare
Coduz
approved these changes
Oct 18, 2024
added 14 commits
October 21, 2024 15:53
…nEventHandler to propagate internal exceptions
… throw by XML/JSON parsing
…e propagated in the stack by MOXy
…with a default value
…"Error deserializing object from entity stream" error message upon auth
a4b5820 to
be39606
Compare
dseurotech
approved these changes
Oct 28, 2024
Coduz
approved these changes
Oct 28, 2024
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
With this PR I changed the MOXy exception handling in a way that improves error responses when XMLs/JSONs provided via rest API cannot be parsed correctly.
ACTUAL SITUATION
Currently, in the case when a field in the XML sent via rest API is not parsable, its value is set to null by MOXy and only then the "*Service" class (for example deviceService) perform checks on non-null fields and, eventually, throws an exception.
If this could seem reasonable, it creates some scenarios when this behaviour is wrong.
For example, assume that I want to update the expiration date of a user. I would then call the endpoint /{scopeId}/users/{userId} specifying the new expirationDate field in the request body. If I erroneously set a wrong / not parsable value for it (for example, a wrong random string), the "userService" class will receive the expirationDate value as null, interpreting this a request to set an "unlimited" expiration date (just as If I had made the request without the field, with the purpose to remove the expirationDate of the user)
There are other cases like this, that can be summarized as an impossibility of the code to differentiate the case where we provide a field that cannot be parsed (set "null" by MOXy) from the case when a field has not been provided in the request (set to "null" also in this case) or actually set to null by the client. I wanted to change the code in order to differentiate these cases and be able to alert the client when the provided XML contains not parsable fields OR when a mandatory provided field is missing/null.
PROVIDED SOLUTION
I changed our MOXy configuration introducing a custom "MoxyJsonProvider" that tweaks the internal "validationEventHandler". In this way, a different exception handling is performed internally and exceptions are propagated to the stack instead of being caught. These exception are then handled by the "EclipseLinkExceptionMapper" Class in order to return to the client an appropriate response.
EXAMPLE RESPONSE IN CASE OF PARSING ERRORS
In the example of a user update as presented above, assuming that we are updating a user that has an expiration date in the future, this is what happens now:
Before this PR, this request would have removed the expiration date of this user