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

feat(serde_with_macros): proper handling of cfg_attr and schemars #782

Merged
merged 1 commit into from
Oct 1, 2024

Conversation

sivizius
Copy link
Contributor

@sivizius sivizius commented Sep 21, 2024

With this commit, cfg_attr will be handled properly. The current implementation does not accept e.g.:

#[serde_as]
#[derive(JsonSchema, Serialize)]
struct Test {
    #[serde_as(as = "DisplayFromStr")]
    #[cfg_attr(any(), arbitrary("some" |weird| syntax::<bool, 2>()))]
    #[cfg_attr(any(), schemars(rename = "foo"), schemars(range(min = 0)))]
    custom: i32,
}

Because due to https://github.com/jonasbb/serde_with/blob/master/serde_with_macros/src/utils.rs#L91, the parser does not accept cfg_attr with multiple arguments, which is allowed, see e.g. https://doc.rust-lang.org/reference/conditional-compilation.html#the-cfg_attr-attribute. Another issue I’ve found with arbitrary where some unexpected errors due to this custom parser: https://github.com/jonasbb/serde_with/blob/master/serde_with_macros/src/utils.rs#L191-L218 in combination with this line: https://github.com/jonasbb/serde_with/blob/master/serde_with_macros/src/utils.rs#L240.

Line 240 will only continue, if the outer-most meta if neither cfg_attr or schemars. If however the attribute is cfg_attr, the code after the match-block is reached and the parser will just assume, that the inner meta is schemars and thus fail to parse any meta with some weird syntax.

Even #[schemars(length(min = 1.0))] will fail, because the parser expects all schemars-attributes to be a list of key-value-pairs (foo = bar), thus failing for length(min = 1.0), even though this is perfectly legal (https://graham.cool/schemars/deriving/attributes/#length).

I introduced the type LazyBool to simplify dealing with various conditions.

Copy link

codecov bot commented Sep 21, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 67.34%. Comparing base (378fa24) to head (2d70b70).
Report is 8 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #782      +/-   ##
==========================================
- Coverage   67.45%   67.34%   -0.11%     
==========================================
  Files          40       40              
  Lines        2464     2468       +4     
==========================================
  Hits         1662     1662              
- Misses        802      806       +4     

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

@sivizius sivizius changed the title feat(serde_with_macros): proper handling of cfg_attr feat(serde_with_macros): proper handling of cfg_attr and schemars Sep 23, 2024
@jonasbb
Copy link
Owner

jonasbb commented Oct 1, 2024

Thanks for the PR. In my opinion, it is a bit unfortunate that it is necessary to parse the cfg_attr in the first place, but without that you loose some usability. Since the PR is not so big and this fixes some issues I am fine to merge this. I didn't really check the code in detail, but it looks reasonable overall. Let's hope the test cover enough.

@jonasbb jonasbb merged commit 67326f8 into jonasbb:master Oct 1, 2024
24 checks passed
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