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

Enhance ToResponse implementation #419

Merged
merged 2 commits into from
Dec 23, 2022
Merged

Conversation

juhaku
Copy link
Owner

@juhaku juhaku commented Dec 23, 2022

This commit will add support for unit type responses. Structs without fields will be threated as a responses without body.

 #[derive(ToSchema, ToResponse)]
 struct PersonSuccessResponse;

This commit changes ToResponse behaviour in named field structs and enums without #[content] attribute so that the fields and variants of those types will become inlined schema for the response. Prior to this PR ToSchema ignored fields and variants of those types and created a reference to the type having same name as response. That behaviour was not correct and was illogical since you can use new type pattern to create a reference response.

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

Add new attribute #[to_schema] which can be used to inline schema of unnamed field struct or enum variant which already has #[content] attribute. Without #[to_schema] attribute response will be created with a reference to the field type.

 #[derive(ToResponse)]
 struct PersonSuccessResponse(#[to_schema] Vec<Person>);

Resolves #412

@juhaku juhaku added the enhancement New feature or request label Dec 23, 2022
@juhaku juhaku self-assigned this Dec 23, 2022
This commit will add support for unit type responses. Structs without
fields will be threated as a responses without body.

```rust
 #[derive(ToSchema, ToResponse)]
 struct PersonSuccessResponse;
```

This commit changes `ToResponse` behaviour in named field structs and
enums without `#[content]` attribute so that the fields and variants of
those types will become inlined schema for the response. Prior to this
PR `ToSchema` ignored fields and variants of those types and created a
reference to the type having same name as response. That behaviour was
not correct and was illogical since you can use new type pattern to
create a reference response.

```rust
 #[derive(ToResponse)]
 struct Person {
     name: String,
 }
```

Add new attribute `#[to_schema]` which can be used to inline schema of
unnamed field struct or enum variant which already has `#[content]`
attribute. Without `#[to_schema]` attribute response will be created
with a reference to the field type.

```rust
 #[derive(ToResponse)]
 struct PersonSuccessResponse(#[to_schema] Vec<Person>);
```
@juhaku juhaku force-pushed the enhance-to-response-implementation branch from 8e2faa9 to 34820b8 Compare December 23, 2022 00:11
@juhaku juhaku merged commit 571fc10 into master Dec 23, 2022
@juhaku juhaku deleted the enhance-to-response-implementation branch December 23, 2022 00:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Status: Released
Development

Successfully merging this pull request may close these issues.

Derive Responses implementation
1 participant