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 #[openapi(rename = "...")] attribute #367

Merged
merged 3 commits into from
Jan 28, 2022

Conversation

tomsik68
Copy link
Contributor

@tomsik68 tomsik68 commented Dec 8, 2021

Hello!

I designed a fix for #357 which is a little different from the approach in #358. This time, I'm adding the explicit rename using #[openapi(rename = "...")]. If there's a good way to test my approach using existing paperclip tests, I'd love to know about it.

macros/src/actix.rs Show resolved Hide resolved
macros/src/actix.rs Outdated Show resolved Hide resolved
macros/src/actix.rs Outdated Show resolved Hide resolved
@tiagolobocastro
Copy link
Collaborator

tiagolobocastro commented Dec 9, 2021

Hello!

I designed a fix for #357 which is a little different from the approach in #358. This time, I'm adding the explicit rename using #[openapi(rename = "...")]. If there's a good way to test my approach using existing paperclip tests, I'd love to know about it.

So the tests basically take your "config", generate the openapi spec and compare it against a hardcoded spec using check_json. I see you've renamed a Pet - that should fail the tests now are they expect to see Pet and not PetRenamed - however please add a new test instead to avoid changing all the existing tests which expect Pet.

Example:

#[test]
fn test_rename_api() {
    #[derive(Deserialize, Serialize, Apiv2Schema)]
    #[serde(rename_all = "camelCase")]
    /// Pets are awesome!
    struct Pet {
        /// Pick a good one.
        name: String,
    }

    #[get("/pets")]
    #[api_v2_operation]
    fn echo_pets() -> impl Future<Output = Result<web::Json<Vec<Pet>>, Error>> {
        fut_ok(web::Json(vec![]))
    }

    run_and_check_app(
        || {
            App::new()
                .wrap_api()
                .service(echo_pets)
                .with_raw_json_spec(|app, spec| {
                    app.route(
                        "/api/spec",
                        web::get().to(move || actix_web::HttpResponse::Ok().json(&spec)),
                    )
                })
                .build()
        },
        |addr| {
            let resp = CLIENT
                .get(&format!("http://{}/api/spec", addr))
                .send()
                .expect("request failed?");

            check_json(
                resp,
                json!({
                    "definitions": {
                        "Pet": {
                            "description": "Pets are awesome!",
                            "properties": {
                                "name": {
                                    "description": "Pick a good one.",
                                    "type": "string"
                                },
                            },
                            "required":[
                                "name"
                            ],
                            "type":"object"
                        }
                    },
                    "info": {
                        "title":"",
                        "version":""
                    },
                    "paths": {
                        "/pets": {
                            "get": {
                                "responses": {
                                "200": {
                                    "description": "OK",
                                    "schema": {
                                        "items": {
                                            "$ref": "#/definitions/Pet"
                                        },
                                        "type": "array"
                                    }
                                }
                                },
                            }
                        }
                    },
                    "swagger": "2.0"
                }),
            );
        },
    );
}

@tomsik68
Copy link
Contributor Author

Hey @tiagolobocastro, I rebased, reimplemented extract_rename using your suggestions and added a test case for it! Could you please take another look when you have a chance? Thanks!

@tiagolobocastro
Copy link
Collaborator

awesome thanks @tomsik68, I've kicked gh actions and will try to have a look during the week

@tiagolobocastro
Copy link
Collaborator

hey @tomsik68 would you mind fixing the clippy errors here

@tiagolobocastro tiagolobocastro merged commit 54ea0f9 into paperclip-rs:master Jan 28, 2022
@tiagolobocastro
Copy link
Collaborator

Thank you @tomsik68 !

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.

2 participants