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

Allow #[schemars(with = "Type")] on struct #267

Open
Sytten opened this issue Feb 2, 2024 · 1 comment
Open

Allow #[schemars(with = "Type")] on struct #267

Sytten opened this issue Feb 2, 2024 · 1 comment

Comments

@Sytten
Copy link

Sytten commented Feb 2, 2024

Hi!
I am using custom serde deserializers on some of my structs to parse data. For example the string $hello.world is deserialized into:

#[derive(Clone, Debug, JsonSchema)]
pub struct Reference {
    pub left_alias: Alias,
    pub right_alias: Alias,
}

Right now we don't have a good way to tell schemars that Reference is a String without implementing JsonSchema ourselves. I would like to propose the syntax:

#[derive(Clone, Debug, JsonSchema)]
#[schemars(with = "String")]
pub struct Reference {
    ...
}

This is similar to the syntax used on fields, but allows the consumer to avoid adding it everywhere.

@Sytten
Copy link
Author

Sytten commented Feb 2, 2024

For those interested this is the current way to do it AFAIK:

impl JsonSchema for Reference {
    fn schema_name() -> String {
        "Reference".to_owned()
    }

    fn schema_id() -> Cow<'static, str> {
        Cow::Borrowed(concat!(module_path!(), "::Reference"))
    }

    fn json_schema(gen: &mut schemars::gen::SchemaGenerator) -> Schema {
        gen.subschema_for::<String>()
    }
}

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

1 participant