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

Implement JsonSchemaAs for EnumMap #697

Merged
merged 1 commit into from
Feb 11, 2024

Conversation

swlynch99
Copy link
Contributor

It's been a little busy but I'm back with yet another PR :)

This is the first one that really involves a custom deserialize implementation. EnumMap involves transforming a list of enums to a map and to generate its schema we end up needing to do the same to the schema.

Basically, we end up transforming

{
  "type": "object",
  "oneOf": [
    { "properties": { "A": "..." } },
    { "properties": { "B": "..." } }
  ]
}       

into

{
  "type": "object",
  "properties": {
    "A": "...",
    "B": "..."
  }
}

I have also included a number of tests that I think should cover most of the different cases.

Notes

  • If the schema for the inner type doesn't match what would be expected for a rust enum then we'll end up generating an invalid overall schema. I don't think this is an issue since it doesn't appear that EnumMap itself works in this case?

This JsonSchemaAs impl is the first one that really is a custom impl.
EnumMap turns a vec of enums into an key-value map and so in order to
create its schema we end up needing to do the same.
Copy link

codecov bot commented Feb 10, 2024

Codecov Report

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

Comparison is base (1be8fa3) 65.41% compared to head (2a65f20) 65.64%.
Report is 8 commits behind head on master.

Files Patch % Lines
serde_with/src/schemars_0_8.rs 80.95% 4 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #697      +/-   ##
==========================================
+ Coverage   65.41%   65.64%   +0.22%     
==========================================
  Files          38       38              
  Lines        2322     2343      +21     
==========================================
+ Hits         1519     1538      +19     
- Misses        803      805       +2     

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

@jonasbb
Copy link
Owner

jonasbb commented Feb 11, 2024

Hey, thank you for that. The transformation looks good. I think it is fine to not handle schema mismatches for the inner enum. At best, you could detect that something is going on and raise an error. The json_schema function is infallible, so it is not possible to nicely pass an error. Panicing would be a worse choice. EnumMap can only handle default style enums. I don't think there is much error handling in that case either, since nothing can be done really.

@jonasbb jonasbb merged commit 9de54be into jonasbb:master Feb 11, 2024
22 checks passed
@swlynch99 swlynch99 deleted the schemars-enum-map branch March 3, 2024 00:04
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