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

PoC: Include support for schemars #623

Closed
wants to merge 1 commit into from
Closed

PoC: Include support for schemars #623

wants to merge 1 commit into from

Conversation

jonasbb
Copy link
Owner

@jonasbb jonasbb commented Jul 26, 2023

schemars and serde_with support was requested over at the schemars repository: GREsau/schemars#198
There are some limitations in how well this can be done within schemars since its support for serde's with attribute is quite limited.

This is just a minimal example, showing how to implement the traits for wrapper, container, and conversion types. It includes a simple test that also verifies that the schema can be used for validation.

Instead of building a comprehensive set of tests only for schemars, it might be simpler to modify the test functions in utils.rs to also run schema instance validation tests.

If someone wants to invest the time to bring this to completion, I am happy to review the submissions.

@jonasbb jonasbb closed this Jul 26, 2023
swlynch99 added a commit to swlynch99/serde_with that referenced this pull request Dec 6, 2023
This commit adds support for emitting

    #[schemars(with = "::serde_with::Schema<T, ...>")]

field annotations when any of the following are true:
- there is a #[derive(JsonSchema)] attribute, or,
- the user explicitly requests it via #[serde_as(schemars)]

This requires a bunch of extra work to properly scan for a few different
possible paths to JsonSchema (see the added docs for details) and also
to properly handle the case where the derive is within a #[cfg_attr]. eg

    #[cfg_attr(feature = "blah", derive(schemars::JsonSchema))]

While rustc will evaluate the cfgs for attributes declared by derive
macros unfortunately it does not do the same for attribute macros so we
need to do it ourselves.

The code here started from jonasbb#623 as a base and all changes have been made
on top of that.

Co-Authored-By: Jonas Bushart <[email protected]>
jonasbb added a commit to swlynch99/serde_with that referenced this pull request Jan 2, 2024
This commit adds support for emitting

    #[schemars(with = "::serde_with::Schema<T, ...>")]

field annotations when any of the following are true:
- there is a #[derive(JsonSchema)] attribute, or,
- the user explicitly requests it via #[serde_as(schemars)]

This requires a bunch of extra work to properly scan for a few different
possible paths to JsonSchema (see the added docs for details) and also
to properly handle the case where the derive is within a #[cfg_attr]. eg

    #[cfg_attr(feature = "blah", derive(schemars::JsonSchema))]

While rustc will evaluate the cfgs for attributes declared by derive
macros unfortunately it does not do the same for attribute macros so we
need to do it ourselves.

The code here started from jonasbb#623 as a base and all changes have been made
on top of that.

Co-Authored-By: Jonas Bushart <[email protected]>
jonasbb added a commit to swlynch99/serde_with that referenced this pull request Jan 2, 2024
This commit adds support for emitting

    #[schemars(with = "::serde_with::Schema<T, ...>")]

field annotations when any of the following are true:
- there is a #[derive(JsonSchema)] attribute, or,
- the user explicitly requests it via #[serde_as(schemars)]

This requires a bunch of extra work to properly scan for a few different
possible paths to JsonSchema (see the added docs for details) and also
to properly handle the case where the derive is within a #[cfg_attr]. eg

    #[cfg_attr(feature = "blah", derive(schemars::JsonSchema))]

While rustc will evaluate the cfgs for attributes declared by derive
macros unfortunately it does not do the same for attribute macros so we
need to do it ourselves.

The code here started from jonasbb#623 as a base and all changes have been made
on top of that.

Co-Authored-By: Jonas Bushart <[email protected]>
@jonasbb jonasbb deleted the schemars branch February 19, 2024 20:55
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

Successfully merging this pull request may close these issues.

1 participant