question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Default values in models unset in openAPI result, v2 spec

See original GitHub issue

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.

Issue Analytics

  • State:open
  • Created 5 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
spacethercommented, Feb 19, 2019

Hi @gracekarina unfortunately I’m swamped with my normal work and other open source contributions right now. My above spec fully defines the issue.

Have you tried re-creating it? Could you add the test case?

In ~ a week I may have more time. If I have time then, where should I store the java test file, and where would I store the sample spec in this project?

0reactions
spacethercommented, Mar 4, 2019

@gracekarina this bug also exists for date and date-time strings. Defaults are not parsed and returned from the spec file. I have updated my example up above to show that.

Read more comments on GitHub >

github_iconTop Results From Across the Web

OpenAPI Specification - Version 2.0 - Swagger
Version 2.0 specification defines a set of files required to describe an API. ... Otherwise, the property MAY be included and its default...
Read more >
Can a swagger object passed as a parameter have default ...
Swagger 2.0 spec Defines schema default as default (Unlike JSON Schema, the value MUST conform to the defined type for the Schema Object)....
Read more >
Extending OpenAPI - FastAPI - tiangolo
It just returns a JSON response with the result of the application's .openapi() method. By default, what the method .openapi() does is check...
Read more >
Usage - OpenAPI Generator
displays the reserved words which may result in renamed model or ... Go codegen comment to disable Go Lint and collapse by default...
Read more >
Advanced Input And Output Modeling - API Handyman
Writing OpenAPI (Swagger) Specification Tutorial Series - Part 5 ... Required or optional parameter; Parameter with default value ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found