Skip to content
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

A & (B | C) would produce only A in the schema #62

Closed
kanitw opened this issue Jan 25, 2019 · 5 comments
Closed

A & (B | C) would produce only A in the schema #62

kanitw opened this issue Jan 25, 2019 · 5 comments

Comments

@kanitw
Copy link
Member

kanitw commented Jan 25, 2019

From vega/vega-lite#4440 (comment):

export type TopLevelFacetedUnitSpec = TopLevel<FacetedExtendedUnitSpec> & DataMixins;`

TopLevel<S extends BaseSpec> = S &
  TopLevelProperties & {
...
}

export type FacetedExtendedUnitSpec = ExtendedUnitSpec<FacetMapping<Field>>;

export type ExtendedUnitSpec<
  /** Extra Encoding */
  EE = EmptyObject
> = NormalizedUnitSpec<EE> | CompositeUnitSpec<EE>;
@kanitw kanitw changed the title Can't apply TopLevel with | Can't apply TopLevel with | in Vega-Lite Jan 25, 2019
@kanitw
Copy link
Member Author

kanitw commented Jan 25, 2019

It seems like TopLevelFacetedUnitSpec currently only gets things from TopLevel, but nothing from FacetedExtendedUnitSpec.

Perhaps, the NormalizedUnitSpec<EE> | CompositeUnitSpec<EE> part is something the schema isn't smart about.

@kanitw
Copy link
Member Author

kanitw commented Jan 25, 2019

If I have to guess, the generator doesn't use allOf, but rather create a more complex object, but isn't good at converting A & (B | C) into (A & B) | (A & C) yet.

@kanitw kanitw changed the title Can't apply TopLevel with | in Vega-Lite A & (B | C) would produce only A in the schema Jan 26, 2019
@kanitw
Copy link
Member Author

kanitw commented Jan 26, 2019

I think using allOf will reduce the JSON schema size in many places as well.

For example, in Vega-Lite, we have many things that share common parts such as:

  • Encoding and EncodingWithFacet
  • *Spec and TopLevel*Spec

Since we already reproduce merged schema for each of these pairs, the output schema can be quite huge.

@domoritz
Copy link
Member

domoritz commented Feb 10, 2019

export interface A {
    a: number;
}


export interface B {
    b: number;
}

export interface C {
    c: number;
}

export type MyType = A & (B | C);

Produces

{
  "$ref": "#/definitions/MyType",
  "$schema": "http://json-schema.org/draft-06/schema#",
  "definitions": {
    "C": {
      "additionalProperties": false,
      "properties": {
        "c": {
          "type": "number"
        }
      },
      "required": [
        "c"
      ],
      "type": "object"
    },
    "MyType": {
      "additionalProperties": false,
      "properties": {
        "a": {
          "type": "number"
        }
      },
      "required": [
        "a"
      ],
      "type": "object"
    }
  }

@domoritz
Copy link
Member

allOf is actually incorrect. The issue is that allOf requires that all definitions match. However, we make heavy use of additionalProperties: false, which means that none of the definitions match.

{
  "$ref": "#/definitions/BC",
  "$schema": "http://json-schema.org/draft-06/schema#",
  "definitions": {
    "B": {
      "additionalProperties": false,
      "properties": {
        "b": {
          "type": "number"
        },
        "x": {
          "type": "number"
        }
      },
      "type": "object"
    },
    "BC": {
      "allOf": [
        {
          "$ref": "#/definitions/B"
        },
        {
          "$ref": "#/definitions/C"
        }
      ]
    },
    "C": {
      "additionalProperties": false,
      "properties": {
        "c": {
          "type": "number"
        },
        "x": {
          "type": "number"
        }
      },
      "type": "object"
    }
  }
}

We actually discussed this issue back on 2017 at #4

joshkel added a commit to joshkel/ts-json-schema-generator that referenced this issue Oct 12, 2021
ts-json-schema-generator correctly handles basic types like `(A | B) & C`, but if A is itself an alias for `A1 | A2`, it fails.

Delete the FIXME comment and the reference to vega#62; if I understand correctly, this is correctly handled as of d9b9a15.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants