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
21.54k stars 6.51k forks source link

[REQ][Ruby] Add Strict Type Validation #11889

Open dbunn-calendly opened 2 years ago

dbunn-calendly commented 2 years ago

Is your feature request related to a problem? Please describe.

Currently the ruby generated library doesn't perform strict type validation on properties.
Given I have this schema:

Widget:
  type: object
  required: [name, owner]
  properties:
    name:
      type: string
    owner:
      type: object
      required: [id]
      properties:
        id:
          type: string

The generated Widget model will accept the following and not cause an error:

widget = Widget.new(name: 123, owner: true)

If I was then to call widget.valid?, it would return true even though the widget object does not adhere to the schema.

This can be problematic when integrating the library into other projects and you want to rely upon it for schema validation during testing. It is common when testing for external HTTP requests/responses to be mocked. This can result in a situation where incorrect data is mocked and passed to the library but tests pass because the schema wasn't strictly validated at any point.

Describe the solution you'd like

Update methods like initialize and valid? with strict type validation. For valid? this could be as simple as this:

return false unless @name.is_a?(String)
return false unless @owner.is_a?(Owner)
return false unless @owner.valid?

This additional validation could be disabled by default and only added when a generator specific config is set. Maybe strictTypeValidation=true? Putting this behind a config will help ensure we don't introduce any backward incompatible behavior.

Describe alternatives you've considered

Including json-schema as a dependence and replacing existing schema validation with calls to this library.

Alternatively this solution or the purposed one could be implement in a new Ruby client generator. Maybe ruby-strict-types?

Additional context

None that I can think of at this time.

wing328 commented 2 years ago

Maybe strictTypeValidation=true? Putting this behind a config will help ensure we don't introduce any backward incompatible behavior.

Sounds good to me. I wonder if you can file a PR for that to start with. Thank you.

dbunn-calendly commented 2 years ago

@wing328, I can work on a PR for this. Thanks