question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[BUG] Ruby models order in inheritance case

See original GitHub issue
Description

By generating a ruby client, the models order defined in openapi_client.rb is incorrect in case where there is some inheritance.

openapi-generator version
$ openapi-generator version
4.2.2
OpenAPI declaration file content or url

petstore.yaml

openapi: 3.0.0
info:
  version: 1.0.0-SNAPSHOT
  title: petstore
  description: |
    This is the specification of Petstore with the following resources:

    - Pet
  license:
    name: MIT
servers:
  - url: http://petstore.swagger.io/v1
paths:
  /pets:
    get:
      tags:
        - pets
      responses:
        "200":
          description: A paged array of pets
          content:
            application/json:
              schema:
                type: object
                description: 1.0.0
                required:
                  - pet_type
                properties:
                  pet_type:
                    type: string
                discriminator:
                  propertyName: pet_type
                  mapping:
                    dog: "#/components/schemas/Dog"
                    cat: "#/components/schemas/Cat"
                    lizard: "#/components/schemas/Lizard"
components:
  schemas:
    Pet:
      type: object
      required:
        - pet_type
      properties:
        pet_type:
          type: string
        startDate:
          type: string
          example: "2019-11-08"
        num:
          type: integer
          example: 1
      discriminator:
        propertyName: pet_type
        mapping:
          dog: "#/components/schemas/Dog"
          cat: "#/components/schemas/Cat"
          lizard: "#/components/schemas/Lizard"
    Dog:
      allOf:
        - $ref: "#/components/schemas/Pet"
        - type: object
          properties:
            bark:
              type: string
    Cat:
      allOf:
        - $ref: "#/components/schemas/Pet"
        - type: object
          properties:
            name:
              type: string
    Lizard:
      allOf:
        - $ref: "#/components/schemas/Pet"
        - type: object
          properties:
            lovesRocks:
              type: boolean

Command line used for generation
openapi-generator generate --generator-name ruby -i petstore.yaml
Steps to reproduce
  1. After generating, build gem gem build openapi_client.gemspec
  2. Install it gem install openapi_client-1.0.0.gem
  3. Use it in your ruby code in test.py file for example: `require ‘openapi_client’
  4. Execute it ruby test.py

The following error has occured: uninitialized constant OpenapiClient::Pet (NameError)

The problem is due to openapi_client.rb file where Cat, Dogs and Lizard modules are loaded before Pet.

Suggest a fix

Rather to generate the model list from alphabetic order, generate it in the same order of OpenAPI specs file.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:5
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
EranAvidorcommented, Dec 3, 2019

👍 experiencing the same issue.

The root gem file (openapi_client.rb) has 3 requires blocks, each sorted alphabetically but not by dependency:

# Common files
require 'openapi_client/api_client'
require 'openapi_client/api_error'
require 'openapi_client/version'
require 'openapi_client/configuration'

# Models
require 'openapi_client/models/cat'
require 'openapi_client/models/cat_all_of'
require 'openapi_client/models/dog'
require 'openapi_client/models/dog_all_of'
require 'openapi_client/models/inline_response200'
require 'openapi_client/models/lizard'
require 'openapi_client/models/lizard_all_of'
require 'openapi_client/models/pet'

# APIs
require 'openapi_client/api/pets_api'

The openapi_client/models/cat.rb defines the Cat class which derives from Pet base class:

require 'date'

module OpenapiClient
  class Cat < Pet
  ...
end

Pet was not loaded when Cat is being loaded and resulted in an uninitialized constant error.

A suggested fix can be requiring the base classes on the derived classes.

require 'date'
require 'openapi_client/models/pet'

module OpenapiClient
  class Cat < Pet
  ...
end
0reactions
wing328commented, Mar 27, 2021

I’ve filed https://github.com/OpenAPITools/openapi-generator/pull/9103 to fix it. Please review when you guys have time.

Read more comments on GitHub >

github_iconTop Results From Across the Web

SubclassNotFound: The single-table inheritance mechanism ...
I found adding self.inheritance_column = :_type_disabled to the model with the offending column name fixed the error which I found here. Disable ...
Read more >
Ruby programing tutorial - class inheritance & modules
Inheritance is when a class inherits behavior from another class. ... We've eliminated the speak method from the GoodDog class in order to...
Read more >
Refactoring our Rails app out of single-table inheritance
James Coglan, a Developer at FutureLearn, explains a common problem with STI and how we refactored our Rails app to solve it.
Read more >
Understanding the Ruby Object Model In Depth
In this article, we'll cover the following concepts in detail: Classes and instances; Inheritance; Public, private, and protected methods ...
Read more >
Buggy Code: 10 Common Rails Programming Mistakes - Toptal
As a simple example, consider a case in which we have a current_user method ... Common Rails Programming Mistake #3: Overloading the Model...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found