[BUG] Ruby models order in inheritance case
See original GitHub issueDescription
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
- After generating, build gem
gem build openapi_client.gemspec
- Install it
gem install openapi_client-1.0.0.gem
- Use it in your ruby code in
test.py
file for example: `require ‘openapi_client’ - 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:
- Created 4 years ago
- Reactions:5
- Comments:5 (1 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
👍 experiencing the same issue.
The root gem file (
openapi_client.rb
) has 3 requires blocks, each sorted alphabetically but not by dependency:The
openapi_client/models/cat.rb
defines theCat
class which derives fromPet
base class:Pet
was not loaded whenCat
is being loaded and resulted in an uninitialized constant error.A suggested fix can be requiring the base classes on the derived classes.
I’ve filed https://github.com/OpenAPITools/openapi-generator/pull/9103 to fix it. Please review when you guys have time.