poem icon indicating copy to clipboard operation
poem copied to clipboard

Optional path parameters plus validation always fail when the parameter is ommited.

Open stevenj opened this issue 1 year ago • 2 comments

Expected Behavior

Optional path parameters shouldn't be validated if they are not present.

Actual Behavior

Validation fails, because the parameter is missing.

Steps to Reproduce the Problem

I defined a path parameter like so:

       #[oai(validator(
            pattern = "[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}",
            max_length = "36"
        ))]
        action: Path<Option<String>>,

It will only validate if present, so it's not optional.

stevenj avatar Sep 08 '23 13:09 stevenj

The path parameter cannot be optional, but I did a test and couldn't reproduce the problem you encountered.

     struct Api;

    #[OpenApi]
    impl Api {
        #[oai(path = "/", method = "get")]
        async fn test(
            &self,
            #[oai(validator(
                pattern = "[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}",
                max_length = "36"
            ))]
            action: Path<Option<String>>,
        ) {
            assert_eq!(action.0, None);
        }
    }

    let api = OpenApiService::new(Api, "test", "1.0");
    TestClient::new(api)
        .get("/")
        .send()
        .await
        .assert_status_is_ok();

sunli829 avatar Sep 10 '23 05:09 sunli829

You are correct about path parameters not being allowed to be optional in OpenAPI. The difference in our examples is I declared the path as /my/path/:optional_thing. Which isn't clear from my bug report. Apologies.

However given that they are not optional, then I have no issue with the behavior I experienced. The only comment I could make if it's possible, would be to restrict the type of T in Path<T> to something with isn't optional, so that code like I attempted wouldn't even compile, or check it at runtime or when the schema is being built?

stevenj avatar Sep 15 '23 06:09 stevenj