Skip to content

Adds option to disable union type de-duplication for performance reasons #424

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

Closed
wants to merge 3 commits into from
Closed
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ See [server demo](example) and [browser demo](https://github.com/bcherny/json-sc
| bannerComment | string | `"/* tslint:disable */\n/**\n* This file was automatically generated by json-schema-to-typescript.\n* DO NOT MODIFY IT BY HAND. Instead, modify the source JSONSchema file,\n* and run json-schema-to-typescript to regenerate this file.\n*/"` | Disclaimer comment prepended to the top of each generated file |
| cwd | string | `process.cwd()` | Root directory for resolving [`$ref`](https://tools.ietf.org/id/draft-pbryan-zyp-json-ref-03.html)s |
| declareExternallyReferenced | boolean | `true` | Declare external schemas referenced via `$ref`? |
| disableUnionDeduplication | boolean | `false` | Performance optimization to disable union type de-duplication. |
| enableConstEnums | boolean | `true` | Prepend enums with [`const`](https://www.typescriptlang.org/docs/handbook/enums.html#computed-and-constant-members)? |
| format | boolean | `true` | Format code? Set this to `false` to improve performance. |
| ignoreMinAndMaxItems | boolean | `false` | Ignore maxItems and minItems for `array` types, preventing tuples being generated. |
Expand Down
9 changes: 7 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ export interface Options {
* [$RefParser](https://github.com/BigstickCarpet/json-schema-ref-parser) Options, used when resolving `$ref`s
*/
$refOptions: $RefOptions
/**
* Performance optimization to disable union type de-duplication.
*/
disableUnionDeduplication: boolean
}

export const DEFAULT_OPTIONS: Options = {
Expand All @@ -90,7 +94,8 @@ export const DEFAULT_OPTIONS: Options = {
useTabs: false
},
unreachableDefinitions: false,
unknownAny: true
unknownAny: true,
disableUnionDeduplication: false,
}

export function compileFromFile(filename: string, options: Partial<Options> = DEFAULT_OPTIONS): Promise<string> {
Expand Down Expand Up @@ -157,7 +162,7 @@ export async function compile(schema: JSONSchema4, name: string, options: Partia
const parsed = parse(normalized, _options)
log('blue', 'parser', time(), '✅ Result:', parsed)

const optimized = optimize(parsed)
const optimized = optimize(parsed, _options)
if (process.env.VERBOSE) {
if (isDeepStrictEqual(parsed, optimized)) {
log('cyan', 'optimizer', time(), '✅ No change')
Expand Down
35 changes: 19 additions & 16 deletions src/optimizer.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import stringify = require('json-stringify-safe')
import {uniqBy} from 'lodash'
import {AST, T_ANY, T_UNKNOWN} from './types/AST'
import {Options} from './'
import {log} from './utils'

export function optimize(ast: AST, processed = new Set<AST>()): AST {
export function optimize(ast: AST, options: Options, processed = new Set<AST>()): AST {
log('cyan', 'optimizer', ast, processed.has(ast) ? '(FROM CACHE)' : '')

if (processed.has(ast)) {
Expand All @@ -15,7 +16,7 @@ export function optimize(ast: AST, processed = new Set<AST>()): AST {
switch (ast.type) {
case 'INTERFACE':
return Object.assign(ast, {
params: ast.params.map(_ => Object.assign(_, {ast: optimize(_.ast, processed)}))
params: ast.params.map(_ => Object.assign(_, {ast: optimize(_.ast, options, processed)}))
})
case 'INTERSECTION':
case 'UNION':
Expand All @@ -31,23 +32,25 @@ export function optimize(ast: AST, processed = new Set<AST>()): AST {
return T_UNKNOWN
}

// [A, B, B] -> [A, B]
const shouldTakeStandaloneNameIntoAccount = ast.params.filter(_ => _.standaloneName).length > 1
const params = uniqBy(
ast.params,
_ => `
${_.type}-
${shouldTakeStandaloneNameIntoAccount ? _.standaloneName : ''}-
${stringify((_ as any).params)}
`
)
if (params.length !== ast.params.length) {
log('cyan', 'optimizer', '[A, B, B] -> [A, B]', ast)
ast.params = params
if (!options.disableUnionDeduplication) {
// [A, B, B] -> [A, B]
const shouldTakeStandaloneNameIntoAccount = ast.params.filter(_ => _.standaloneName).length > 1
const params = uniqBy(
ast.params,
_ => `
${_.type}-
${shouldTakeStandaloneNameIntoAccount ? _.standaloneName : ''}-
${stringify((_ as any).params)}
`
)
if (params.length !== ast.params.length) {
log('cyan', 'optimizer', '[A, B, B] -> [A, B]', ast)
ast.params = params
}
}

return Object.assign(ast, {
params: ast.params.map(_ => optimize(_, processed))
params: ast.params.map(_ => optimize(_, options, processed))
})
default:
return ast
Expand Down
Loading