fabien0102 / openapi-codegen

A tool for generating code base on an OpenAPI schema.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Support `readOnly` and `writeOnly`

olivierbeaulieu opened this issue · comments

This project doesn't seem to support these two properties from OAS. It would be a hugely helpful feature to support, as today we need to create distinct schemas for requests & responses.

Example: Given the OpenAPI spec below, the generated postThing function should have read_only_prop should only be present in the response, and write_only_prop should only be present in the request.

{
  "info": {
    "title": "Example"
  },
  "openapi": "3.0.2",
  "servers": [
    {
      "url": "https://example.com"
    }
  ],
  "paths": {
    "/post-thing": {
      "post": {
        "operationId": "post_thing",
        "requestBody": {
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/ExampleSchema"
              }
            }
          },
          "required": true
        },
        "responses": {
          "201": {
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ExampleSchema"
                }
              }
            },
            "description": "Success"
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "ExampleSchema": {
        "properties": {
          "read_only_prop": {
            "type": "string",
            "readOnly": true
          },
          "write_only_prop": {
            "type": "boolean",
            "writeOnly": true
          }
        },
        "type": "object"
      }
    }
  }
}

Nice suggestion, I was not aware of this feature!

For ref: https://swagger.io/docs/specification/data-models/data-types/#readonly-writeonly

How do you see the generated output for this feature?

Something like:

export const exampleSchemaReadonlyProps = ["read_only_prop"]
export const exampleSchemaWriteonlyProps = ["write_only_prop"]

So you can filter the keys later on. Or do you have a better idea?