OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
20.51k stars 6.27k forks source link

[BUG] [Ruby] minLength validation fails on `null` nullable strings #18954

Open phoenixy1 opened 1 week ago

phoenixy1 commented 1 week ago
Description

Ruby generator applies minLength validation to nullable string fields that are null. Per everything I can find, string validation is not supposed to be applied to null values if the field is nullable, because nullable indicates that the value can be null OR a string. However, the generator is running .to_s on whatever is passed in, even if it is nil, and then validating minLength based on that, which is causing all fields with a specified minLength to fail validation if nil is passed instead of a string. I haven't checked whether this applies to other forms of string properties, like pattern and format, but it seems like there is a risk that it does.

You can see an example here where document_number is marked as nullable: https://github.com/plaid/plaid-ruby/blob/master/lib/plaid/models/watchlist_screening_search_terms.rb#L67

But is still being subjected to a length check even if it's null: https://github.com/plaid/plaid-ruby/blob/master/lib/plaid/models/watchlist_screening_search_terms.rb#L132

openapi-generator version

Using 6.3.0. I did not test on newer libraries, but I did not see a bug fix or existing bug for this submitted anywhere.

OpenAPI declaration file content or url
    WatchlistScreeningDocumentValueNullable:
      type: string
      title: WatchlistScreeningDocumentValue
      minLength: 4
      example: C31195855
      description: The numeric or alphanumeric identifier associated with this document.
      nullable: true
Generation Details
Steps to reproduce
Related issues/PRs

https://github.com/OpenAPITools/openapi-generator/issues/9141 https://github.com/OpenAPITools/openapi-generator/issues/16629

Suggest a fix

Before applying any string-based validation rules, check if the value is nil and if so, automatically pass the validation if the field has nullable:true.

wing328 commented 1 week ago

Before applying any string-based validation rules, check if the value is nil and if so, automatically pass the validation if the field has nullable:true.

may I know if you've time to contribute a fix?

ruby model templates can be found in https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/resources/ruby-client/partial_model_generic.mustache