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

Expose extraneous fields in error in strict decoding #125

Merged
merged 5 commits into from
Feb 26, 2021

Conversation

travisbrown
Copy link
Member

In some cases it can be necessary to know exactly what extraneous fields where encountered when using strict decoding, but currently the API here only indicates those fields in the DecodingFailure error message string (which is really the only thing we can do, given the Decoder and DecodingFailure API, which don't let us return additional structured information like this in the result of failure).

The change in this PR adds a new ExtrasDecoder API that includes a decodeStrict method (on the model of e.g. decodeAccumulating) that also returns a list of extraneous field keys in the case of failure.

I've added new deriveExtrasDecoder, etc. methods that return derived instances statically typed as ExtrasDecoder or ExtrasAsObjectCodec. (I've also included deriveExtrasEncoder for consistency, although it's currently identical to deriveConfiguredEncoder.) We could alternatively just have the derivedConfiguredDecoder methods return this subtype, but while that would be source-compatible, it would break binary compatibility (which probably doesn't matter much for a macro-oriented project like this, but still). In its current form this PR is verified by MiMa to be binary compatible with the 0.13.0 release.

@erwan What do you think?

@erwan
Copy link
Collaborator

erwan commented Feb 23, 2021

So that would be a decoder that fails if there are additional fields in the json, that we're not using?

@travisbrown
Copy link
Member Author

@erwan Yes, that part is already supported, via withStrictDecoding configuration. The issue is that it only tells you the extra fields in the error message string, while it's often useful to have them be programmatically available, which is what this part adds.

@travisbrown
Copy link
Member Author

I'm not sure what's going on with the Scala.js failure on 2.12. I'll take a look tomorrow.

@erwan
Copy link
Collaborator

erwan commented Feb 24, 2021

yes, sounds like a good idea to expose those fields in a more usable way than just the error string 👍

@travisbrown
Copy link
Member Author

That should fix the build issue—I'll go ahead and publish this as an 0.13.1-M1 when green.

@travisbrown travisbrown merged commit 74c4c0e into master Feb 26, 2021
@travisbrown travisbrown deleted the topic/expose-extraneous-fields branch August 26, 2021 09:52
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.

2 participants