Skip to content
GitLab
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in / Register
  • O openapi-generator
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 3,476
    • Issues 3,476
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 402
    • Merge requests 402
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Packages and registries
    • Packages and registries
    • Package Registry
    • Infrastructure Registry
  • Monitor
    • Monitor
    • Incidents
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • OpenAPI Tools
  • openapi-generator
  • Issues
  • #3670
Closed
Open
Issue created Aug 18, 2019 by Administrator@rootContributor5 of 6 checklist items completed5/6 checklist items

[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.

Assignee
Assign to
Time tracking