Skip to content

Default values in models unset in openAPI result, v2 spec #971

@spacether

Description

@spacether

When one defines a swagger spec with a model per the below yaml

swagger: "2.0"
info:
  title: "carrier api"
  description: "boop boop"
  version: "1.0.0"
host: "example.com"
basePath: "/api"
schemes:
- "http"
paths:
  /putter:
    put:
      summary: "a summary"
      description: "a description"
      operationId: "putter"
      consumes:
      - "application/json"
      produces:
      - "application/json"
      parameters:
        - in: body
          name: TypeHolder
          description: "type holder"
          required: true
          schema:
            $ref: "#/definitions/TypeHolder"
      responses:
        200:
          description: "Valid input"


definitions:
  TypeHolder:
    type: object
    required:
      - string_item
      - number_item
      - integer_item
      - bool_item
      - array_item
      - object_item
      - date_item
      - datetime_item
    properties:
      string_item:
        type: string
        # default: 'what'
        example: 'what'
      number_item:
        type: number
        default: 1.234
        example: 1.234
      integer_item:
        type: integer
        default: -2
        example: -2
      bool_item:
        type: boolean
        # default: true
        example: true
      array_item:
          type: array
          items:
            type: integer
          default:
            - 0
            - 1
            - 2
            - 3
          example:
            - 0
            - 1
            - 2
            - 3
      object_item:
        type: object
        properties:
          id:
            type: integer
            default: 0
            example: 0
          name:
            type: string
            default: "key_val"
            example: "key_val"
      date_item:
        type: string
        format: date
        default: 2017-07-21
      datetime_item:
        type: string
        format: date-time
        default: 2017-07-21T17:32:28Z

Default values are returned by the OpenAPIParser for string and bool types only.
Default values are missing for array, integer, and number.

We can see this from the terminal results of swagger-codegen with the -DdebugSwagger flag on

  "components" : {
    "schemas" : {
      "TypeHolder" : {
        "required" : [ "array_item", "bool_item", "integer_item", "number_item", "object_item", "string_item" ],
        "type" : "object",
        "properties" : {
          "string_item" : {
            "type" : "string",
            "example" : "what",
            "default" : "what"
          },
          "number_item" : {
            "type" : "number",
            "example" : 1.234
          },
          "integer_item" : {
            "type" : "integer",
            "example" : -2
          },
          "bool_item" : {
            "type" : "boolean",
            "example" : true,
            "default" : true
          },
          "array_item" : {
            "type" : "array",
            "example" : [ 0, 1, 2, 3 ],
            "items" : {
              "type" : "integer"
            }
          },
          "object_item" : {
            "type" : "object",
            "properties" : {
              "name" : {
                "type" : "string",
                "example" : "key_val",
                "default" : "key_val"
              },
              "id" : {
                "type" : "integer",
                "example" : 0
              }
            }
          },

Question

  • Can someone point me to where this parsing is done? I am having trouble finding the location in the code base. I am happy to submit a PR to fix this issue if someone can help me find the code doing it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions