Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to import API into ruby client #268

Closed
bark-mshort opened this issue Jul 11, 2022 · 11 comments
Closed

Unable to import API into ruby client #268

bark-mshort opened this issue Jul 11, 2022 · 11 comments

Comments

@bark-mshort
Copy link

bark-mshort commented Jul 11, 2022

I'm trying to generate a ruby client via OpenAPI Generator. We've used both the npm installation approach as well as the homebrew installation. When trying to generate the client via CLI, we run into a number of warnings and some errors that end up blocking file generation.

Steps to reproduce

Install CLI

brew install openapi-generator

Generate 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

Errors

Exception in thread "main" org.openapitools.codegen.SpecValidationException: 
There were issues with the specification. The option can be disabled via validateSpec (Maven/Gradle) or --skip-validate-spec (CLI).
 | Error count: 80, Warning count: 210

The primary error seems to be

        -Null key for a Map not allowed in JSON (use a converting NullKeySerializer?) (through reference chain: java.util.LinkedHashMap["components"]->java.util.LinkedHashMap["schemas"]->java.util.LinkedHashMap["Message"]->java.util.LinkedHashMap["allOf"]->java.util.ArrayList[1]->java.util.LinkedHashMap["discriminator"]->java.util.LinkedHashMap["mapping"]->java.util.LinkedHashMap["null"])

But there are also a bunch of missing schema errors too (unsure if it's related to the above error):

  • -attribute components.schemas.ReplicaMyCartDraft.items is missing
  • -attribute paths.'/{projectKey}/product-projections/search'(post).requestBody.content.'application/x-www-form-urlencoded'.schema.#/components/schemas/string is missing

As is suggested by the generator, I appended to --skip-validate-spec to the generator command and all files successfully populate, but the classes in the newly created gem are failing to load:

hackathon-ct-sdk main 18m ⍉ ➜ irb
irb(main):001:0> require 'hackathon_ct_sdk'
Traceback (most recent call last):
        .......
        .......
        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

Could this be an issue with the spec?

@jenschude
Copy link
Contributor

The OAS definition is generated. I ran it through a validator and found some smaller issues. I fixed it in a branch https://github.com/commercetools/commercetools-api-reference/compare/oas_fix

Can't merge it as before the code generator has to be adjusted

@jenschude
Copy link
Contributor

I fixed the issues in the generator and the OpenApi spec file is now valid

@bark-mshort
Copy link
Author

@jenschude I'm no longer getting the errors for certain schemas that are missing, but I'm still getting the below error - are there any additional updates I need to run and/or arguments that need passed for it to run successfully?

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
 | Error count: 1, Warning count: 210
Errors: 
        -Null key for a Map not allowed in JSON (use a converting NullKeySerializer?) (through reference chain: java.util.LinkedHashMap["components"]->java.util.LinkedHashMap["schemas"]->java.util.LinkedHashMap["Message"]->java.util.LinkedHashMap["allOf"]->java.util.ArrayList[1]->java.util.LinkedHashMap["discriminator"]->java.util.LinkedHashMap["mapping"]->java.util.LinkedHashMap["null"])
Warnings: 
        -Null key for a Map not allowed in JSON (use a converting NullKeySerializer?) (through reference chain: java.util.LinkedHashMap["components"]->java.util.LinkedHashMap["schemas"]->java.util.LinkedHashMap["Message"]->java.util.LinkedHashMap["allOf"]->java.util.ArrayList[1]->java.util.LinkedHashMap["discriminator"]->java.util.LinkedHashMap["mapping"]->java.util.LinkedHashMap["null"])

        at org.openapitools.codegen.config.CodegenConfigurator.toContext(CodegenConfigurator.java:604)
        at org.openapitools.codegen.config.CodegenConfigurator.toClientOptInput(CodegenConfigurator.java:631)
        at org.openapitools.codegen.cmd.Generate.execute(Generate.java:457)
        at org.openapitools.codegen.cmd.OpenApiGeneratorCommand.run(OpenApiGeneratorCommand.java:32)
        at org.openapitools.codegen.OpenAPIGenerator.main(OpenAPIGenerator.java:66)

@jenschude
Copy link
Contributor

jenschude commented Jul 12, 2022

Ah there is a small mistake still inside.

Could you just remove line 76330

              InventoryEntryQuantitySet: '#/components/schemas/InventoryEntryQuantitySetMessage'
              null: '#/components/schemas/OrderMessage'         # <---- this line here
              OrderPaymentAdded: '#/components/schemas/OrderPaymentAddedMessage'

and line 79819

          InventoryEntryQuantitySet: '#/components/schemas/InventoryEntryQuantitySetMessagePayload'
          null: '#/components/schemas/OrderMessagePayload'             # <---- this line here
          OrderPaymentAdded: '#/components/schemas/OrderPaymentAddedMessagePayload'

@bark-mshort
Copy link
Author

Same error - looks like the last thing that runs before I do receive the error is for ZoneAddLocationAction_allOf

@bark-mshort
Copy link
Author

bark-mshort commented Jul 12, 2022

Correction - it is a different error but seems to still be caused by a null request body:

  Exception: Request body cannot be null. Possible cause: missing schema in body parameter (OAS v2): class RequestBody {
    description: null
    content: class Content {
        {*/*=class MediaType {
            schema: null
            examples: null
            example: null
            encoding: null
        }}
    }
    required: null
}
        at org.openapitools.codegen.DefaultGenerator.processOperation(DefaultGenerator.java:1186)
        at org.openapitools.codegen.DefaultGenerator.processPaths(DefaultGenerator.java:1080)
        at org.openapitools.codegen.DefaultGenerator.generateApis(DefaultGenerator.java:571)
        at org.openapitools.codegen.DefaultGenerator.generate(DefaultGenerator.java:914)
        at org.openapitools.codegen.cmd.Generate.execute(Generate.java:465)
        at org.openapitools.codegen.cmd.OpenApiGeneratorCommand.run(OpenApiGeneratorCommand.java:32)
        at org.openapitools.codegen.OpenAPIGenerator.main(OpenAPIGenerator.java:66)
Caused by: java.lang.RuntimeException: Request body cannot be null. Possible cause: missing schema in body parameter (OAS v2): class RequestBody {
    description: null
    content: class Content {
        {*/*=class MediaType {
            schema: null
            examples: null
            example: null
            encoding: null
        }}
    }
    required: null

@jenschude
Copy link
Contributor

It was the issue with the image upload endpoint, that the code generator expected some body definition whereas the spec says it's valid to have no body type at all. 🤷

Fixed it to be a string. Tried your command and it works now.

@bark-mshort
Copy link
Author

@jenschude thanks! It successfully runs now - but still getting this error when I go to require the gem from the irb console

        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

@jenschude
Copy link
Contributor

Don't know what the openapi generator is creating there and i don't know enough of ruby to be of help here.

@bark-mshort
Copy link
Author

Okay I can open an Issue with openapi and make sure it's not an issue with the ruby generator. Essentially it's not able to load the class Message (but I can confirm the model is there). I'll see if they can confirm whether or not it's an issue on the commercetools side or their side.

@bark-mshort
Copy link
Author

@jenschude I think this is an issue with the openapi generator after all, so I'm going to go ahead and close this issue. Thanks for all your help!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants