Skip to content

Ideas for supporting readOnly and writeOnly? #14

@markandrus

Description

@markandrus

Very nice tool! Thank you :-)

Do you have ideas for how to support readOnly and writeOnly properties, where readOnly properties are omitted from POST, PUT, PATCH, etc., and writeOnly properties are omitted from GET? I've documented the current behavior below.

I think that, ideally the generated TypeBox schemas would have some way to be evaluated in the context of a "read" or "write" operation, which would result in certain properties being omitted. But I'm not sure (1) if that's something TypeBox should do, or (2) something @geut/openapi-box should do. IIUC, it's OpenAPI which extends the semantics of readOnly and writeOnly, and so should @geut/openapi-box be the one to handle this?

Given this schema…

openapi: 3.0.3
info:
  description: Title
  version: 1.0.0
servers:
  - url: https

paths:
  /records:
    get:
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/Record'
      responses:
        '200':
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Record'
    post:
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/Record'
      responses:
        '200':
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Record'
components:
  schemas:
    Record:
      type: object
      properties:
        readOnly:
          type: string
          readOnly: true
        writeOnly:
          type: string
          writeOnly: true
        both:
          type: string

@geut/openapi-box generates…

const ComponentsSchemasRecord = T.Object({
  readOnly: T.Optional(T.String({ readOnly: true })),
  writeOnly: T.Optional(T.String({ writeOnly: true })),
  both: T.Optional(T.String())
})

const schema = {
  '/records': {
    GET: {
      args: T.Optional(
        T.Object({
          body: T.Optional(
            CloneType(ComponentsSchemasRecord, {
              'x-content-type': 'application/json'
            })
          )
        })
      ),
      data: CloneType(ComponentsSchemasRecord, {
        'x-status-code': '200',
        'x-content-type': 'application/json'
      }),
      error: T.Union([T.Any({ 'x-status-code': 'default' })])
    },
    POST: {
      args: T.Optional(
        T.Object({
          body: T.Optional(
            CloneType(ComponentsSchemasRecord, {
              'x-content-type': 'application/json'
            })
          )
        })
      ),
      data: CloneType(ComponentsSchemasRecord, {
        'x-status-code': '200',
        'x-content-type': 'application/json'
      }),
      error: T.Union([T.Any({ 'x-status-code': 'default' })])
    }
  }
}

Background

I'm using TypeSpec to generate OpenAPI schemas, and @geut/openapi-box for converting those to TypeBox. TypeSpec generates schemas using readOnly and writeOnly (which is quite handy for reducing the size of the OpenAPI schema); however, I've yet to find a tool, at least in the TypeScript ecosystem, that handles this well. Because of that, I've had to avoid using readOnly and writeOnly in my OpenAPI schemas (related TypeSpec issue).

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