[BUG] [rust-axum] In PUT requests with arrays, a body type string fails to fulfil Validate trait in generated server code
Bug Report Checklist
- [x] Have you provided a full/minimal spec to reproduce the issue?
- [x] Have you validated the input using an OpenAPI validator (example)?
- [x] Have you tested with the latest master to confirm the issue still exists?
- [x] Have you searched for related issues/PRs?
- [x] What's the actual output vs expected output?
- [ ] [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description
With this spec The generation of the client with openapi-generator-cli (OPENAPI_GENERATOR_VERSION=7.12.0) succeeds, but compilation with cargo build (rustc 1.85, 2024 edition) fails due to:
error[E0599]: the method `validate` exists for reference `&&Vec<String>`, but its trait bounds were not satisfied
--> oapicode\src\server\mod.rs:1508:14
|
1508 | #[derive(validator::Validate)]
| ^^^^^^^^^^^^^^^^^^^
|
::: C:\Users\Benedikt\.rustup\toolchains\stable-x86_64-pc-windows-gnu\lib/rustlib/src/rust\library\alloc\src\vec\mod.rs:397:1
|
397 | pub struct Vec<T, #[unstable(feature = "allocator_api", issue = "32838")] A: Allocator = Global> {
| ------------------------------------------------------------------------------------------------ doesn't satisfy `Vec<std::string::String>: Validate`
|
::: C:\Users\Benedikt\.rustup\toolchains\stable-x86_64-pc-windows-gnu\lib/rustlib/src/rust\library\alloc\src\string.rs:362:1
|
362 | pub struct String {
| ----------------- doesn't satisfy `std::string::String: Validate`
|
= note: the following trait bounds were not satisfied:
`Vec<std::string::String>: Validate`
which is required by `&Vec<std::string::String>: Validate`
`&Vec<std::string::String>: Validate`
which is required by `&&Vec<std::string::String>: Validate`
`std::string::String: Validate`
which is required by `Vec<std::string::String>: Validate`
`std::string::String: Validate`
which is required by `[std::string::String]: Validate`
= note: this error originates in the derive macro `validator::Validate` (in Nightly builds, run with -Z macro-backtrace for more info)
For more information about this error, try `rustc --explain E0599`.
error: could not compile `openapi` (lib) due to 1 previous error
openapi-generator version
7.12.0
OpenAPI declaration file content or url
With Error
https://raw.githubusercontent.com/Chrystalkey/landtagszusammenfasser/refs/tags/rust-axum-plain-string/docs/specs/openapi.yml
Without Error
https://github.com/Chrystalkey/landtagszusammenfasser/blob/rust-axum-body-wrapped/docs/specs/openapi.yml
Generation Details
with OPENAPI_GENERATOR_VERSION=7.12.0 set:
java -jar "./oapi-generator/openapi-generator-cli.jar" generate -g rust-axum -i "$(Get-Location)/openapi.yml" -o "$(Get-Location)/oapicode"
(on linux replace $(Get-Location) by $(pwd))
Steps to reproduce
with OPENAPI_GENERATOR_VERSION=7.12.0 set:
& Invoke-WebRequest -OutFile openapi-generator-cli.jar https://repo1.maven.org/maven2/org/openapitools/openapi-generator-cli/7.12.0/openapi-generator-cli-7.12.0.jar
& Invoke-WebRequest -OutFile openapi.yml https://raw.githubusercontent.com/Chrystalkey/landtagszusammenfasser/refs/tags/rust-axum-plain-string/docs/specs/openapi.yml
java -jar "./oapi-generator/openapi-generator-cli.jar" generate -g rust-axum -i "$(Get-Location)/openapi.yml" -o "$(Get-Location)/oapicode"
cd oapicode
& cargo build
(on linux replace $(Get-Location) by $(pwd))
Related issues/PRs
This is a bug that happens after the generic arguments in oapicode/src/server/mod are grep-replaced: "<I, A, E>" -> "<I, A, E, C>" There is a seperate issue for that, #21144
Suggest a fix
This is worked around (not fixed!) when wrapping the body of operationId: enum_put into an object:
type: object
properties:
entry:
type: string
as was done in this version
However I still think this is an issue and should be possible just using plain strings.
Hi!
I get the same issue with version 7.13.0
When you declare an array of integer like this (for a POST request) :
requestBody:
required: true
content:
application/json:
schema:
type: array
items:
type: integer
the generated code looks like :
#[derive(validator::Validate)]
#[allow(dead_code)]
struct Dummy<'a> {
#[validate(nested)]
body: &'a Vec<i32>,
}
But the Validate trait is not implemented on i32 (nor on String, as show above)
Don't know why validator is even involved here, I'll keep looking.
Thank you for reporting this issue. I will have a look on this matter.
(I did not received any notification so far, sorry for my late response)
Hi @Chrystalkey @gcavelier
The fix is on going: https://github.com/OpenAPITools/openapi-generator/pull/22155
Please feel free to review my PR. Thank you
looks good to me too, thanks for fixing this.
It fixes the issue on my side too, thank you @linxGnu !