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

Allow external crates to add their own JsonSchema impls for custom SerializeAs and DeserializeAs types #679

Merged
merged 1 commit into from
Jan 20, 2024

Conversation

swlynch99
Copy link
Contributor

@swlynch99 swlynch99 commented Jan 19, 2024

This is written on top of #676.


Implementing JsonSchema directly on serde_with::Schema<T, TA> works fine when within serde_with. Once you are outside the crate, however, the orphan rules prevent you from adding impls of the form

impl JsonSchema for Schema<T, MyCustomType> { ... }

We would like users to be able to make that impl (or something equivalent) so we need to find a way to make it work. We make this work by adding a new layer of trait indirection.

This PR introduces a new JsonSchemaAs<T> trait, adds a blanket

impl<T, TA> JsonSchema for Schema<T, TA> where TA: JsonSchemaAs<T>

and then changes all the existing impls to be for JsonSchemaAs<T> instead of on Schema<T, TA>.

Now, when a user wants to add json schema support for their own type they can implement JsonSchemaAs<T> for their custom type and the orphan rules will allow it.

As a bonus, this seems to have made the extraordinarily confusing "reached recursion limit" errors that I have been encountering disappear. Which is nice.

Copy link

codecov bot commented Jan 19, 2024

Codecov Report

Attention: 6 lines in your changes are missing coverage. Please review.

Comparison is base (170b6e1) 65.24% compared to head (b0ee3ab) 65.16%.

Files Patch % Lines
serde_with/src/schemars_0_8.rs 50.00% 6 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #679      +/-   ##
==========================================
- Coverage   65.24%   65.16%   -0.09%     
==========================================
  Files          38       38              
  Lines        2256     2268      +12     
==========================================
+ Hits         1472     1478       +6     
- Misses        784      790       +6     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@jonasbb jonasbb marked this pull request as ready for review January 19, 2024 23:45
Implementing JsonSchema directly on serde_with::Schema<T, TA> works fine
when within serde_with. Once you are outside the crate, however, the
orphan rules prevent you from adding impls of the form

impl JsonSchema for Schema<T, MyCustomType> { ... }

We would like users to be able to make that impl (or something
equivalent) so we need to find a way to make it work. We make this work
by adding a new layer of trait indirection.

This commit introduces an new JsonSchemaAs<T> trait, adds a blanket

impl<T, TA> JsonSchema for Schema<T, TA> where TA: JsonSchemaAs<T>

and then changes all the existing impls to be for JsonSchemaAs<T>.

Now, when a user wants to add json schema support for their own type
they can implement JsonSchemaAs<T> for their custom type and the orphan
rules will allow it.

As a bonus, this seems to have made the extraordinarily confusing
"reached recursion limit" errors that I have been encountering
disappear. Which is nice.
@swlynch99
Copy link
Contributor Author

This should be a bit easier to review now that I've rebased onto the latest master :)

@jonasbb jonasbb merged commit 6553804 into jonasbb:master Jan 20, 2024
22 checks passed
@swlynch99 swlynch99 deleted the schemars-ext branch January 21, 2024 20:34
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