Skip to content

Ref variant naming #497

@StefanTerdell

Description

@StefanTerdell

Hello, and thanks for building and maintaining schemars!

I've been toying around with _unstable_ref_variants to get around the nested additional properties issue, but I've ran into another one: colliding ref variant names. This is currently resolved by schemars adding a number to the name, but I would prefer to have a way to provide a better name. Normally I'd just use #[schemars(rename = "...")] for this, but that changes the constant value used for discrimination as well.

Some possible solutions:

  1. Adding a field attribute for enum members (perhaps tag, name or even ref_variant, neither of which would collide with serde)
  2. Deriving the name from an existing field attribute like title (not my favorite, but would work)
  3. Deriving more specific names for the member schemas by default (perhaps using the parent enum name and tag as a suffix, so something like "MyEnumMyTagA", "MyOtherEnumB" etc. based on the example below)
  4. There's already a way to do this and I just haven't found it 😅

Here's a quick snippet for reproducing the issue:

fn main() {
    #[derive(schemars::JsonSchema)]
    #[schemars(_unstable_ref_variants, tag = "MyTag")]
    enum MyEnum {
        A { number: f32 },
        B { other: MyOtherEnum },
    }

    #[derive(schemars::JsonSchema)]
    #[schemars(_unstable_ref_variants)]
    enum MyOtherEnum {
        A { string: String },
        B { bool: bool },
    }

    println!("{:#?}", schemars::schema_for!(MyEnum));
}

I would be happy to try and work on a PR if you agree with the problem.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions