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.94k stars 6.59k forks source link

[BUG][OCaml] Generating stripe api fails with unreferenced enums #16890

Open joprice opened 1 year ago

joprice commented 1 year ago

Bug Report Checklist

Description

The openapi specification for Stripe fails with the following error:

Exception in thread "main" java.lang.RuntimeException: Could not process model 'payment_source'.Please make sure that your schema is correct!
    at org.openapitools.codegen.DefaultGenerator.generateModels(DefaultGenerator.java:539)
    at org.openapitools.codegen.DefaultGenerator.generate(DefaultGenerator.java:960)
    at org.openapitools.codegen.cmd.Generate.execute(Generate.java:511)
    at org.openapitools.codegen.cmd.OpenApiGeneratorCommand.run(OpenApiGeneratorCommand.java:32)
    at org.openapitools.codegen.OpenAPIGenerator.main(OpenAPIGenerator.java:66)
Caused by: java.lang.IllegalArgumentException: Unreferenced enum custom,express,standard,ach_credit_transfer,ach_debit,acss_debit,alipay,au_becs_debit,bancontact,card,card_present,eps,giropay,ideal,klarna,multibanco,p24,sepa_debit,sofort,three_d_secure,wechat
    at org.openapitools.codegen.languages.OCamlClientCodegen.toEnumName(OCamlClientCodegen.java:787)
    at org.openapitools.codegen.DefaultCodegen.fromProperty(DefaultCodegen.java:4057)
    at org.openapitools.codegen.DefaultCodegen.fromProperty(DefaultCodegen.java:3854)
    at org.openapitools.codegen.DefaultCodegen.addVars(DefaultCodegen.java:5882)
    at org.openapitools.codegen.DefaultCodegen.addVars(DefaultCodegen.java:5803)
    at org.openapitools.codegen.DefaultCodegen.updateModelForComposedSchema(DefaultCodegen.java:2850)
    at org.openapitools.codegen.DefaultCodegen.fromModel(DefaultCodegen.java:3132)
    at org.openapitools.codegen.DefaultGenerator.processModels(DefaultGenerator.java:1341)
    at org.openapitools.codegen.DefaultGenerator.generateModels(DefaultGenerator.java:534)
    ... 4 more

With a subset of the spec that contains an enum with an empty string, the following exception is thrown:

    at org.openapitools.codegen.DefaultGenerator.generateModels(DefaultGenerator.java:539)
    at org.openapitools.codegen.DefaultGenerator.generate(DefaultGenerator.java:960)
    at org.openapitools.codegen.cmd.Generate.execute(Generate.java:511)
    at org.openapitools.codegen.cmd.OpenApiGeneratorCommand.run(OpenApiGeneratorCommand.java:32)
    at org.openapitools.codegen.OpenAPIGenerator.main(OpenAPIGenerator.java:66)
Caused by: java.lang.IllegalArgumentException: Unreferenced enum
    at org.openapitools.codegen.languages.OCamlClientCodegen.toEnumName(OCamlClientCodegen.java:787)
    at org.openapitools.codegen.DefaultCodegen.fromProperty(DefaultCodegen.java:4057)
    at org.openapitools.codegen.DefaultCodegen.fromProperty(DefaultCodegen.java:3854)
    at org.openapitools.codegen.DefaultCodegen.getComposedProperties(DefaultCodegen.java:8183)
    at org.openapitools.codegen.DefaultCodegen.getComposedSchemas(DefaultCodegen.java:8165)
    at org.openapitools.codegen.DefaultCodegen.fromModel(DefaultCodegen.java:3106)
    at org.openapitools.codegen.DefaultGenerator.processModels(DefaultGenerator.java:1341)
    at org.openapitools.codegen.DefaultGenerator.generateModels(DefaultGenerator.java:534)
    ... 4 more
openapi-generator version

7.0.1

OpenAPI declaration file content or url

https://github.com/stripe/openapi/blob/master/openapi/spec3.json

Generation Details
openapi-generator-cli generate -i spec3.json -g ocaml -o stripe-api
Steps to reproduce

Run above command and see that it quickly fails with the first exception. Slimming down the spec to a single endpoint that has an empty string as an enum shows the second error:

cat spec3.json | jq '(.paths = {"/v1/customers": .paths["/v1/customers"]}) | .components = {}' > spec3-slim.json
openapi-generator-cli generate -i spec3-slim.json -g ocaml -o stripe-api --skip-validate-spec

An example of the enum that trips it up is:

"support_url": {
                          "anyOf": [
                            {
                              "type": "string"
                            },
                            {
                              "enum": [
                                ""
                              ],
                              "type": "string"
                            }
                          ]
                        }

After removing the empty enum, the generation succeeds for this single endpoint.

Related issues/PRs
Suggest a fix

For the first issue, it's unclear to me what the underlying problem is. For the second issue, it seems that empty string enums trip up the generator. I tried this with the java generator and it succeeded, so it seems to be specific to this generator.

wing328 commented 1 year ago

the rule SIMPLIFY_ANYOF_STRING_AND_ENUM_STRING in openapi normalizer can help handle such case: https://github.com/OpenAPITools/openapi-generator/blob/master/docs/customization.md#openapi-normalizer

please give it a try when you've time.

joprice commented 1 year ago

Thank you. I'll try that. However, this schema does work with at least one other language's generator. I didn't run the generated java code to test it. Perhaps the java generator is less strict and actually generates an invalid result?

wing328 commented 1 year ago

Java (client) generator supports anyOf but OCaml client generator doesn't yet support oneOf/anyOf. We welcome contributions to improve the Ocaml Client generator.