-
Notifications
You must be signed in to change notification settings - Fork 391
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Intersection schema generation is order-dependent (#603)
* Add new test cases. * Intersection schema generation is order-dependent - Given a schema that contains a named definition (`B`), - And that named definition is referenced in multiple locations, - And that named schema is also an intersection type (`allOf` in this example), Then when parsed, the generated TypeScript will contain the correct reference only for the _first_ location in which the named schema is encountered, during a depth-first traversal. Subsequent references to the same schema will be generated as though they were only the intersection type, and not the named schema. Example Given the following schema: ```yaml $id: Intersection type: object oneOf: - $ref: '#/definitions/A' - $ref: '#/definitions/B' definitions: A: type: object additionalProperties: false allOf: [$ref: '#/definitions/Base'] properties: b: {$ref: '#/definitions/B'} B: type: object additionalProperties: false, allOf: [$ref: '#/definitions/Base'] properties: x: {type: string} Base: type: object additionalProperties: false, properties: y: {type: string} ``` The current resulting TypeScript will be (comments adjusted for clarity): ```ts // Incorrect: should be `type Intersection = A | B` // Note that the B type at this location is the _second_ reference to // B during a depth-first traversal. export type Intersection = A | B1; export type A = A1 & { b?: B; }; export type A1 = Base; export type B = B1 & { x?: string; }; export type B1 = Base; export interface Base { y?: string; } ``` Root Cause In `parser.ts`, [lines 57 - 75][1], when schema that matches multiple "types" is encountered, the parser generates a new `ALL_OF` intersection schema to contain each sub-type, then adds each sub-type to the new `ALL_OF` schema. Each sub-type is then parsed sequentially. During this process, `maybeStripNameHints` is called, which mutates the schema by removing the `$id`, `description`, and `name` properties. Notably, these properties are used by `typesOfSchema` to detect the `NAMED_SCHEMA` type. As a result, this schema object will never again be detected as a `NAMED_SCHEMA` type. Therefore, the _first_ instance of the schema object is correctly handled as an intersection schema **and** a named schema, but all subsequent instances are treated as though they are **only** an intersection schema. Proposed Solution - The call to `typesOfSchema` is moved from `parser.ts` to `normalizer.ts`, with the goal of avoiding confusion due to a mutated schema object. The resulting list of schema types is persisted on the schema using a newly-introduced `Types` symbol. - The generated intersection schema is _also_ moved from `parser.ts` to `normalizer.ts`. This is because it is advantageous to let the generated intersection schema participate in the caching mechanism (which it could not previously do, since it was generated dynamically during each encounter). Without this, multiple instances of the same schema are generated. Related Issues - #597 [1]: https://github.com/bcherny/json-schema-to-typescript/blob/31993def993b610ba238d3024260129e31ddc371/src/parser.ts#L57-L75 'parser.ts, lines 57 - 75' * Additionally hoist `allOf` behavior. * Traverse the generated intersection schema.
- Loading branch information
Showing
12 changed files
with
977 additions
and
790 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import type {LinkedJSONSchema} from './types/JSONSchema' | ||
import {Intersection, Parent, Types} from './types/JSONSchema' | ||
import {typesOfSchema} from './typesOfSchema' | ||
|
||
export function applySchemaTyping(schema: LinkedJSONSchema) { | ||
const types = typesOfSchema(schema) | ||
|
||
Object.defineProperty(schema, Types, { | ||
enumerable: false, | ||
value: types, | ||
writable: false, | ||
}) | ||
|
||
if (types.size === 1) { | ||
return | ||
} | ||
|
||
// Some schemas can be understood as multiple possible types (see related | ||
// comment in `typesOfSchema.ts`). In such cases, we generate an `ALL_OF` | ||
// intersection that will ultimately be used to generate a union type. | ||
// | ||
// The original schema's name, title, and description are hoisted to the | ||
// new intersection schema to prevent duplication. | ||
// | ||
// If the original schema also contained its own `ALL_OF` property, it is | ||
// also hoiested to the new intersection schema. | ||
const intersection = { | ||
[Parent]: schema, | ||
[Types]: new Set(['ALL_OF']), | ||
$id: schema.$id, | ||
description: schema.description, | ||
name: schema.name, | ||
title: schema.title, | ||
allOf: schema.allOf ?? [], | ||
required: [], | ||
additionalProperties: false, | ||
} | ||
|
||
types.delete('ALL_OF') | ||
delete schema.allOf | ||
delete schema.$id | ||
delete schema.description | ||
delete schema.name | ||
delete schema.title | ||
|
||
Object.defineProperty(schema, Intersection, { | ||
enumerable: false, | ||
value: intersection, | ||
writable: false, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.