YousefED / YousefED/typescript-json-schema

uniqueNames setting crashes generation for interface with empty field

Open
#413 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
TypeScript
Stars
3.3k
Forks
332
PR merge metrics
No merged PRs in 30d

Description

System Information:

  • Node - v14.15.4
  • Typescript-json-schema version - 0.48.0
  • OS: macOS 10.15.7 / Darwin 19.6.0
  • Arch: x86_64

The file that I'm trying to parse is dead simple:

export interface RequestInterface {
    query: {
    }
}

The code that I'm trying to parse it with:

const typescript_json_schema_1 = require("typescript-json-schema");
const program = typescript_json_schema_1.getProgramFromFiles(['DeleteUser.ts'])
const generator = typescript_json_schema_1.buildGenerator(program, { required: true, uniqueNames: true });
const n = generator.getSymbols('RequestInterface')
generator.getSchemaForSymbol(n[0].name);

As you can see I'm just reading program, building generator and trying to get schema for my only interface. When launching, the app crashes with the following log:

Uncaught TypeError: Cannot read property 'length' of undefined
    at getCanonicalDeclaration (/Users/jbaramidze/playground/node_modules/typescript-json-schema/dist/typescript-json-schema.js:216:31)
    at getSourceFile (/Users/jbaramidze/playground/node_modules/typescript-json-schema/dist/typescript-json-schema.js:222:23)
    at JsonSchemaGenerator.getTypeDefinition (/Users/jbaramidze/playground/node_modules/typescript-json-schema/dist/typescript-json-schema.js:878:34)
    at JsonSchemaGenerator.getDefinitionForProperty (/Users/jbaramidze/playground/node_modules/typescript-json-schema/dist/typescript-json-schema.js:458:31)
    at /Users/jbaramidze/playground/node_modules/typescript-json-schema/dist/typescript-json-schema.js:752:37
    at Array.reduce (<anonymous>)
    at JsonSchemaGenerator.getClassDefinition (/Users/jbaramidze/playground/node_modules/typescript-json-schema/dist/typescript-json-schema.js:750:45)
    at JsonSchemaGenerator.getTypeDefinition (/Users/jbaramidze/playground/node_modules/typescript-json-schema/dist/typescript-json-schema.js:969:26)
    at JsonSchemaGenerator.getSchemaForSymbol (/Users/jbaramidze/playground/node_modules/typescript-json-schema/dist/typescript-json-schema.js:1000:24)

It works fine if I remove the uniqueNames: true setting. It also works fine if I add any member in query param.

P.S. It also crashes if I try to parse with CLI:

typescript-json-schema --uniqueNames DeleteUser.ts RequestInterface

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the crash with the shown RequestInterface and the uniqueNames option, then trace the reported path through dist/typescript-json-schema.js from getCanonicalDeclaration and getSourceFile into JsonSchemaGenerator.getTypeDefinition. Done means schema generation succeeds for an interface containing an empty query field, both through the API example and the --uniqueNames CLI invocation.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.