Repository navigation
Description
These modifiers are on specific properties and allow for a single definition to be used in both read & write endpoints, but provide a different shape to each -
// ... "Data": { ...
"properties": {
"id": { // Returned by GET, not used in POST/PUT/PATCH
"type": "integer",
"readOnly": true
},
"username": {
"type": "string"
},
"password": { // Used in POST/PUT/PATCH, not returned by GET
"type": "string",
"writeOnly": true
}
}Currently this just generates a single interface similar to -
export interface components {
schemas: {
Data: {
id: number;
username: string;
password: string;
};
};
}Unfortunately that makes it unusable for any endpoint as GET doesn't show the password and POST etc doesn't want an id. One way to change this is to use multiple interfaces (alternatively using a suffix for the schema might be suitable - but there is a possibility of a name clash that way) - possibly something like -
export interface components {
read_schemas: {
Data: {
id: number;
username: string;
};
};
write_schemas: {
Data: {
username: string;
password: string;
};
};
schemas: {
Data: components["read_schemas"]["Data"] | components["read_schemas"]["Data"];
}
}Alternatively it could go the other way around, although I feel this makes the typing slightly less useful (adding type guards to code is pretty simple, but they like the union types more) -
export interface components {
read_schemas: {
Data: components["schemas"]["Data"] & {
id: number;
};
};
write_schemas: {
Data: components["schemas"]["Data"] & {
password: string;
};
};
schemas: {
Data: {
username: string;
};
}
}If there's no readOnly or writeOnly properties then nothing would need to change for any schemas that don't use them.
Activity
drwpow commentedon May 26, 2021
Interesting! I had been wondering if
readOnlyandwriteOnlyneeded representation in TypeScript somehow, but I had personally never used them in a schema. Your example makes sense.In the past, I’ve been bitten by trying to remap or rename generated types. Because no matter how clever you think you are, there’s always some wild schema someone uses that breaks your assumptions, and you have to release a breaking version (that’s v1 of this library in a nutshell)! So I’m hesitant to have dynamically-generated components that don’t exist in your schema, but I do agree with coming up with some solution.
We could possibly do something clever with Extract<> or Pick<> inside
responsesandrequests? In other words, we leavecomponents["schemas"]["Data"]alone, but modify the properties returned forresponses[…]andrequests[…](basically wherever it’s referenced)?Rycochet commentedon May 26, 2021
That's pretty much what I'm doing now (but with
Omit<>due to length - so I only need to add the ones that come from the "opposite" side):Of the two the Pick is definitely clearer what is inside there, but if the properties are pretty long it could make for a lot of duplication - not exactly a problem for generated code, and far easier to just look at it to see what's included in there - Prettier can handle the line length!)
The potential downside I can see is that
components["schemas"]["Data"]is not a union, and is instead assumed to be totally that content, which means code using it could be wrong by accessing bothidandpasswordin the same code (type guard + union ftw lol).Having those read and write within the requests and responses would make it a lot safer though, and for that reason alone makes a lot of sense.
At the very least, adding comments to the end of the schema lets the person reading see that there's something going on? (Could do before, but that can get mixed up with the
descriptionif it exists, looking at the code adding a comment to the end isn't done anywhere?)Rycochet commentedon May 27, 2021
Ok, put a bit of time into this - and got code in there that works, however using
Pick<>style actually doesn't work - nested properties breaks it. The root property will be correct, but anything further down doesn't know that it should be in a readonly / writeonly state - will have a try with the multiple-schema style instead but wanted to report that back before anyone else spends time on it!olivierbeaulieu commentedon Feb 15, 2023
Hi! I've keeping an eye on this issue for a while and finally decided to take a stab at it. I think I've found a pretty simple solution that doesn't rely on
PickorOmit, and it seems to work pretty well. I haven't written tests yet, but I wanted to share the approach here and get some feedback: https://github.com/drwpow/openapi-typescript/compare/main...olivierbeaulieu:pr/read-write-only?expand=1In short, we simply add a generic param on the top-level interfaces (
components), and set the mode to'read'or'write'depending on whether we're looking at a request or a response context. We pass down this generic value every time we consume a$ref, allowing this to work with nested properties.A few key points:
readOnlyandwriteOnlyare ignoredcomponents<'read'>['schemas']['MySchema'])If the approach makes sense, I can get the PR up shortly.
bryanhoulton commentedon Aug 26, 2023
Any progress on this? Running into similar issues.
tnagorra commentedon Sep 8, 2023
I tried the approach described by @olivierbeaulieu with minor changes.
The approach works nicely.
ashe0047 commentedon Nov 27, 2023
How did you implement the approach mentioned by @olivierbeaulieu ? Do you mind sharing what changes to make after generating the types using this library?
H01001000 commentedon Feb 13, 2024
Sorry for asking but any update?
elmarsto commentedon Feb 28, 2024
Is there anything I can do to expedite this? Some frameworks, like Django, use this feature of OpenAPI extensively. At the moment I'm stuffing extra fields into my requests just to make openapi-fetch happy, which is a pity because the whole point of using it was to get a 1:1 correspondence between frontend and backend :/
NexRX commentedon Mar 10, 2024
I'm considering using @olivierbeaulieu fork because this feature is really important to me, Would be awesome to see his work merged :)
drwpow commentedon Mar 12, 2024
Chiming in because I realize I haven’t replied to @olivierbeaulieu’s suggestion: I think that is an elegant solution! And you’re right that’s much more “TypeScript-y” than some of the original ideas about doing deep-schema traversal and modifying things in a more heavy-handed (and unpredictable way). TypeScript’s inference can do a lot when you lean into it!
I’d be open to adding a flag to support this behavior—this seems simple enough—and do expect some edge cases where the inference fails, but maybe that’s OK? If people have been using this method with different schemas for a while, and getting good results, then I’d be happy to add it to the project. Sorry for being accidentally silent on this one (wasn’t intentional), and also very happy to see a lot of people have tested (and validated) this approach in the meantime. At initial proposal I didn’t know if this would be the right way forward or not, but it’s starting to look like it is 🙂.
Happy for anyone to open a PR to add this as an opt-in flag, provided they add some tests as well!
16 remaining items
drwpow commentedon Aug 17, 2024
@dan-cooke you’re welcome to just make your own fork and start another PR! I believe you can start where the original author left off with the GitHub CLI
dan-cooke commentedon Aug 17, 2024
@drwpow oh duh my bad, I was trying to reopen the original authors fork PR so obviously that isn’t going to work
I’ll get started soon
Thanks!
sahandsn commentedon Oct 20, 2024
really interested to have this feature!
drwpow commentedon Jan 25, 2025
Minor update: The attached PR (#1863) gets it as close as ever, and this has been a long-requested feature. Will have to run this by the other maintainers but it’s a candidate for 8.x since it would be a breaking change. Tracking it in the project for further review.
feat(openapi-typescript): add readOnly/writeOnly support via markers
feat(openapi-typescript): add readOnly/writeOnly support via markers
feat(openapi-typescript): add readOnly/writeOnly support via markers
feat(openapi-typescript): add readOnly/writeOnly support via markers
feat(openapi-typescript): add readOnly/writeOnly support via markers
feat(openapi-typescript): add readOnly/writeOnly support via markers
abumalick commentedon Dec 24, 2025
This issue is fixed in #2549
Please show your interest in the PR to give some motivation to the maintainers to review this quickly 🚀
feat(openapi-typescript): add readOnly/writeOnly support via markers
feat(openapi-typescript): add readOnly/writeOnly support via markers (o…