Skip to content

Commit 05c2762

Browse files
authored
Merge branch 'openapi-ts:main' into support-custom-path-serializer
2 parents ce1961e + 12f9c29 commit 05c2762

40 files changed

+5056
-2523
lines changed

.changeset/eleven-shoes-mate.md

Lines changed: 0 additions & 5 deletions
This file was deleted.

.changeset/modern-jars-camp.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"openapi-typescript": patch
3+
---
4+
5+
All kinds of enum related fixes (enums in arrays, in optional props, in unions, in request body, with record types...)

.changeset/nice-parts-poke.md

Lines changed: 0 additions & 5 deletions
This file was deleted.

.github/workflows/ci.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ jobs:
1515
runs-on: ubuntu-latest
1616
steps:
1717
- uses: actions/checkout@v5
18-
- uses: actions/setup-node@v4
18+
- uses: actions/setup-node@v6
1919
with:
2020
node-version: 22
2121
- uses: pnpm/action-setup@v4
@@ -29,7 +29,7 @@ jobs:
2929
node-version: [20, 22]
3030
steps:
3131
- uses: actions/checkout@v5
32-
- uses: actions/setup-node@v4
32+
- uses: actions/setup-node@v6
3333
with:
3434
node-version: ${{ matrix.node-version }}
3535
- uses: pnpm/action-setup@v4
@@ -40,7 +40,7 @@ jobs:
4040
runs-on: ubuntu-latest
4141
steps:
4242
- uses: actions/checkout@v5
43-
- uses: actions/setup-node@v4
43+
- uses: actions/setup-node@v6
4444
with:
4545
node-version: 22
4646
- uses: pnpm/action-setup@v4
@@ -52,7 +52,7 @@ jobs:
5252
runs-on: macos-latest
5353
steps:
5454
- uses: actions/checkout@v5
55-
- uses: actions/setup-node@v4
55+
- uses: actions/setup-node@v6
5656
with:
5757
node-version: 22
5858
- uses: pnpm/action-setup@v4
@@ -63,7 +63,7 @@ jobs:
6363
runs-on: windows-latest
6464
steps:
6565
- uses: actions/checkout@v5
66-
- uses: actions/setup-node@v4
66+
- uses: actions/setup-node@v6
6767
with:
6868
node-version: 22
6969
- uses: pnpm/action-setup@v4

.github/workflows/release.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ jobs:
2020
uses: actions/checkout@v5
2121

2222
- name: Node setup
23-
uses: actions/setup-node@v4
23+
uses: actions/setup-node@v6
2424
with:
2525
node-version: 22
2626

.github/workflows/size-limit.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ jobs:
1313
runs-on: ubuntu-latest
1414
steps:
1515
- uses: actions/checkout@v5
16-
- uses: actions/setup-node@v4
16+
- uses: actions/setup-node@v6
1717
with:
1818
node-version: 22
1919
- uses: pnpm/action-setup@v4

docs/node.md

Lines changed: 144 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,14 @@ const ast = await openapiTS(mySchema, { redocly });
8383

