openapi-generator icon indicating copy to clipboard operation
openapi-generator copied to clipboard

[BUG] [rust-axum] In PUT requests with arrays, a body type string fails to fulfil Validate trait in generated server code

Open Chrystalkey opened this issue 10 months ago • 1 comments

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.

Chrystalkey avatar Apr 25 '25 07:04 Chrystalkey

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.

gcavelier avatar May 28 '25 12:05 gcavelier

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)

linxGnu avatar Oct 07 '25 07:10 linxGnu

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

linxGnu avatar Oct 16 '25 04:10 linxGnu

I tried this spec.

It looks good to me with the patch.

linxGnu avatar Oct 16 '25 04:10 linxGnu

looks good to me too, thanks for fixing this.

Chrystalkey avatar Oct 16 '25 10:10 Chrystalkey

It fixes the issue on my side too, thank you @linxGnu !

gcavelier avatar Oct 17 '25 15:10 gcavelier