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

[BUG] Array keyword minItems not in serde validator #17136

Closed
vances opened this issue Nov 19, 2023 · 6 comments · Fixed by #17201
Closed

[BUG] Array keyword minItems not in serde validator #17136

vances opened this issue Nov 19, 2023 · 6 comments · Fixed by #17201

Comments

@vances
Copy link
Contributor

vances commented Nov 19, 2023

Description

Array keyword minItems results with empty validation in Rust model.

openapi-generator version

7.1.0

OpenAPI declaration file content or url
---
swagger: '2.0'
info:
  title: Bug in Array Validation
  version: 0.1.0
paths:
  "/foo":
    get:
      responses:
        '200':
          description: Success
          schema:
            "$ref": "#/definitions/Foo"
definitions:
  Foo:
    type: object
    properties:
      bar:
        type: array
        items:
          "$ref": "#/definitions/Bar"
        minItems: 1
  Bar:
    type: string
Generation Details
$ uname -a
Darwin box.local 22.5.0 Darwin Kernel Version 22.5.0: Thu Jun  8 22:22:19 PDT 2023; root:xnu-8796.121.3~7/RELEASE_ARM64_T8103 arm64
$ cargo version
cargo 1.71.0 (cfd3bbd8f 2023-06-08)
Steps to reproduce
$ openapi-generator generate --generator-name rust-server -i bug.yaml
$ cargo build
error: Invalid attribute #[validate] on field `bar`: it needs at least one validator
  --> src/models.rs:56:5
   |
56 |     #[validate(
   |     ^

error: could not compile `openapi_client` (lib) due to previous error

The generated src/models.rs:

#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize, validator::Validate)]
#[cfg_attr(feature = "conversion", derive(frunk::LabelledGeneric))]
pub struct Foo {
    #[serde(rename = "bar")]
    #[validate(
        )]
    #[serde(skip_serializing_if="Option::is_none")]
    pub bar: Option<Vec<models::Bar>>,

}
Related issues/PRs
Suggest a fix

The validation in the generated model should be:

    #[validate(
        length(min = 1)
       )]
@wing328
Copy link
Member

wing328 commented Nov 19, 2023

@vances thanks for reporting the issue. Would you have time to contribute a PR with a fix?

@vances
Copy link
Contributor Author

vances commented Nov 20, 2023

@wing328 I don't do Java myself, so I probably can't contribute a PR.

@wing328
Copy link
Member

wing328 commented Nov 20, 2023

I could be wrong but looks like the bug can be fixed by simply updating the mustache templates in https://github.com/OpenAPITools/openapi-generator/tree/master/modules/openapi-generator/src/main/resources/rust-server

and here is how another template (python-flask) is using minItems:

@vances
Copy link
Contributor Author

vances commented Nov 20, 2023

@wing328 Heh, I had just reached that conclusion by fumbling my way around. :)

I'll try to make it work. Thanks.

@wing328
Copy link
Member

wing328 commented Nov 20, 2023

👍

let me know if you need any help (PM me via Slack)

@vances
Copy link
Contributor Author

vances commented Nov 27, 2023

Completed in PR #17201

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants