Skip to content

Commit 5e054db

Browse files
authored
Support JSONSchema $defs (#1317)
* Support JSONSchema $defs * Add JSONSchema $defs docs * Add robots.txt * Docs update
1 parent c5b6ed8 commit 5e054db

26 files changed

+866
-265
lines changed

.changeset/config.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"$schema": "https://unpkg.com/@changesets/[email protected]/schema.json",
33
"changelog": ["@changesets/changelog-github", { "repo": "drwpow/openapi-typescript" }],
4-
"ignore": ["openapi-typescript-docs"],
4+
"ignore": ["openapi-typescript-docs", "@example/*"],
55
"commit": false,
66
"fixed": [],
77
"linked": [],

.changeset/kind-ladybugs-kneel.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"openapi-typescript": patch
3+
---
4+
5+
Fix JSONSchema $defs

.changeset/sour-ants-walk.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"openapi-typescript": patch
3+
---
4+
5+
Improve remote $ref parsing

docs/public/robots.txt

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
User-agent: *
2+
Allow: /

docs/src/content/docs/advanced.md

+67
Original file line numberDiff line numberDiff line change
@@ -351,3 +351,70 @@ prefixItems:
351351
352352
</tbody>
353353
</table>
354+
355+
### Use `$defs` only in objects
356+
357+
<a href="https://json-schema.org/understanding-json-schema/structuring.html#defs" target="_blank" rel="noopener noreferrer">JSONSchema $defs</a> can be used to provide sub-schema definitions anywhere. However, these won’t always convert cleanly to TypeScript. For example, this works:
358+
359+
```yaml
360+
components:
361+
schemas:
362+
DefType:
363+
type: object # ✅ `type: "object"` is OK to define $defs on
364+
$defs:
365+
myDefType:
366+
type: string
367+
MyType:
368+
type: object
369+
properties:
370+
myType:
371+
$ref: "#/components/schemas/DefType/$defs/myDefType"
372+
```
373+
374+
This will transform into the following TypeScript:
375+
376+
```ts
377+
export interface components {
378+
schemas: {
379+
DefType: {
380+
$defs: {
381+
myDefType: string;
382+
};
383+
};
384+
MyType: {
385+
myType?: components["schemas"]["DefType"]["$defs"]["myDefType"]; // ✅ Works
386+
};
387+
};
388+
}
389+
```
390+
391+
However, this won’t:
392+
393+
```yaml
394+
components:
395+
schemas:
396+
DefType:
397+
type: string # ❌ this wont keep its $defs
398+
$defs:
399+
myDefType:
400+
type: string
401+
MyType:
402+
properties:
403+
myType:
404+
$ref: "#/components/schemas/DefType/$defs/myDefType"
405+
```
406+
407+
Because it will transform into:
408+
409+
```ts
410+
export interface components {
411+
schemas: {
412+
DefType: string;
413+
MyType: {
414+
myType?: components["schemas"]["DefType"]["$defs"]["myDefType"]; // ❌ Property '$defs' does not exist on type 'String'.
415+
};
416+
};
417+
}
418+
```
419+
420+
So be wary about where you define `$defs` as they may go missing in your final generated types. When in doubt, you can always define `$defs` at the root schema level.

packages/openapi-fetch/examples/nextjs/lib/api/v1.d.ts

+2
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ export interface components {
9898
pathItems: never;
9999
}
100100

101+
export type $defs = Record<string, never>;
102+
101103
export type external = Record<string, never>;
102104

103105
export interface operations {

packages/openapi-fetch/examples/react-query/src/lib/api/v1.d.ts

+2
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ export interface components {
9898
pathItems: never;
9999
}
100100

101+
export type $defs = Record<string, never>;
102+
101103
export type external = Record<string, never>;
102104

103105
export interface operations {

packages/openapi-fetch/examples/sveltekit/src/lib/api/v1.d.ts

+2
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ export interface components {
9898
pathItems: never;
9999
}
100100

101+
export type $defs = Record<string, never>;
102+
101103
export type external = Record<string, never>;
102104

103105
export interface operations {

packages/openapi-fetch/test/v1.d.ts

+2
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,8 @@ export interface components {
422422
pathItems: never;
423423
}
424424

425+
export type $defs = Record<string, never>;
426+
425427
export type external = Record<string, never>;
426428

427429
export interface operations {

0 commit comments

Comments
 (0)