[BUG] [Ruby] `valid?` for model that contains oneOf or anyOf raises exception
Created by: autopp
Bug Report Checklist
-
Have you provided a full/minimal spec to reproduce the issue? -
Have you validated the input using an OpenAPI validator (example)? -
What's the version of OpenAPI Generator used? -
Have you search for related issues/PRs? -
What's the actual output vs expected output? -
[Optional] Bounty to sponsor the fix (example)
Description
When schema uses oneOf
or anyOf
, The valid?
method in the corresponding model contains wrong code that leads to runtime error.
openapi-generator version
$ java -jar modules/openapi-generator-cli/target/openapi-generator-cli.jar version
4.1.1-SNAPSHOT
$ git log --oneline -n 1
024f46814c (HEAD -> master, origin/master, origin/HEAD) cpp-qt5-client: fix memory leak in api-body (#3661)
OpenAPI declaration file content or url
See: https://gist.github.com/autopp/0385a0d0341304c1854f32a473f5d4ca
Note that the OneOfProps
uses oneOf
and the AnyOfProps
uses anyOf
.
Command line used for generation
$ java -jar modules/openapi-generator-cli/target/openapi-generator-cli.jar generate --skip-validate-spec -g ruby -o client/one_of_any_of -i 'https://gist.githubusercontent.com/autopp/0385a0d0341304c1854f32a473f5d4ca/raw/faaab37bb357cbd296984cce5bba5e3acdd29854/example.yml'
Steps to reproduce
Run the above command to generate client code for Ruby.
lib/openapi_client/models/one_of_prop.rb
contains wrong definition of OneOfProp#valid?
:
def valid?
_one_of_found = false
openapi_one_of.each do |_class| # <- NameError
_one_of = OpenapiClient.const_get(_class).build_from_hash(self.to_hash)
if _one_of.valid?
if _one_of_found? # <- NoMethodError
return false
else
_one_of_found = true
end
end
end
if !_one_of_found? # <- NoMethodError
return false
end
true
end
And lib/openapi_client/models/any_of_prop.rb
contains wrong definition of AnyOfProp#valid?
also:
def valid?
_any_of_found = false
openapi_any_of.each do |_class| # <- NameError
_any_of = OpenapiClient.const_get(_class).build_from_hash(self.to_hash)
if _any_of.valid?
_any_of_found = true
end
end
if !_any_of_found? # <- NoMethodError
return false
end
true
end
Related issues/PRs
N/A
Suggest a fix
Fix modules/openapi-generator/src/main/resources/ruby-client/partial_model_generic.mustache
. I'll create PR.