ts-json-schema-generator icon indicating copy to clipboard operation
ts-json-schema-generator copied to clipboard

Mapped access of extended class does not work correctly

Open imjuni opened this issue 5 years ago • 2 comments

ts-json-schema-generator cannot schema from create extended class. See below,

Example source code

export class TypeA {
  public name: string;

  constructor() {
    this.name = 'hello';
  }
}

export class TypeB extends TypeA {
  public age: number;

  constructor() {
    super();

    this.age = 21;
  }
}

export interface InterfaceA {
  name: TypeB['name'];
}

expect

{
  $schema: 'http://json-schema.org/draft-07/schema#',
  type: 'object',
  properties: {
    name: {
        type: 'string'
    },
  },
  required: ['name'],
  definitions: {},
};

result

{
  $schema: 'http://json-schema.org/draft-07/schema#',
  type: 'object',
  properties: {
    name: {},
  },
  required: ['name'],
  definitions: {},
};

I use another cli tool that used option,

{
  path: 'TestAA.ts',
  tsconfig: '... valid path of tsconfig.json',
  type: 'InterfaceA',
  expose: 'none',
  jsDoc: 'extended',
  topRef: false,
  skipTypeCheck: false,
  extraTags: [ 'dateformatex', 'example', 'examples', 'collectionFormat' ],
  additionalProperties: true
}

I think that ts-json-schema-generator cannot resolve extended field. I resolve this problem change TypeB['name'] -> TypeA['name'], But I believe that TypeB['name'] correct syntax. Have any idea?

imjuni avatar Jan 12 '21 04:01 imjuni

Extend actually works. The problem is in the mapped access of an extended class.

yarn run run -p test.ts -t TypeB
yarn run v1.22.10
$ ts-node ts-json-schema-generator.ts -p test.ts -t TypeB
{
  "$ref": "#/definitions/TypeB",
  "$schema": "http://json-schema.org/draft-07/schema#",
  "definitions": {
    "TypeB": {
      "properties": {
        "age": {
          "type": "number"
        },
        "name": {
          "type": "string"
        }
      },
      "required": [
        "age",
        "name"
      ],
      "type": "object"
    }
  }
✨  Done in 4.16s.

domoritz avatar Jan 12 '21 09:01 domoritz

Any progress? Where are from this problem?

imjuni avatar Aug 24 '21 06:08 imjuni