feat(openapi-typescript): add readOnly/writeOnly support via markers#2549
feat(openapi-typescript): add readOnly/writeOnly support via markers#2549abumalick wants to merge 3 commits intoopenapi-ts:mainfrom
Conversation
🦋 Changeset detectedLatest commit: 61d7b87 The changes in this PR will be included in the next version bump. This PR includes changesets to release 5 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
👷 Deploy request for openapi-ts pending review.Visit the deploys page to approve it
|
b3daedc to
b1d920a
Compare
There was a problem hiding this comment.
Pull request overview
This PR adds opt-in support for OpenAPI's readOnly and writeOnly property modifiers via a new --read-write-markers CLI flag. When enabled, the type generator wraps these properties with marker types ($Read<T> and $Write<T>), enabling compile-time enforcement of visibility rules in openapi-fetch.
Key Changes:
- Added
--read-write-markersflag to wrap readOnly properties with$Read<T>and writeOnly properties with$Write<T> - Generated inline helper types (
Readable<T>,Writable<T>) strip markers appropriately for request/response contexts - Updated openapi-fetch to use
Writable<T>for request bodies andReadable<T>for responses
Reviewed changes
Copilot reviewed 11 out of 14 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| packages/openapi-typescript/src/types.ts | Added readWriteMarkers option to OpenAPITSOptions and GlobalContext interfaces |
| packages/openapi-typescript/src/index.ts | Wired up readWriteMarkers option from user input to global context |
| packages/openapi-typescript/bin/cli.js | Added --read-write-markers CLI flag support |
| packages/openapi-typescript/src/transform/schema-object.ts | Implemented wrapWithReadWriteMarker function and integrated marker wrapping for properties and $defs |
| packages/openapi-typescript/src/transform/index.ts | Added inline generation of helper types ($Read, $Write, Readable, Writable) when flag is enabled |
| packages/openapi-typescript-helpers/src/index.ts | Added exportable helper types for marker definitions and transformation utilities |
| packages/openapi-fetch/src/index.d.ts | Applied Writable to request bodies and Readable to response data/errors |
| packages/openapi-typescript/test/transform/schema-object/object.test.ts | Added comprehensive test cases for readWriteMarkers option behavior |
| packages/openapi-typescript/test/test-helpers.ts | Added readWriteMarkers: false to default test context |
| packages/openapi-fetch/test/read-write-visibility/schemas/read-write.yaml | Test schema with readOnly and writeOnly properties |
| packages/openapi-fetch/test/read-write-visibility/schemas/read-write.d.ts | Generated types from test schema with markers enabled |
| packages/openapi-fetch/test/read-write-visibility/read-write.test.ts | Runtime and compile-time tests verifying marker behavior |
| docs/cli.md | Documentation for the new --read-write-markers flag with usage examples |
| .changeset/happy-pans-run.md | Changeset describing the new feature |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Add opt-in `--read-write-markers` flag that generates `$Read<T>` and `$Write<T>` marker types for readOnly/writeOnly OpenAPI properties. - Wrap readOnly properties with `$Read<T>` - Wrap writeOnly properties with `$Write<T>` - Generate inline helper types (`Readable`, `Writable`) in output - Update openapi-fetch to use `Writable<T>` for request bodies and `Readable<T>` for responses - Add `$Read`, `$Write`, `Readable`, `Writable` types to openapi-typescript-helpers This enables proper type-checking: - readOnly properties are excluded from request bodies - writeOnly properties are excluded from response types Closes openapi-ts#604
b1d920a to
3dd3cf1
Compare
|
mmh, sorry it is not fully ready yet, after more testing I get some bugs |
When Readable<$Read<U>> was resolved, it returned U directly instead of Readable<U>, causing nested $Read markers inside U to not be unwrapped. Same issue for Writable<$Write<U>>.
|
Here it is ready incha Allah. Please tell me if you need any changes or if you have any concern @htunnicliff @drwpow |
|
@htunnicliff @drwpow any feedback please? |
|
Ping for feedback @drwpow @kerwanp @gzm0 @htunnicliff This implements the Production validation: Running in 2 company projects with Key decisions needing review:
Happy to adjust based on feedback. Is this direction acceptable or should I take a different approach? |
|
@duncanbeevers or any other maintainer. Can I get a review on this? |
Summary
--read-write-markersflag that generates$Read<T>and$Write<T>marker types for readOnly/writeOnly OpenAPI properties$Read<T>, writeOnly with$Write<T>Readable,Writable) in outputWritable<T>for request bodies andReadable<T>for responsesThis enables proper type-checking:
Closes #604
Test plan
packages/openapi-fetch/test/read-write-visibility/