8484
The Node API supports all the [CLI flags](/cli#flags) in `camelCase` format, plus the following additional options:
8585

86-
| Name | Type | Default | Description |
87-
| :-------------- | :-------------: | :-------------: | :------------------------------------------------------------------------------------------- |
88-
| `transform` | `Function` | | Override the default Schema Object ➝ TypeScript transformer in certain scenarios |
89-
| `postTransform` | `Function` | | Same as `transform` but runs _after_ the TypeScript transformation |
90-
| `silent` | `boolean` | `false` | Silence warning messages (fatal errors will still show) |
91-
| `cwd` | `string \| URL` | `process.cwd()` | (optional) Provide the current working directory to help resolve remote `$ref`s (if needed). |
92-
| `inject` | `string` | | Inject arbitrary TypeScript types into the start of the file |
86+
| Name | Type | Default | Description |
87+
| :----------------- | :-------------: | :-------------: | :------------------------------------------------------------------------------------------- |
88+
| `transform` | `Function` | | Override the default Schema Object ➝ TypeScript transformer in certain scenarios |
89+
| `postTransform` | `Function` | | Same as `transform` but runs _after_ the TypeScript transformation |
90+
| `transformProperty`| `Function` | | Transform individual property signatures for Schema Object properties |
91+
| `silent` | `boolean` | `false` | Silence warning messages (fatal errors will still show) |
92+
| `cwd` | `string \| URL` | `process.cwd()` | (optional) Provide the current working directory to help resolve remote `$ref`s (if needed). |
93+
| `inject` | `string` | | Inject arbitrary TypeScript types into the start of the file |
9394

9495
### transform / postTransform
9596

@@ -254,4 +255,139 @@ file?: Blob | null; // [!code ++]
254255

255256
Any [Schema Object](https://spec.openapis.org/oas/latest.html#schema-object) present in your schema will be run through this formatter (even remote ones!). Also be sure to check the `metadata` parameter for additional context that may be helpful.
256257

257-
There are many other uses for this besides checking `format`. Because this must return a **string** you can produce any arbitrary TypeScript code you’d like (even your own custom types).
258+
There are many other uses for this besides checking `format`. Because this must return a **string** you can produce any arbitrary TypeScript code you'd like (even your own custom types).
259+
260+
### transformProperty
261+
262+
Use the `transformProperty()` option to modify individual property signatures within Schema Objects. This is particularly useful for adding JSDoc comments, validation annotations, or modifying property-level attributes that can't be achieved with `transform` or `postTransform`.
263+
264+
- `transformProperty()` runs **after** type conversion but **before** JSDoc comments are added
265+
- It receives the property signature, the original schema object, and transformation options
266+
- It should return a modified `PropertySignature` or `undefined` to leave the property unchanged
267+
268+
#### Example: JSDoc validation annotations
269+
270+
A common use case is adding validation annotations based on OpenAPI schema constraints:
271+
272+
::: code-group
273+
274+
```yaml [my-openapi-3-schema.yaml]
275+
components:
276+
schemas:
277+
User:
278+
type: object
279+
properties:
280+
name:
281+
type: string
282+
minLength: 1
283+
pattern: "^[a-zA-Z0-9]+$"
284+
email:
285+
type: string
286+
format: email
287+
age:
288+
type: integer
289+
minimum: 0
290+
maximum: 120
291+
required: [name, email]
292+
```
293+
294+
:::
295+
296+
::: code-group
297+
298+
```ts [src/my-project.ts]
299+
import fs from "node:fs";
300+
import ts from "typescript";
301+
import openapiTS, { astToString } from "openapi-typescript";
302+
303+
const ast = await openapiTS(mySchema, {
304+
transformProperty(property, schemaObject, options) {
305+
const validationTags: string[] = [];
306+
307+
// Add validation JSDoc tags based on schema constraints
308+
if (schemaObject.minLength !== undefined) {
309+
validationTags.push(`@minLength ${schemaObject.minLength}`);
310+
}
311+
if (schemaObject.maxLength !== undefined) {
312+
validationTags.push(`@maxLength ${schemaObject.maxLength}`);
313+
}
314+
if (schemaObject.minimum !== undefined) {
315+
validationTags.push(`@minimum ${schemaObject.minimum}`);
316+
}
317+
if (schemaObject.maximum !== undefined) {
318+
validationTags.push(`@maximum ${schemaObject.maximum}`);
319+
}
320+
if (schemaObject.pattern !== undefined) {
321+
validationTags.push(`@pattern ${schemaObject.pattern}`);
322+
}
323+
if (schemaObject.format !== undefined) {
324+
validationTags.push(`@format ${schemaObject.format}`);
325+
}
326+
327+
// If we have validation tags, add them as JSDoc comments
328+
if (validationTags.length > 0) {
329+
// Create a new property signature
330+
const newProperty = ts.factory.updatePropertySignature(
331+
property,
332+
property.modifiers,
333+
property.name,
334+
property.questionToken,
335+
property.type,
336+
);
337+
338+
// Add JSDoc comment
339+
const jsDocText = `*\n * ${validationTags.join('\n * ')}\n `;
340+
341+
ts.addSyntheticLeadingComment(
342+
newProperty,
343+
ts.SyntaxKind.MultiLineCommentTrivia,
344+
jsDocText,
345+
true,
346+
);
347+
348+
return newProperty;
349+
}
350+
351+
return property;
352+
},
353+
});
354+
355+
const contents = astToString(ast);
356+
fs.writeFileSync("./my-schema.ts", contents);
357+
```
358+
359+
:::
360+
361+
This transforms the schema into TypeScript with validation annotations:
362+
363+
::: code-group
364+
365+
```ts [my-schema.d.ts]
366+
export interface components {
367+
schemas: {
368+
User: {
369+
/**
370+
* @minLength 1
371+
* @pattern ^[a-zA-Z0-9]+$
372+
*/
373+
name: string;
374+
/**
375+
* @format email
376+
*/
377+
email: string;
378+
/**
379+
* @minimum 0
380+
* @maximum 120
381+
*/
382+
age?: number;
383+
};
384+
};
385+
}
386+
```
387+
388+
:::
389+
390+
The `transformProperty` function provides access to:
391+
- `property`: The TypeScript PropertySignature AST node
392+
- `schemaObject`: The original OpenAPI Schema Object for this property
393+
- `options`: Transformation context including path information and other utilities

docs/openapi-fetch/middleware-auth.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Each middleware can provide `onRequest()`, `onResponse()` and `onError` callback
1313
::: code-group
1414

1515
```ts [src/my-project.ts]
16-
import createClient from "openapi-fetch";
16+
import createClient, { Middleware } from "openapi-fetch";
1717
import type { paths } from "./my-openapi-3-schema"; // generated by openapi-typescript
1818

1919
const myMiddleware: Middleware = {

docs/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"update-contributors": "node scripts/update-contributors.js"
1010
},
1111
"devDependencies": {
12-
"@shikijs/vitepress-twoslash": "3.12.2",
12+
"@shikijs/vitepress-twoslash": "3.13.0",
1313
"openapi-metadata": "workspace:*",
1414
"vite": "catalog:",
1515
"vitepress": "1.6.4"

package.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"name": "Drew Powers",
88
"email": "[email protected]"
99
},
10-
"packageManager": "pnpm@10.16.1",
10+
"packageManager": "pnpm@10.18.3",
1111
"scripts": {
1212
"build": "turbo run build",
1313
"lint": "turbo run lint",
@@ -20,15 +20,15 @@
2020
},
2121
"devDependencies": {
2222
"@arethetypeswrong/cli": "0.18.2",
23-
"@biomejs/biome": "2.2.5",
23+
"@biomejs/biome": "2.2.6",
2424
"@changesets/changelog-github": "0.5.1",
2525
"@changesets/cli": "2.29.7",
26-
"@playwright/test": "1.55.1",
26+
"@playwright/test": "1.56.0",
2727
"@size-limit/preset-small-lib": "11.2.0",
28-
"@types/node": "22.18.3",
28+
"@types/node": "22.18.10",
2929
"prettier": "3.6.2",
3030
"size-limit": "11.2.0",
31-
"turbo": "2.5.6",
31+
"turbo": "2.5.8",
3232
"typescript": "catalog:",
3333
"unbuild": "3.6.1",
3434
"vitest": "3.2.4"

0 commit comments

Comments
 (0)