Swagger-codegen: Swagger codegen generates a nullable integer as method return value in SDK for operation that returns an integer

Created on 2 Nov 2017  路  7Comments  路  Source: swagger-api/swagger-codegen

Description

I have an operation which returns an integer, and when I generate a C# SDK using codegen, the method associated with the operation returns a nullable integer instead of an integer. I tried to set the produced response schema as required using an operation filter and setting the default value as 0 but it had no effect.

Here is the definition of my operation responses:

       "responses":{  
           "200":{  
              "description":"Success",
              "schema":{  
                 "format":"int32",
                 "default":0,
                 "required":[  
                    "200"
                 ],
                 "type":"integer"
              }
           }
        }

Thank you for any insight

C-Sharp General Question help wanted

Most helpful comment

I also stumbled upon this.
I think that simply changing the mapping is not a good idea. for optional method arguments it still should be nullable. For required arguments it should not be nullable. Not sure if you can tell that to mustache ;)

All 7 comments

@bzglm currently, the type mapping for C# client uses nullable type (e.g. int?)

You can try the type mapping option to map the integer to just int:

        --type-mappings <type mappings>
            sets mappings between swagger spec types and generated code types in
            the format of swaggerType=generatedType,swaggerType=generatedType.
            For example: array=List,map=Map,string=String

but likely you will need to modify the templates to skip some syntax errors (you can use customized templates with the -t option)

Thank you very much for your answer. I tried to use the type-mappings option (providing integer=int as parameter), but unfortunately, I have the following error:

[main] WARN io.swagger.codegen.languages.AbstractCSharpCodegen - int(reserved word) cannot be used as model name. Renamed to ModelInt

Is there something I am missing ?

Seems like "int" is treated as a model instead. We'll need to update the list in https://github.com/swagger-api/swagger-codegen/blob/master/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/AbstractCSharpCodegen.java#L94 to include non-nullable primitive type as well.

I second this request, much needed indeed.

@albator1932 would you have time to file a PR to update AbstractCSharpCodegen.java#L94?

I'm afraid I have other priorities (work) right now and for the next two weeks but I'll what I can do then if it is still to be done at that time.

I also stumbled upon this.
I think that simply changing the mapping is not a good idea. for optional method arguments it still should be nullable. For required arguments it should not be nullable. Not sure if you can tell that to mustache ;)

Was this page helpful?
0 / 5 - 0 ratings