poem
poem copied to clipboard
Optional path parameters plus validation always fail when the parameter is ommited.
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.
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();
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?