typescript-json-schema icon indicating copy to clipboard operation
typescript-json-schema copied to clipboard

0.44.0 intersection types regression

Open laat opened this issue 5 years ago • 8 comments

types.ts given these types:

export interface Links {
    readonly self: HrefLink;
    readonly myLink: HrefLink & TemplatedLink;
}
export interface HrefLink {
  readonly href: string;
}
export interface TemplatedLink {
  readonly templated: true;
}

program.ts and a program that generates the schema:

import path from 'path';
import util from 'util'
import * as TJS from 'typescript-json-schema';

const program = TJS.getProgramFromFiles([path.join(__dirname, "types.ts")]);
const schema = TJS.generateSchema(program, "Links", {
  noExtraProps: true, // important
});

console.log(util.inspect(schema, { depth: Infinity }));

0.43.0 gives the expected intersection:

{
  type: 'object',
  properties: {
    self: { '$ref': '#/definitions/HrefLink' },
    myLink: {
      additionalProperties: false,
      type: 'object',
      properties: {
        href: { type: 'string' },
        templated: { type: 'boolean', enum: [ true ] }
      }
    }
  },
  additionalProperties: false,
  definitions: {
    HrefLink: {
      type: 'object',
      properties: { href: { type: 'string' } },
      additionalProperties: false
    }
  },
  '$schema': 'http://json-schema.org/draft-07/schema#'
}

0.44.0 HrefLink properties is missing in myLink:

{
  type: 'object',
  properties: {
    self: { '$ref': '#/definitions/HrefLink' },
    myLink: {
      additionalProperties: false,
      type: 'object',
      properties: { templated: { type: 'boolean', enum: [ true ] } }
    }
  },
  additionalProperties: false,
  definitions: {
    HrefLink: {
      type: 'object',
      properties: { href: { type: 'string' } },
      additionalProperties: false
    }
  },
  '$schema': 'http://json-schema.org/draft-07/schema#'
}

laat avatar Nov 25 '20 18:11 laat

Could that be coming from https://github.com/YousefED/typescript-json-schema/pull/383? cc @darkowic

domoritz avatar Nov 25 '20 19:11 domoritz

Not sure... But we do have a similar test here https://github.com/YousefED/typescript-json-schema/blob/master/test/programs/type-intersection/main.ts and everything is passing on CI.

darkowic avatar Nov 26 '20 07:11 darkowic

I bisected the commits, and it was introduced in b69cd3cf87107f4cac71af6dc50d85780506f8ac

laat avatar Nov 26 '20 09:11 laat

Can you improve the test scenarios and try to handle it? I did in this comment my best but if something was not covered then it is hard to be safe... Maybe I will have time at the weekend to look at it...

darkowic avatar Nov 26 '20 09:11 darkowic

It's very understandable that it happens when it's not covered by tests, this just happens from time to time. Nobody blames you :)

I can create a PR with a test-case that should pass, but I'm afraid that I'm not familiar enough with the guts of this package to fix it.

laat avatar Nov 26 '20 09:11 laat

@darkowic I have added a test that fails in #388.

I have verified that this test passed before b69cd3c.

I'm sorry that I cannot be of more help.

laat avatar Nov 26 '20 10:11 laat

Thank you for debugging, providing a test case, and helping to fix the issue both of you!

domoritz avatar Nov 26 '20 16:11 domoritz

Hey guys, do you know if there is any progress with this issue?

dmitry-urenev avatar Feb 01 '23 20:02 dmitry-urenev