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

Avoid emitting a #[schemars(with = "...")] annotation when one already exists #682

Merged
merged 5 commits into from
Jan 23, 2024

Conversation

swlynch99
Copy link
Contributor

The #[schemars] annotation emitted by #[serde_as] will cause errors if the user has already added an existing #[schemars(with = ...)] annotation. This ends up being rather annoying since there's no way to work around the error other than to manually expand what #[serde_as] does.

I ended up running into this when I was trying to convert some of my existing code to use the new integration instead of manually sticking #[schemars(with = "blah")] everywhere.

This PR fixes the issue by making #[serde_as] avoid emitting the schemars attribute if there is an existing one of the field that specifies any one of

  • #[schemars(with = "...")]
  • #[schemars(serialize_with = "...")]
  • #[schemars(deserialize_with = "...")]

The actual code is a bit more involved (e.g. serialize_as doesn't conflict with #[schemars(deserialize_with = "...")]) but that's the gist of it.

I have also included a test case to validate that this works as expected.

The #[schemars] annotation emitted by #[serde_as] will cause errors if
the user has already added an existing #[schemars(with = ...)]
annotation. This ends up being rather annoying since there's no way to
work around the error other than to manually expand what #[serde_as]
does.

This commit fixes the issue by making #[serde_as] avoid emitting a
schemars attribute if there is an existing one of the field that
specifies any one of
- #[schemars(with = "...")]
- #[schemars(serialize_with = "...")]
- #[schemars(deserialize_with = "...")]

The actual code is a bit more involved (e.g. serialize_as doesn't
conflict with #[schemars(deserialize_with = "...")]) but that's the gist
of it.

I have also included a test case to validate that this works as
expected.
Copy link

codecov bot commented Jan 23, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (6ecde3c) 65.16% compared to head (1c9131f) 65.16%.

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #682   +/-   ##
=======================================
  Coverage   65.16%   65.16%           
=======================================
  Files          38       38           
  Lines        2268     2268           
=======================================
  Hits         1478     1478           
  Misses        790      790           

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

@jonasbb jonasbb merged commit 88e9879 into jonasbb:master Jan 23, 2024
23 checks passed
@swlynch99 swlynch99 deleted the schemars-custom-with branch January 23, 2024 20:39
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