openapi-typescript-codegen icon indicating copy to clipboard operation
openapi-typescript-codegen copied to clipboard

required and readyOnly/writeOnly properties

Open stefanprobst opened this issue 5 years ago • 4 comments

hi, i'm curious how this library handles the contextual behavior of required properties interacting with readOnly/writeOnly?

from https://swagger.io/specification

  • "If the property is marked as readOnly being true and is in the required list, the required will take effect on the response only."
  • "If the property is marked as writeOnly being true and is in the required list, the required will take effect on the request only."

i have tried with the following example, and it looks like marking the property as readonly does not fully capture the above behavior?

Example
{
  "openapi": "3.0.0",
  "info": {
    "title": "Test API",
    "version": "1.0.0"
  },
  "paths": {
    "/api/tests": {
      "get": {
        "operationId": "getTests",
        "responses": {
          "200": {
            "description": "",
            "content": {
              "application/json": {
                "schema": { "type": "array", "items": { "$ref": "#/components/schemas/Test" } }
              }
            }
          }
        }
      },
      "post": {
        "operationId": "createTest",
        "requestBody": {
          "content": {
            "application/json": { "schema": { "$ref": "#/components/schemas/Test" } }
          }
        },
        "responses": {
          "200": {
            "description": "",
            "content": {
              "application/json": { "schema": { "$ref": "#/components/schemas/Test" } }
            }
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "Test": {
        "type": "object",
        "properties": {
          "id": { "type": "integer" },
          "res": { "type": "string", "readOnly": true },
          "req": { "type": "string", "writeOnly": true }
        },
        "required": ["req", "res"]
      }
    }
  }
}

stefanprobst avatar Nov 18 '20 17:11 stefanprobst

@stefanprobst you might be right, it could be that this is not taken into account

ferdikoomen avatar Nov 18 '20 19:11 ferdikoomen

"If the property is marked as readOnly being true and is in the required list, the required will take effect on the response only."

So here is a bug now, we generated readOnly+required field as required in requestBody. :)

leohxj avatar Dec 15 '20 03:12 leohxj

For omitting read-only I recommend using a utility type for typescript. It could be placed for any request. I use this utility type

export type OmitReadonly<T extends object> = Omit<T, ReadonlyKeys<T>>

ReadonlyKeys is a utilty type from ts-essentials Omit is a utility type built in typescript

In the end generating a function like this:

postCommentsArticle(
        article: string,
        requestBody?: (OmitReadonly<Comment> & {
            /**
             * Email of the author
             */
            email: string;
        }),
    ){}

sweethuman avatar Jan 23 '22 16:01 sweethuman