Skip to content

Support JSONSchema $defs #1317

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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/[email protected]/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 won’t 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
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