这是indexloc提供的服务,不要输入任何密码
Skip to content

Operation parameter default values should be generated in types #962

@nadavhames

Description

@nadavhames

Description

Lets say I have an openapi schema with an object property with a default value like this:

paths:
  /foo/bar:
    post:
      operationId: foo_bar
      parameters: []
      responses:
        '200':
          description: The request has succeeded.
          content:
            application/json:
              schema:
                anyOf:
                  - type: object
                    required:
                      - ok
                    properties:
                      ok:
                        type: boolean
                        default: true

I would expect that openapi-ts would generate approximately the following type definition containing the ok object property:

type FooBar = {
    ok: boolean = true;
}

but when I try and generate I get the following without true as the default for ok:

type FooBar = {
    ok: boolean;
}

Is there a way to achieve this with this library? Much appreciated!

OpenAPI 3.0 clarifies semantics of the default property keyword in section 4.7.25.1 of the spec:

The default value represents what would be assumed by the consumer of the input as the value of the schema if one is not provided. Unlike JSON Schema, the value MUST conform to the defined type for the Schema Object defined at the same level. For example, if type is string, then default can be "foo" but cannot be 1.

Reproducible example or configuration

No response

OpenAPI specification (optional)

Steps to reproduce: Just add default to any property - it does not appear in the generated types.
I would expect that in the Error type for this schema, the type should generate to have property message: boolean = true;

openapi: "3.0.0"
info:
  version: 1.0.0
  title: Swagger Petstore
  license:
    name: MIT
servers:
  - url: http://petstore.swagger.io/v1
paths:
  /pets:
    get:
      summary: List all pets
      operationId: listPets
      tags:
        - pets
      parameters:
        - name: limit
          in: query
          description: How many items to return at one time (max 100)
          required: false
          schema:
            type: integer
            maximum: 100
            format: int32
      responses:
        '200':
          description: A paged array of pets
          headers:
            x-next:
              description: A link to the next page of responses
              schema:
                type: string
          content:
            application/json:    
              schema:
                $ref: "#/components/schemas/Pets"
        default:
          description: unexpected error
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Error"
components:
  schemas:
    Pet:
      type: object
      required:
        - id
        - name
      properties:
        id:
          type: integer
          format: int64
        name:
          type: string
        tag:
          type: string
    Pets:
      type: array
      maxItems: 100
      items:
        $ref: "#/components/schemas/Pet"
    Error:
      type: object
      required:
        - code
        - message
      properties:
        code:
          type: integer
          format: int32
        message:
          type: boolean
          default: true

System information (optional)

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug 🔥Something isn't workingfeature 🚀New feature or request

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions