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

Refactor to schema casting as #459

Merged
merged 6 commits into from
Jan 26, 2023
Merged

Refactor to schema casting as #459

merged 6 commits into from
Jan 26, 2023

Conversation

juhaku
Copy link
Owner

@juhaku juhaku commented Jan 26, 2023

This commit changes the ToSchema trait to be similar with ToResponse trait. It also introduces lifetimes to avoid unnecessary allocations.

pub trait ToSchema<'__s> {
    fn schema() -> (&'__s str, openapi::RefOr<openapi::schema::Schema>);

    fn aliases() -> Vec<(&'__s str, openapi::schema::Schema)> {
        Vec::new()
    }
}

Prior to this commit the name that was used to register a schema to OpenAPI spec was defined in #[openapi(components(schemas(...)))] attribute with syntax like schemas(path::to::Schema as path::MySchema). This is a bit inconvenient at least and behaved completely opposite to how response and request bodies behave when defined in #[utoipa::path] macro.

From now the name of the schema registered in OpenAPI spec will be defined by the ToSchema trait itself the same way as ToResponse does it. It will return a tuple of (name, component) pair.

This also introduces new feature As what is then used to define alternative name for the schema derived from ToSchema if necessary. For example the below struct would be registered to the OpenAPI with name path.to.Foo instead of just Foo when it is added to openapi with #[openapi(components(schemas(Foo)))].

 #[derive(ToSchema)]
 #[schema(as = path::to::Foo)]
 pub struct Foo {
     name: String,
 }

This commit changes the `ToSchema` trait to be similar with `ToResponse`
trait. It also introduces lifetimes to avoid unnecessary allocations.
```rust
pub trait ToSchema<'__s> {
    fn schema() -> (&'__s str, openapi::RefOr<openapi::schema::Schema>);

    fn aliases() -> Vec<(&'__s str, openapi::schema::Schema)> {
        Vec::new()
    }
}
```

Prior to this commit the name that was used to register a schema to
OpenAPI spec was defined in `#[openapi(components(schemas(...)))]`
attribute with syntax like `schemas(path::to::Schema as
path::MySchema)`. This is a bit inconvenient at least and behaved
completely opposite to how response and request bodies behave when
defined in `#[utoipa::path]` macro.

From now the name of the schema registered in OpenAPI spec will be
defined by the `ToSchema` trait itself the same way as `ToResponse` does
it. It will return a tuple of `(name, component)` pair.

This also introduces new feature `As` what is then used to define
alternative name for the schema derived from `ToSchema` if necessary.
For example the below struct would be registered to the OpenAPI with
name `path.to.Foo` instead of just `Foo` when it is added to openapi
with `#[openapi(components(schemas(Foo)))]`.
```rust
 #[derive(ToSchema)]
 #[schema(as = path::to::Foo)]
 pub struct Foo {
     name: String,
 }
```
@juhaku juhaku merged commit 11288c3 into master Jan 26, 2023
@juhaku juhaku deleted the refactor-to-schema-casting-as branch January 26, 2023 18:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Released
Development

Successfully merging this pull request may close these issues.

1 participant