Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ideas for supporting readOnly and writeOnly? #14

Open
markandrus opened this issue Dec 13, 2024 · 0 comments
Open

Ideas for supporting readOnly and writeOnly? #14

markandrus opened this issue Dec 13, 2024 · 0 comments

Comments

@markandrus
Copy link
Contributor

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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant