Skip to content

[BUG] [Ruby] Initalize validation should apply to 'build_from_hash' too #5199

@NeilW

Description

@NeilW
Description

The changes to introduce better validation to the attributes hash in the initialize method, should also apply in the build_from_hash method.

openapi-generator version

$ docker run --rm openapitools/openapi-generator-cli version
4.2.3-SNAPSHOT

Steps to reproduce
  1. Generate any ruby API
  2. Build a hash with an incorrectly spelled attribute and create a model object with it.

E.g.

irb(main):012:0> Kubernetes::IoK8sApiBatchV1Job.new(api_version: 'batch/v1', kid: 'Job')
...
ArgumentError (`kid` is not a valid attribute in `Kubernetes::IoK8sApiBatchV1Job`. Please check the name to make sure it's valid. List of attributes: [:api_version, :kind, :metadata, :spec, :status])

vs

irb(main):013:0> Kubernetes::IoK8sApiBatchV1Job.build_from_hash(api_version: 'batch/v1', kid: 'Job')
=> #<Kubernetes::IoK8sApiBatchV1Job:0x000055ae59e2d238>
Related issues/PRs

#2226

Suggest a fix

Factor out the validation code from initialize and either call it directly from the 'build_from_hash' method, and/or add a 'strictValidation' parameter to allow backward compatibility.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions