OpenAPITools / OpenAPITools/openapi-generator

[protobuf-schema] Error trying to generate schema with an Enum list of integers

Open
#13,433 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Description

we try to generate a protobuf-schema but we have this error:

Exception in thread "main" java.lang.RuntimeException: Could not process model 'Event'.Please make sure that your schema is correct!
        at org.openapitools.codegen.DefaultGenerator.generateModels(DefaultGenerator.java:518)
        at org.openapitools.codegen.DefaultGenerator.generate(DefaultGenerator.java:912)
        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.ClassCastException: java.lang.Integer incompatible with java.lang.String
        at org.openapitools.codegen.languages.ProtobufSchemaCodegen.addEnumValuesPrefix(ProtobufSchemaCodegen.java:239)
        at org.openapitools.codegen.languages.ProtobufSchemaCodegen.postProcessModels(ProtobufSchemaCodegen.java:321)
        at org.openapitools.codegen.DefaultGenerator.processModels(DefaultGenerator.java:1322)
        at org.openapitools.codegen.DefaultGenerator.generateModels(DefaultGenerator.java:513)

I think it is because in our model we have something like this:

        severity:
          description: >
            Indicates the event severity level
          type: integer
          enum:
            - 1
            - 2
            - 3
            - 4
            - 5
            - 6

This is probably because the code implies this is a string and not an int here:

https://github.com/OpenAPITools/openapi-generator/blob/44d500ccf3d1ef282aba16dbcdf82cf366dc51ca/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/ProtobufSchemaCodegen.java#L238

Note that java and others ( javascript, python) do not fail with the same schema

openapi-generator version

Version 2.5.2

OpenAPI declaration file content or url

See description

Command line used for generation
  ${ROOT_DIR}/node_modules/.bin/openapi-generator-cli generate \
    -g "$target" \
    -i "${ROOT_DIR}/models/all.schema.yaml" \
    -p library=native \
    -o ${ROOT_DIR}/dist/$target \
    $config

with $target = protobuf-schema

Steps to reproduce

Run the command to generate a schema that has a enum list of integers

Related issues/PRs

Same area as #7168

Suggest a fix/enhancement

This is probably because the code implies this is a string and not an int here:

https://github.com/OpenAPITools/openapi-generator/blob/44d500ccf3d1ef282aba16dbcdf82cf366dc51ca/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/ProtobufSchemaCodegen.java#L238

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/ProtobufSchemaCodegen.java at addEnumValuesPrefix, then reproduce the failure with the provided protobuf-schema generation command and integer enum. Check how postProcessModels passes enum values, and consider the issue done when a schema with enum values 1 through 6 generates without the ClassCastException.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
api, tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.