swagger-api / swagger-codegen

swagger-codegen contains a template-driven engine to generate documentation, API clients and server stubs in different languages by parsing your OpenAPI / Swagger definition.
http://swagger.io
Apache License 2.0
16.94k stars 6.03k forks source link

swagger-codegen-cli omits integer default values #6929

Open MartinDelVecchio opened 6 years ago

MartinDelVecchio commented 6 years ago
Description

I am using swagger-codegen-cli.jar to generate JSON with "-l swagger" from my input YAML. This tool does not include default values specified for integer properties in object types

Swagger-codegen version

2.2.3

Swagger declaration file content or url

Input YAML file:

swagger: '2.0'
info:
  title: Settings API
  contact:
    name: Somebody
    url: https://settings.com
    email: settings@settings.com
  description: Allows the user to get some settings
  version: v1
host: api.settings.com
schemes:
- https
- http
basePath: /api/v1/stuff
produces:
- application/json

paths:
  /settings:
    get:
      summary: Get the settings
      responses:
        200:
          description: The settings
          schema:
            $ref: '#/definitions/Settings'

definitions:
  Settings:
    summary: The settings object
    type: object
    properties:
      StringProperty:
        type: string
        summary: A string property
        default: "Stringy"
      BooleanProperty:
        type: boolean
        summary:  A boolean property
        default: true
      IntProperty:
        type: integer
        summary: An integer property
        default: 12345

Command line:

java -jar swagger-codegen-cli.jar generate -i input.yaml -l swagger -o .

Output JSON:


{
  "swagger" : "2.0",
  "info" : {
    "description" : "Allows the user to get some settings",
    "version" : "v1",
    "title" : "Settings API",
    "contact" : {
      "name" : "Somebody",
      "url" : "https://settings.com",
      "email" : "settings@settings.com"
    }
  },
  "host" : "api.settings.com",
  "basePath" : "/api/v1/stuff",
  "schemes" : [ "https", "http" ],
  "produces" : [ "application/json" ],
  "paths" : {
    "/settings" : {
      "get" : {
        "summary" : "Get the settings",
        "parameters" : [ ],
        "responses" : {
          "200" : {
            "description" : "The settings",
            "schema" : {
              "$ref" : "#/definitions/Settings"
            }
          }
        }
      }
    }
  },
  "definitions" : {
    "Settings" : {
      "type" : "object",
      "properties" : {
        "StringProperty" : {
          "type" : "string",
          "default" : "Stringy"
        },
        "BooleanProperty" : {
          "type" : "boolean",
          "default" : true
        },
        "IntProperty" : {
          "type" : "integer"
        }
      }
    }
  }
}

Note that the default values for the string and boolean properties are included, but the default value for the integer property (12345) is missing.

##### Command line used for generation

java -jar swagger-codegen-cli.jar generate -i input.yaml -l swagger -o .

##### Steps to reproduce

Type the above command

##### Related issues/PRs

##### Suggest a fix/enhancement
MartinDelVecchio commented 6 years ago

Interestingly, when I added "format: int32" to the integer property definition:

      IntProperty:
        type: integer
        format: int32
        summary: An integer property
        default: 12345

It worked:

        "IntProperty" : {
          "type" : "integer",
          "format" : "int32",
          "default" : 12345

The Swagger spec says that "format" is optional:

An optional format keyword serves as a hint for the tools to use a specific numeric type:

So I don't think it should be required here.

Thanks.

MartinDelVecchio commented 6 years ago

Any interest in addressing this issue?

Thanks.

DarkWingDuckWing commented 6 years ago

I am seeing the same issue when generating Java models with swagger-codegen-cli and the JavaClientCodegen, the defaults are not generated for Integer types.

andreathacker commented 6 years ago

@dankling are you able to get defaults generated for other types? I'm not seeing any of the defaults being generated. Could you post your source and output files?

thinw002 commented 5 years ago

Same issue here. I'm getting model defaults for boolean types but not integer. using -l csharp and -DdebugModels I'm not seeing "defaultValue" in the output.

erccarls commented 5 years ago

Same issue here for python as well.

mdelvecchio-wasabi commented 4 years ago

Two years and two jobs later, circling back to this.

Disappointing to see that it has not been addressed.