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

Add ToResponse derive implementation #416

Merged
merged 4 commits into from
Dec 21, 2022
Merged

Conversation

juhaku
Copy link
Owner

@juhaku juhaku commented Dec 21, 2022

Add new ToResponse derive attribute macro. This can be used alternatively to generate path response. It support all the attributes which are supported in tuple response syntax to further configure the generated response. By default the name of the struct which is decorated with ToResponse macro will be used as a name for the response and as response type as well.

To define simple response component where name of the repsonse and schema must be same one can use named field struct or enum without content attributes.

 #[derive(utoipa::ToSchema, utoipa::ToResponse)]
 struct Person {
     name: String,
 }

To define multiple contents for single response one must use enum style definition.

 #[derive(utoipa::ToResponse)]
 enum Foo {
   Foo(#[content("type")] Foo),
   Bar(#[content("type")] Bar),
 }

To define Vec or Option response the new type pattern must be used.

 #[derive(utoipa::ToResponse)]
 struct MyResponse(Vec<String>);

Resolves #412

Add new `ToResponse` derive attribute macro. This can be used
alternatively to generate path response. It support all the attributes
which are supported in tuple response syntax to further configure the
generated response. By default the name of the `struct` which is
decorated with `ToResponse` macro will be used as a name for the
response and as response type as well.

To define multiple contents for single response one must use enum style
definition.
```rust
 #[derive(utoipa::ToResponse)]
 enum Foo {
   Foo(#[content("type")] Foo),
   Bar(#[content("type")] Bar),
 }
```

To define `Vec` or `Option` response the new type pattern must be used.
```rust
 #[derive(utoipa::ToResponse)]
 struct MyResponse(Vec<String>);
```
@juhaku juhaku force-pushed the feature-derive-response branch from 8fe8f7d to 399c209 Compare December 21, 2022 00:36
Add support for enum with `#[content]` attribute syntax for `ToResponse`
derive attribute macro.
Add `ToResponse` docs and update `OpenApi` and `path` docs.
@juhaku juhaku marked this pull request as ready for review December 21, 2022 15:43
@juhaku juhaku merged commit 3d0ea69 into master Dec 21, 2022
@juhaku juhaku deleted the feature-derive-response branch December 21, 2022 15:58
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.

Derive Responses implementation
1 participant