Skip to content

Commit

Permalink
Support JSONSchema $defs (#1317)
Browse files Browse the repository at this point in the history
* Support JSONSchema $defs

* Add JSONSchema $defs docs

* Add robots.txt

* Docs update
  • Loading branch information
drwpow committed Aug 20, 2023
1 parent c5b6ed8 commit 5e054db
Show file tree
Hide file tree
Showing 26 changed files with 866 additions and 265 deletions.
2 changes: 1 addition & 1 deletion .changeset/config.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"$schema": "https://unpkg.com/@changesets/config@2.3.0/schema.json",
"changelog": ["@changesets/changelog-github", { "repo": "drwpow/openapi-typescript" }],
"ignore": ["openapi-typescript-docs"],
"ignore": ["openapi-typescript-docs", "@example/*"],
"commit": false,
"fixed": [],
"linked": [],
Expand Down
5 changes: 5 additions & 0 deletions .changeset/kind-ladybugs-kneel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"openapi-typescript": patch
---

Fix JSONSchema $defs
5 changes: 5 additions & 0 deletions .changeset/sour-ants-walk.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"openapi-typescript": patch
---

Improve remote $ref parsing
2 changes: 2 additions & 0 deletions docs/public/robots.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
User-agent: *
Allow: /
67 changes: 67 additions & 0 deletions docs/src/content/docs/advanced.md
Original file line number Diff line number Diff line change
Expand Up @@ -351,3 +351,70 @@ prefixItems:
</tbody>
</table>
### Use `$defs` only in objects
<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:
```yaml
components:
schemas:
DefType:
type: object # ✅ `type: "object"` is OK to define $defs on
$defs:
myDefType:
type: string
MyType:
type: object
properties:
myType:
$ref: "#/components/schemas/DefType/$defs/myDefType"
```
This will transform into the following TypeScript:
```ts
export interface components {
schemas: {
DefType: {
$defs: {
myDefType: string;
};
};
MyType: {
myType?: components["schemas"]["DefType"]["$defs"]["myDefType"]; // ✅ Works
};
};
}
```
However, this won’t:
```yaml
components:
schemas:
DefType:
type: string # ❌ this wont keep its $defs
$defs:
myDefType:
type: string
MyType:
properties:
myType:
$ref: "#/components/schemas/DefType/$defs/myDefType"
```
Because it will transform into:
```ts
export interface components {
schemas: {
DefType: string;
MyType: {
myType?: components["schemas"]["DefType"]["$defs"]["myDefType"]; // ❌ Property '$defs' does not exist on type 'String'.
};
};
}
```
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.
2 changes: 2 additions & 0 deletions packages/openapi-fetch/examples/nextjs/lib/api/v1.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ export interface components {
pathItems: never;
}

export type $defs = Record<string, never>;

export type external = Record<string, never>;

export interface operations {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ export interface components {
pathItems: never;
}

export type $defs = Record<string, never>;

export type external = Record<string, never>;

export interface operations {
Expand Down
2 changes: 2 additions & 0 deletions packages/openapi-fetch/examples/sveltekit/src/lib/api/v1.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ export interface components {
pathItems: never;
}

export type $defs = Record<string, never>;

export type external = Record<string, never>;

export interface operations {
Expand Down
2 changes: 2 additions & 0 deletions packages/openapi-fetch/test/v1.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,8 @@ export interface components {
pathItems: never;
}

export type $defs = Record<string, never>;

export type external = Record<string, never>;

export interface operations {
Expand Down
Loading

0 comments on commit 5e054db

Please sign in to comment.