schemars icon indicating copy to clipboard operation
schemars copied to clipboard

Add basic support for enum variant aliases

Open suluke opened this issue 3 years ago • 2 comments

Hi! This is a fairly basic implementation to have #[serde(alias = "foo")] result in additional enum variants in the generated schema. I would be happy to make this configurable via GeneratorSettings, extend it to the other possible enum representations and add more tests. I just wanted to check first with you if there is some fundamental flaw with this feature or if my implementation is totally backwards.

Thank you for this great project!

Unrelated P.S.: Would you be interested in the following 2 post-processing visitors I implemented to make my schema work for my use case?

/// Removes any generated "format" keys in the generated schema
///
/// Schemars will add "format" entries like e.g. "uint64".
/// This isn't even known in the OpenAPI 3 spec and much less for vanilla json-schema.
struct RemoveTypeFormats;

/// Removes `"additionalProperties": false` from any structs which have an "oneOf"
///
/// At least [jsonschemalint.com](https://jsonschemalint.com/) does not allow properties not listed in `properties` to be "consumed" by `one_of` instead.
struct RemoveAdditionalPropsFalseWhereEnumFlattened;

suluke avatar Apr 15 '22 19:04 suluke

I should have searched a little better it seems. This obviously addresses #25. As mentioned there, my change should probably include some configurability (SchemaSettings.variant_aliases). But I already offered to add this once this initial commit has received some general feedback :)

suluke avatar Apr 16 '22 16:04 suluke