swagger-api / swagger-api/swagger-codegen

In golang, Int enums with named fields generate errors

Open
#10,583 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

Description

If we have an enum definition

definitions:
  MyEnumType:
    type: integer
    format: int32
    x-numerical-enum: true
    enum:
    - Value
    - AnotherValue
    - Whatever

On v3.0.20 this produces a Java exception:

Exception in thread "Thread-1" java.lang.IllegalArgumentException: Cannot deserialize value of type `java.lang.Number` from String "Value": not a valid number                                         
 at [Source: UNKNOWN; line: -1, column: -1] (through reference chain: io.swagger.v3.oas.models.media.IntegerSchema["enum"]->java.util.ArrayList[0])                                                         
        at com.fasterxml.jackson.databind.ObjectMapper._convert(ObjectMapper.java:3938)                                                                                                                     
        at com.fasterxml.jackson.databind.ObjectMapper.convertValue(ObjectMapper.java:3869)                                                                                                                 
        at io.swagger.v3.core.util.ModelDeserializer.deserialize(ModelDeserializer.java:53)                                                                                                                 
        at io.swagger.v3.core.util.ModelDeserializer.deserialize(ModelDeserializer.java:28)                                                                                                                 
        at com.fasterxml.jackson.databind.ObjectMapper._convert(ObjectMapper.java:3933)                                                                                                                     
        at com.fasterxml.jackson.databind.ObjectMapper.convertValue(ObjectMapper.java:3869)                                                                                                                 
        at io.swagger.v3.parser.converter.SwaggerConverter.convert(SwaggerConverter.java:1204)                                                                                                              
        at io.swagger.v3.parser.converter.SwaggerConverter.convert(SwaggerConverter.java:260)                                                                                                               
        at io.swagger.v3.parser.converter.SwaggerConverter.readResult(SwaggerConverter.java:110)                                                                                                           
        at io.swagger.v3.parser.converter.SwaggerConverter.readLocation(SwaggerConverter.java:91)                                                                                                           
        at io.swagger.parser.OpenAPIParser.readLocation(OpenAPIParser.java:16)                                                                                                                              
        at io.swagger.codegen.v3.config.CodegenConfigurator.toClientOptInput(CodegenConfigurator.java:517)                                                                                                  
        at io.swagger.codegen.v3.cli.cmd.Generate.run(Generate.java:367)                                                                                                                                    
        at java.lang.Thread.run(Thread.java:748)                                                                                                                                                            
Caused by: com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of type `java.lang.Number` from String "Value": not a valid number                                       
 at [Source: UNKNOWN; line: -1, column: -1] (through reference chain: io.swagger.v3.oas.models.media.IntegerSchema["enum"]->java.util.ArrayList[0])                                                         
        at com.fasterxml.jackson.databind.exc.InvalidFormatException.from(InvalidFormatException.java:67)                                                                                                   
        at com.fasterxml.jackson.databind.DeserializationContext.weirdStringException(DeserializationContext.java:1679)                                                                                     
        at com.fasterxml.jackson.databind.DeserializationContext.handleWeirdStringValue(DeserializationContext.java:935)                                                                                    
        at com.fasterxml.jackson.databind.deser.std.NumberDeserializers$NumberDeserializer.deserialize(NumberDeserializers.java:826)                                                                        
        at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:286)                                                                                     
        at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:245)                                                                                     
        at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:27)                                                                                      
        at com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:129)                                                                                              
        at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:288)                                                                                              
        at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:151)                                                                                                     
        at com.fasterxml.jackson.databind.ObjectMapper._convert(ObjectMapper.java:3933)                                                                                                                     
        ... 13 more                                                                                                                           

On v2.4.2 this compiles, but produces code that doesn't compile:

package swagger

type MyEnumType int32

// List of MyEnumType
const (
        VALUE MyEnumType = "Value"
        ANOTHER_VALUE MyEnumType = "AnotherValue"
        WHATEVER MyEnumType = "Whatever"
)
Swagger-codegen version

2.4.2, 3.0.20

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

Reproduce the definition with Swagger-codegen 2.4.2 and 3.0.20, then trace the parser path through ModelDeserializer.java and SwaggerConverter.java. Check the Go generator output for MyEnumType and verify that parsing no longer throws an exception and the generated Go code compiles.

Written by the indexing model from the issue text.

Assessment

Tech stack
go, java, openapi
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.