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

Support enum variant aliases #25

Open
GREsau opened this issue Apr 7, 2020 · 4 comments
Open

Support enum variant aliases #25

GREsau opened this issue Apr 7, 2020 · 4 comments

Comments

@GREsau
Copy link
Owner

GREsau commented Apr 7, 2020

Received via email

Is there a way to get schemars to add serde aliased enum values to the generated schema? E.g. each aliased value shows up in the enum list like a regular enum value.

https://serde.rs/variant-attrs.html#alias

@GREsau
Copy link
Owner Author

GREsau commented Apr 7, 2020

Since aliases are only signifcant when deserializing, this is a case where the schema would be different depending on whether it represents the contract for serialization or deserialization, so I'm not sure what the best way to handle this is.

e.g. consider this enum with an aliased variant:

#[derive(Serialize, Deserialize, JsonSchema)]
enum MyEnum {
    #[serde(alias = "MyAlias")]
    MyVariant1,
    MyVariant2,
}

What should the schema for MyEnum be?

  1. The schema describing what can be deserialized, which has three possible values: {"enum": ["MyVariant1","MyAlias","MyVariant2"]}
  2. The schema describing how it can be serialized, which only has two possible values: {"enum": ["MyVariant1","MyVariant2"]}

This could also be configurable, either by an attribute e.g.:

#[derive(Serialize, Deserialize, JsonSchema)]
#[schemars(include_aliases)]
enum MyEnum {
    #[serde(alias = "MyAlias")]
    MyVariant1,
    MyVariant2,
}

...or by adding a new schema setting e.g.:

let settings = SchemaSettings::draft07().with(|s| {
  s.variant_aliases = Aliases::Include // or Aliases::Ignore
});
let schema = settings.into_generator().into_root_schema_for::<MyEnum>();

@ModProg
Copy link

ModProg commented Jan 18, 2023

@GREsau what would you prefer concerning the deserialize vs. serialize problematic? I have only the deserialize case and would benefit greatly by having this implemented and would therefor be willing to tackle this (in #136) there is already an initial implementation of just the aliasing.

@GREsau
Copy link
Owner Author

GREsau commented Aug 18, 2024

Closing in favour of #48

@GREsau GREsau closed this as not planned Won't fix, can't repro, duplicate, stale Aug 18, 2024
@GREsau GREsau reopened this Sep 8, 2024
@GREsau
Copy link
Owner Author

GREsau commented Sep 8, 2024

Reopening, because the general mechanism for separate serialize/deserialize schemas is done in alpha.15, but alias attributes are not yet respected

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

No branches or pull requests

2 participants