OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
21.23k stars 6.43k forks source link

[BUG] Cannot find class with ruby generator #12854

Open bark-mshort opened 2 years ago

bark-mshort commented 2 years ago

Bug Report Checklist

Description

After importing the API spec for commercetools, the newly created ruby client is unable to load the class Message. An issue was initially opened with commercetools regarding the spec, but after being able to properly generate the client, we are still unable to properly initialize the client via both our rails application and the regular irb console.

openapi-generator version
openapi-generator --version
openapi-generator-cli 6.0.1
  commit : 9e412ed
  built  : 2022-07-03T16:21:27Z
  source : https://github.com/openapitools/openapi-generator
  docs   : https://openapi-generator.tech/
OpenAPI declaration file content or url

https://raw.githubusercontent.com/commercetools/commercetools-api-reference/main/oas/openapi.yaml

Generation Details

Install Generator

brew install openapi-generator

Create Ruby Client

openapi-generator generate -i \
https://raw.githubusercontent.com/commercetools/commercetools-api-reference/main/oas/openapi.yaml \
--additional-properties=gemName=hackathon_ct_sdk,moduleName=HackathonCtSdk -g ruby
Steps to reproduce

As defined by setup instructions in the automatically generated client, build the gem and install locally:

gem build hackathon_ct_sdk.gemspec
gem install ./hackathon_ct_sdk-1.0.0.gem

Load irb console and require gem:

hackathon-ct-sdk ➜ irb                                     
irb(main):001:0> require 'hackathon_ct_sdk'
Traceback (most recent call last):
       14: from /Users/mirandashort/.rbenv/versions/2.7.4/bin/ruby_executable_hooks:22:in `<main>'
       13: from /Users/mirandashort/.rbenv/versions/2.7.4/bin/ruby_executable_hooks:22:in `eval'
       12: from /Users/mirandashort/.rbenv/versions/2.7.4/bin/irb:23:in `<main>'
       11: from /Users/mirandashort/.rbenv/versions/2.7.4/bin/irb:23:in `load'
       10: from /Users/mirandashort/.rbenv/versions/2.7.4/lib/ruby/gems/2.7.0/gems/irb-1.2.6/exe/irb:11:in `<top (required)>'
        9: from (irb):1
        8: from /Users/mirandashort/.rbenv/versions/2.7.4/lib/ruby/2.7.0/rubygems/core_ext/kernel_require.rb:147:in `require'
        7: from /Users/mirandashort/.rbenv/versions/2.7.4/lib/ruby/2.7.0/rubygems/core_ext/kernel_require.rb:158:in `rescue in require'
        6: from /Users/mirandashort/.rbenv/versions/2.7.4/lib/ruby/2.7.0/rubygems/core_ext/kernel_require.rb:158:in `require'
        5: from /Users/mirandashort/.rbenv/versions/2.7.4/lib/ruby/gems/2.7.0/gems/hackathon_ct_sdk-1.0.0/lib/hackathon_ct_sdk.rb:1176:in `<top (required)>'
        4: from /Users/mirandashort/.rbenv/versions/2.7.4/lib/ruby/2.7.0/rubygems/core_ext/kernel_require.rb:83:in `require'
        3: from /Users/mirandashort/.rbenv/versions/2.7.4/lib/ruby/2.7.0/rubygems/core_ext/kernel_require.rb:83:in `require'
        2: from /Users/mirandashort/.rbenv/versions/2.7.4/lib/ruby/gems/2.7.0/gems/hackathon_ct_sdk-1.0.0/lib/hackathon_ct_sdk/models/category_created_message.rb:16:in `<top (required)>'
        1: from /Users/mirandashort/.rbenv/versions/2.7.4/lib/ruby/gems/2.7.0/gems/hackathon_ct_sdk-1.0.0/lib/hackathon_ct_sdk/models/category_created_message.rb:17:in `<module:HackathonCtSdk>'
NameError (uninitialized constant HackathonCtSdk::Message)
Did you mean?  HackathonCtSdk::MessageAllOf
Related issues/PRs

There is an open issue for adding support to autoload classes: https://github.com/OpenAPITools/openapi-generator/issues/12648

This kind of error is something I do typically encounter in our rails application whenever there is an issue with autoloading classes (specifically if the path does not match the class name), so it could possibly be related. It could also be this issue with inheritance, which does reference the above issue: https://github.com/OpenAPITools/openapi-generator/issues/4690. It's unclear to me if this is the same issue though.

Suggest a fix

Ultimately the client is unable to load the class Message, but not sure where specifically the code is breaking.

bark-mshort commented 2 years ago

I actually think it is the same problem as https://github.com/OpenAPITools/openapi-generator/issues/4690 but it's closed. It looks like when I require 'message' in category_created_message, it now has trouble finding message_all_of. I'm not sure what the actual order needs to be for all classes to get loaded, but it seems to be that they are required in the module alphabetically. Autoloading the classes may fix that.

Fryguy commented 2 years ago

When you call openapi-generator generate, can you try passing the new useAutoload parameter as it's not the default? This should fix any require ordering issues.

bark-mshort commented 2 years ago

My coworker @BARK-TWOFFORD has taken the reigns on this project so I will defer to him! I've sent him the issue to test out

SalvatoreT commented 2 years ago

@Fryguy, I had an uninitialized constant issue as well, and setting useAutoload with OPENAPI_GENERATOR_VERSION=6.1.0-SNAPSHOT resolved it. ✅