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

Derive OpenApiResponderInner for #[derive(Responder)] enums #146

Open
Keshi opened this issue Mar 29, 2024 · 1 comment
Open

Derive OpenApiResponderInner for #[derive(Responder)] enums #146

Keshi opened this issue Mar 29, 2024 · 1 comment
Labels
enhancement New feature or request rocket-okapi-codegen This affects the rocket-okapi-codegen crate

Comments

@Keshi
Copy link

Keshi commented Mar 29, 2024

Using this feature rwf2/Rocket#590 results in the trait bound '[...] rocket_okapi::response::OpenApiResponderInner' is not satisfied when the enum type is as a result type in a #[openapi] endpoint function.

It would be great if #[openapi] or similar could be used on an enum deriving Responder to also derive the appropriate forwarding OpenApiResponderInner implementation for the enum.

Example:

#[openapi] // inserting okapi openapi or derive macro here?
#[derive(Responder)]
enum MyResponse {
    Person(Json<Person>), // 200, json object
    Nothing(NoContent), // 204, no content
}

Should generate:

impl rocket_okapi::response::OpenApiResponderInner for MyResponse {
    fn responses(
        gen: &mut rocket_okapi::gen::OpenApiGenerator,
    ) -> rocket_okapi::Result<okapi::openapi3::Responses> {
        let mut responses = okapi::openapi3::Responses::default();
        responses
            .responses
            .extend(Json::<Person>::responses(gen)?.responses);
        responses
            .responses
            .extend(NoContent::responses(gen)?.responses);
        Ok(responses)
    }
}
@ralpha ralpha added enhancement New feature or request rocket-okapi-codegen This affects the rocket-okapi-codegen crate labels Apr 28, 2024
@ralpha
Copy link
Collaborator

ralpha commented Apr 28, 2024

I would not use the #[openapi] syntax but instead use the #[derive(OpenApiResponderInner)].

This could then be done similar to
https://github.com/GREsau/okapi/blob/1608ff7b92e3daca8cf05aa4594e1cf163e584a9/rocket-okapi-codegen/src/lib.rs#L119:L137

Some of the code could even be reused.

Feel free to create a MR to help with adding this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request rocket-okapi-codegen This affects the rocket-okapi-codegen crate
Projects
None yet
Development

No branches or pull requests

2 participants