Skip to content

Support readOnly/writeOnly fields in Schema Object #239

Open
@fmatzy

Description

@fmatzy

Description

readOnly/writeOnly fields for Schema Object properties is not reflected in the output type information.
Specification of readOnly/writeOnly is here.

Example input:

openapi: 3.0.3
info:
  title: example
  version: 0.1.0
servers:
  - url: https://example.com/v1
paths:
  /path:
    put:
      summary: Put Example
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: "#/components/schemas/ExampleObject"
      responses:
        "200":
          description: Success
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/ExampleObject"
components:
  schemas:
    ExampleObject:
      type: object
      required:
        - id
        - name
        - value
      properties:
        id:
          type: integer
          readOnly: true
        name:
          type: string
        value:
          type: integer
          writeOnly: true

I got

api/@types/index.ts:

/* eslint-disable */
export type ExampleObject = {
  id: number
  name: string
  value: number
}

api/path/index.ts:

/* eslint-disable */
import type * as Types from '../@types'

export type Methods = {
  put: {
    status: 200
    /** Success */
    resBody: Types.ExampleObject
    reqBody: Types.ExampleObject
  }
}

The id is readOnly: true and therefore not needed in the request, while the value is writeOnly: true and therefore not needed in the response.

Describe the solution you'd like

Expected output is like below:

/* eslint-disable */
import type * as Types from '../@types'

export type Methods = {
  put: {
    status: 200
    /** Success */
    resBody: Omit<Types.ExampleObject, "value">
    reqBody: Omit<Types.ExampleObject, "id">
  }
}

Omit the writeOnly properties for the resBody type, and conversely, omit the readOnly properties for the reqBody type.

Describe alternatives you've considered

Additional context

Environment

  • Package version: v0.21.0

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