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

Cannot avoid bound on unused generic type parameter #373

Open
jonhoo opened this issue Feb 10, 2025 · 0 comments
Open

Cannot avoid bound on unused generic type parameter #373

jonhoo opened this issue Feb 10, 2025 · 0 comments

Comments

@jonhoo
Copy link

jonhoo commented Feb 10, 2025

I have a type with a generic type parameter that I don't want to be reflected in the schema at all:

use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use std::marker::PhantomData;

#[derive(Serialize, Deserialize, JsonSchema)]
#[serde(bound = "")]
#[schemars(bound = "")]
struct Foo<In> {
    x: usize,
    #[serde(skip)]
    ph: PhantomData<In>,
}

However, this won't compile:

error[E0599]: no function or associated item named `schema_name` found for type parameter `In` in the current scope
 --> src/lib.rs:5:34
  |
5 | #[derive(Serialize, Deserialize, JsonSchema)]
  |                                  ^^^^^^^^^^ function or associated item cannot be called on `In` due to unsatisfied trait bounds
...
8 | struct Foo<In> {
  |            -- function or associated item `schema_name` not found for this type parameter
  |
  = help: items from traits can only be used if the type parameter is bounded by the trait
  = note: this error originates in the derive macro `JsonSchema` (in Nightly builds, run with -Z macro-backtrace for more info)
help: the following trait defines an item `schema_name`, perhaps you need to restrict type parameter `In` with it:
  |
8 | struct Foo<In: JsonSchema> {
  |              ++++++++++++

error[E0599]: no function or associated item named `schema_id` found for type parameter `In` in the current scope
 --> src/lib.rs:5:34
  |
5 | #[derive(Serialize, Deserialize, JsonSchema)]
  |                                  ^^^^^^^^^^ function or associated item cannot be called on `In` due to unsatisfied trait bounds
...
8 | struct Foo<In> {
  |            -- function or associated item `schema_id` not found for this type parameter
  |
  = help: items from traits can only be used if the type parameter is bounded by the trait
  = note: this error originates in the derive macro `JsonSchema` (in Nightly builds, run with -Z macro-backtrace for more info)
help: the following trait defines an item `schema_id`, perhaps you need to restrict type parameter `In` with it:
  |
8 | struct Foo<In: JsonSchema> {
  |              ++++++++++++

The issue is that the generated code still tries to use In as though it implemented JsonSchema:

impl<In> schemars::JsonSchema for Foo<In> {
    fn schema_name() -> std::string::String {
        ::alloc::__export::must_use({
            let res = ::alloc::fmt::format(
                format_args!("Foo_for_{0}", In::schema_name()),
            );
            res
        })
    }
    fn schema_id() -> std::borrow::Cow<'static, str> {
        std::borrow::Cow::Owned(
            ::alloc::__export::must_use({
                let res = ::alloc::fmt::format(
                    format_args!("schemars_bounds::Foo_for_{0}", In::schema_id()),
                );
                res
            }),
        )
    }
    // ...
}

I can work around this by adding

#[serde(rename = "Foo")]

though not clear to me if that's an intended/expected behaviour?

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