swagger-api / swagger-api/swagger-parser

Default values in models unset in openAPI result, v2 spec

Open
#971 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Java
Stars
867
Forks
560
Avg merge
2d 21h
Merged PRs (30d)
7

Description

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.

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

Start with the OpenAPIParser path that converts the supplied Swagger 2.0 YAML, then reproduce the result using swagger-codegen with the -DdebugSwagger flag. Compare the emitted model properties and verify that default values for array, integer, and number types are retained alongside the existing string and boolean defaults.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, openapi
Domain
api, backend-api-design
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.