Skip to content

Commit

Permalink
feat: set refHandling option to "import" by default (#164)
Browse files Browse the repository at this point in the history
  • Loading branch information
toomuchdesign authored Jan 28, 2024
1 parent 85027c1 commit 1f3294a
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/green-cows-sip.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'openapi-ts-json-schema': minor
---

default `refHandling`` option switched from "inline" to "import"
2 changes: 1 addition & 1 deletion .changeset/rude-oranges-worry.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
'openapi-ts-json-schema': minor
---

Throw descriptive error with circular schemas and inline refHandling option
Throw descriptive error with circular schemas and "inline" `refHandling`` option
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ const valid = ajv.validate(myGeneratedModelSchema, myModel);
| ---------------------------------------------- | ------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------ | ---------- |
| **openApiSchema** _(required)_ | `string` | Path to the OpenApi file (supports yaml and json). | - |
| **definitionPathsToGenerateFrom** _(required)_ | `string[]` | OpenApi definition object paths to generate the JSON schemas from. Only matching paths will be generated. (Supports dot notation: `["components.schemas"]`). | - |
| **refHandling** | `"inline" \| "import" \| "keep"` | `"inline"`: inline `$ref` schemas. <br/>`"import"`: generate and import `$ref` schemas.<br/>`"keep"`: keep `$ref` values. | `"inline"` |
| **refHandling** | `"import" \| "inline" \| "keep"` | `"import"`: generate and import `$ref` schemas.<br/>`"inline"`: inline `$ref` schemas.<br/>`"keep"`: keep `$ref` values. | `"import"` |
| **schemaPatcher** | `(params: { schema: JSONSchema }) => void` | Dynamically patch generated JSON schemas. The provided function will be invoked against every single JSON schema node. | - |
| **outputPath** | `string` | Path where the generated schemas will be saved. Defaults to `/schemas-autogenerated` in same directory as provided `openApiSchema`. | - |
| **plugins** | `ReturnType<Plugin>[]` | A set of optional plugin to generate any extra custom. See [plugins docs](./docs/plugins.md). output. | - |
Expand All @@ -76,8 +76,8 @@ Take a look at the [Developer's notes](./docs/developer-notes.md) for a few more

`openapi-ts-json-schema` provides 3 different strategies to handle OpenApi `$ref` properties configurable via the `refHandling` option:

- `inline`: `$ref` values get recursively replaced with inline copies of the target definition. This produces self-contained standalone schemas with usually repeated inline definitions
- `import`: `$ref` values get replaced with a local variable pointing to module of the generated target definition
- `inline`: `$ref` values get recursively replaced with inline copies of the target definition. This produces self-contained standalone schemas with usually repeated inline definitions
- `keep`: `$ref` values get preserved.

### Circular `$ref`s
Expand Down Expand Up @@ -135,6 +135,7 @@ Read [plugins documentation 📖](./docs/plugins.md).
- Consider adding a way to customize the values of the generated JSON schema ids. This could be beneficial even in case of multiple schemas being merged with plugins
- Find a way to merge multiple different OpenApi definitions consistently
- Improve external `#ref`s handling
- Consider implementing a strategy to stop circular reference recursion after a given amount of nesting

[ci-badge]: https://github.com/toomuchdesign/openapi-ts-json-schema/actions/workflows/ci.yml/badge.svg
[ci]: https://github.com/toomuchdesign/openapi-ts-json-schema/actions/workflows/ci.yml
Expand Down
2 changes: 1 addition & 1 deletion src/openapiToTsJsonSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export async function openapiToTsJsonSchema({
outputPath: providedOutputPath,
plugins = [],
silent,
refHandling = 'inline',
refHandling = 'import',
}: {
openApiSchema: string;
definitionPathsToGenerateFrom: string[];
Expand Down

0 comments on commit 1f3294a

Please sign in to comment.