openapi-generator icon indicating copy to clipboard operation
openapi-generator copied to clipboard

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

Open jm-paulin opened this issue 3 years ago • 0 comments

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

jm-paulin avatar Sep 15 '22 15:09 jm-paulin