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

required and readyOnly/writeOnly properties #432

Open
stefanprobst opened this issue Nov 18, 2020 · 3 comments · May be fixed by #1145
Open

required and readyOnly/writeOnly properties #432

stefanprobst opened this issue Nov 18, 2020 · 3 comments · May be fixed by #1145
Assignees
Labels

Comments

@stefanprobst
Copy link

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"]
      }
    }
  }
}
@ferdikoomen
Copy link
Owner

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

@leohxj
Copy link

leohxj commented Dec 15, 2020

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

@ferdikoomen ferdikoomen added this to the @next milestone Jan 27, 2021
@ferdikoomen ferdikoomen removed this from the @next milestone Nov 11, 2021
@sweethuman
Copy link

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;
        }),
    ){}

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