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 for OpenApiResponderInner #157

Open
wants to merge 7 commits into
base: master
Choose a base branch
from

Conversation

vklachkov
Copy link

Description

This PR adds derive macro for the OpenApiResponderInner trait. It's fully compatible with Rocket's Response derive macro.

Also, to reduce code generation, add set_description function to utils.

This pull request does not add any usage examples.

Codegen Examples

  1. Simple enum:

    #[derive(Responder, OpenApiResponder)]
    enum ServerResult {
        A(String),
        B(Json<u32>),
    }
    Macro expansion
    impl ::rocket_okapi::response::OpenApiResponderInner for ServerResult {
        fn responses(
            gen: &mut ::rocket_okapi::gen::OpenApiGenerator,
        ) -> ::rocket_okapi::Result<::rocket_okapi::okapi::openapi3::Responses> {
            let mut responses = ::rocket_okapi::okapi::openapi3::Responses::default();
            responses.responses.extend(
                {
                    let mut r =
                        <String as ::rocket_okapi::response::OpenApiResponderInner>::responses(gen)?;
                    r
                }
                .responses,
            );
            responses.responses.extend(
                {
                    let mut r =
                        <Json<u32> as ::rocket_okapi::response::OpenApiResponderInner>::responses(gen)?;
                    r
                }
                .responses,
            );
            Ok(responses)
        }
    }
  2. Generic struct:

    #[derive(Responder, OpenApiResponder)]
    #[response(status = 404, content_type = "html")]
    struct NotFoundHtml<T>(T);
    Macro expansion
    impl<T: ::rocket_okapi::response::OpenApiResponderInner> ::rocket_okapi::response::OpenApiResponderInner for NotFoundHtml<T> {
        fn responses(
            gen: &mut ::rocket_okapi::gen::OpenApiGenerator,
        ) -> ::rocket_okapi::Result<::rocket_okapi::okapi::openapi3::Responses> {
            let mut responses = ::rocket_okapi::okapi::openapi3::Responses::default();
            responses.responses.extend(
                {
                    let mut r = <T as ::rocket_okapi::response::OpenApiResponderInner>::responses(gen)?;
                    ::rocket_okapi::util::set_status_code(&mut r, 404u16)?;
                    ::rocket_okapi::util::set_content_type(&mut r, "text/html; charset=utf-8")?;
                    r
                }
                .responses,
            );
            Ok(responses)
        }
    }

Issues

Fixes #146.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Derive OpenApiResponderInner for #[derive(Responder)] enums
1 participant