required and readyOnly/writeOnly properties
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 you might be right, it could be that this is not taken into account
"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. :)
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;
}),
){}