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

Generate ToSchema generic bounds on impl instead of requiring it on definition #1079

Merged
merged 3 commits into from
Oct 3, 2024

Conversation

oxalica
Copy link
Contributor

@oxalica oxalica commented Oct 3, 2024

Fixes #1078

#[derive(ToSchema)]
struct Signed<T> { .. }
/// generates
impl<T> ToSchema for Signed<T> where T: ToSchema /* <- introduced by this PR */ { .. }
  • Each generic types now automatically get a ToSchema bound in where clause of generated impl blocks. Thus the user does not need to explicitly add ToSchema bounds on struct definitions.

  • Attribute schema(bound = ..) is introduced for struct or enum containers to opt-out the automatic ToSchema bounds addition, by manually provide where clause bounds for the generated impl blocks. The attribute is inspired by serde(bound = ..).
    Eg. schema(bound = "") can be used to remove bounds in case of PhantomData types.

This PR is based on 61c98bb instead of master because master is already failing the test after the rc version bump (snapshot assertion failure in openapi_schemas_resolve_schema_references and friends). It does not conflict anyway.

- Each generic types now automatically get a `ToSchema` bound in where
  clause of generated `impl` blocks. Thus the user does not need to
  explicitly add `ToSchema` bounds on struct definitions.

- Attribute `schema(bound = ..)` is introduced for struct or enum
  containers to opt-out the automatic `ToSchema` bounds addition, by
  manually provide where clause bounds for the generated impl blocks.
  The attribute is inspired by `serde(bound = ..)`.

  Eg. `schema(bound = "")` can be used to remove bounds in case of
  `PhantomData` types.
@juhaku juhaku force-pushed the feat/derive-to-schema-bound branch 2 times, most recently from 09e5d11 to 71ae28b Compare October 3, 2024 11:19
@oxalica
Copy link
Contributor Author

oxalica commented Oct 3, 2024

@juhaku Seems you are introducing some merge conflicts 🤔

Copy link
Owner

@juhaku juhaku left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks pretty good: 🎉 Thanks for the patch.

I updated it a bit, mainly I fixed the tests and rebased it above the master.

However could you update the docs for the new functionality in /utoipa-gen/src/lib.rs for what is changed in ToSchema derive macro. Also add entry to the utoipa-gen/CHANGELOG.md to top most of suitable category of unreleased changes. Maybe under Added title.

@juhaku juhaku force-pushed the feat/derive-to-schema-bound branch from 71ae28b to ca30255 Compare October 3, 2024 11:23
@juhaku
Copy link
Owner

juhaku commented Oct 3, 2024

@juhaku Seems you are introducing some merge conflicts 🤔

Yeah, that was unfortunate, but fixed them.

@oxalica oxalica force-pushed the feat/derive-to-schema-bound branch from 706b694 to 020769e Compare October 3, 2024 12:13
@oxalica
Copy link
Contributor Author

oxalica commented Oct 3, 2024

Updated docs and added CHANGELOG. Seems the doctest for utoipa-gen (cargo test -p utoipa-gen --doc) is failing on master. Just manually checked all failures are unrelated.

@oxalica oxalica requested a review from juhaku October 3, 2024 12:15
Copy link
Owner

@juhaku juhaku left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great, 🙇 🥇

@juhaku
Copy link
Owner

juhaku commented Oct 3, 2024

Updated docs and added CHANGELOG. Seems the doctest for utoipa-gen (cargo test -p utoipa-gen --doc) is failing on master. Just manually checked all failures are unrelated.

Right, yeah there might still be some of those, I'll fix them later on.

@juhaku juhaku merged commit e5f5d71 into juhaku:master Oct 3, 2024
11 checks passed
@oxalica oxalica deleted the feat/derive-to-schema-bound branch October 3, 2024 13:42
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.

[5.0.0-rc] [generics]: add T: ToSchema bounds on impl instead of requiring it on definition?
2 participants