-
Notifications
You must be signed in to change notification settings - Fork 221
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
Refactor enums processing #1059
Conversation
76c47ec
to
6da98ae
Compare
0919b15
to
59270e6
Compare
Does that mean |
Yes, as it is by the OpenAPI spec. |
84c5403
to
55d64c5
Compare
This commit refactors enum processing from ground up. This fully unifies the how each enum variant schema is resolved and the way variant schemas are generated. This will result easier debugging, changing and updating the enum processing in future and most of all the enums now will behave consistently due to removing bunch of duplication and adding correct abstractions instead. This commit also unifies previously known `SimpleEnum` and `ReprEnum` to a single enum to furhter simplify the code. Also `ComplexEnum` is now known by `MixedEnum`. This commit implements discriminator with support for custom mapping. Discriminator can only be used with `#[serde(untagged)]` enum having only unnamed field variants with one schema reference implementing `ToSchema` trait. It cannot be used with primitive types nor with inlined schemas. Removed `#[serde(tag = ...)]` as discriminator support. Update docs and add support for missing features for enum variants such as `Title`, `Deprecated`, `MinProperties` and `MaxProperties`. ### Breaking changes * `#[serde(tag = ...)]` will not be used as discriminator.
55d64c5
to
ac15071
Compare
Then I don't like this solution. I don't think there must be a 1:1 relation between the OpenAPI Schema and Rust code. You normally would not write code like in the following: struct Dog {
pet_type: String
} |
That is true, however the enums work as previously they did, just the This previous "broken" discriminator implementation has caused errors the users have been facing and led to manual mapping and hacks anyway. Thus it is better to remove the broken implementation in the first place. The assessment of supporting |
When you assess the support of It takes the "broken" enums with inline schemas and a discriminator, generates new variant schemas with a nice name (or in some case edits existing schemas), links them in the enum schema and adapts the discriminator on-the-fly. There are tests below which show the results of the transformation. Maybe you would like to integrate something like this in the future. |
That is pretty neat. Thanks you for providing me link. It is always great to see other peoples solutions for emerging inspiration. Sure something like this could be evaluated at least to the level of supporting unnamed field variants having one But implementing this kind of transformer internally should not be too big of a thing though. Nevertheless there is a need to assess whats feasible and whats possible. |
Why? This is the common case for using serde to deserialize API calls with enums. Is there a workaround to not change all our enums to untagged just because of OpenAPI specs? |
This commit refactors enum processing from ground up. This fully unifies the how each enum variant schema is resolved and the way variant schemas are generated. This will result easier debugging, changing and updating the enum processing in future and most of all the enums now will behave consistently due to removing bunch of duplication and adding correct abstractions instead.
This commit also unifies previously known
SimpleEnum
andReprEnum
to a single enum to furhter simplify the code. AlsoComplexEnum
is now known byMixedEnum
.This commit implements discriminator with support for custom mapping. Discriminator can only be used with
#[serde(untagged)]
enum having only unnamed field variants with one schema reference implementingToSchema
trait. It cannot be used with primitive types nor with inlined schemas.Removed
#[serde(tag = ...)]
as discriminator support.Update docs and add support for missing features for enum variants such as
Title
,Deprecated
,MinProperties
andMaxProperties
.Breaking changes
#[serde(tag = ...)]
will not be used as discriminator.Example of new discriminator syntax. It can be defined with discriminator field or optionally with custom mappping.
Fixes #621 Fixes #324
Closes #617 Closes #346