Skip to content

derive(ToSchema) fails when using field types like <Foo as Bar>::Baz #1558

@LukasKalbertodt

Description

@LukasKalbertodt
#[derive(utoipa::ToSchema)]
struct Foo {
    bar: <Self as Dummy>::Zing,
}

trait Dummy {
    type Zing;
}

impl Dummy for Foo {
    type Zing = String;
}

Results in:

error[E0223]: ambiguous associated type
 --> src/main.rs:4:19
  |
4 |     bar: <Self as Dummy>::Zing,
  |                   ^^^^^^^^^^^^
  |
help: use fully-qualified syntax
  |
4 |     bar: <Self as <Foo as Dummy>::Zing,
  |                   +++++++

Relevant output of cargo expand:

impl utoipa::ToSchema for Foo {
    fn name() -> std::borrow::Cow<'static, str> {
        std::borrow::Cow::Borrowed("Foo")
    }
    fn schemas(
        schemas: &mut Vec<
            (String, utoipa::openapi::RefOr<utoipa::openapi::schema::Schema>),
        >,
    ) {
        schemas
            .extend([
                (
                    String::from(
                        ::alloc::__export::must_use({
                            ::alloc::fmt::format(
                                format_args!(
                                    "{0}",
                                    <Dummy::Zing as utoipa::ToSchema>::name(),
                                ),
                            )
                        }),
                    ),
                    <Dummy::Zing as utoipa::PartialSchema>::schema(),
                ),
            ]);
        <Dummy::Zing as utoipa::ToSchema>::schemas(schemas);
    }
}

Note the multiple occurences of <Dummy::Zing as utoipa::ToSchema>. where the Self as is completely dropped.

The problem is here:

TypeTreeValue::TypePath(type_path) => &type_path.path,

Only the .path is accessed, ignoring the type_path.qself. I tried to fix it myself but it's not trivial as many own types in utoipa-gen (e.g. TypeTree) contain Path so all of those would be need to changed to TypePath to carry along the Self as. I did try that but then run into some other problems where I was lacking knowledge about the code base to proceed.

Would be great if this is fixed :) Thanks!

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