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

"allOf" + "additionalProperties false" with interface extensions #4

Closed
arvind opened this issue Jun 11, 2017 · 2 comments
Closed

"allOf" + "additionalProperties false" with interface extensions #4

arvind opened this issue Jun 11, 2017 · 2 comments
Labels

Comments

@arvind
Copy link
Member

arvind commented Jun 11, 2017

export interface A {a: string;}
export interface B extends A {b?: string;}

generates

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "definitions": {
    "B": {
      "allOf": [
        {
          "type": "object",
          "properties": {
            "b": {
              "type": "string"
            }
          },
          "required": [],
          "additionalProperties": false
        },
        {
          "$ref": "#/definitions/A"
        }
      ]
    },
    "A": {
      "type": "object",
      "properties": {
        "a": {
          "type": "string"
        }
      },
      "required": [
        "a"
      ],
      "additionalProperties": false
    }
  },
  "$ref": "#/definitions/B"
}

The following object type checks correctly with TypeScript. However, it does not validate against the JSON schema. This is because B uses an allOf where each branch has additionalProperties: false (i.e., it fails the first branch).

const foobar:B = {a: 'foo'};

I believe ts2json should be consolidating these definitions together. Note: Switching allOf to anyOf doesn't yield a correct schema either ({b: 'foo'} correctly validates for instance).

@arvind arvind added the bug label Jun 11, 2017
@arvind
Copy link
Member Author

arvind commented Jun 11, 2017

This issue also occurs with intersection types. An identical schema is generated with:

export type A = {a: string}
export type B = A & {b?: string}

@domoritz
Copy link
Member

The output above is from the version on NPM.

{
  "$ref": "#/definitions/MyAlias",
  "$schema": "http://json-schema.org/draft-04/schema#",
  "definitions": {
    "A": {
      "additionalProperties": false,
      "properties": {
        "a": {
          "type": "string"
        }
      },
      "required": [
        "a"
      ],
      "type": "object"
    },
    "B": {
      "additionalProperties": false,
      "properties": {
        "a": {
          "type": "string"
        },
        "b": {
          "type": "string"
        }
      },
      "required": [
        "a"
      ],
      "type": "object"
    },
    "MyAlias": {
      "anyOf": [
        {
          "$ref": "#/definitions/A"
        },
        {
          "$ref": "#/definitions/B"
        }
      ]
    }
  }
}

Use npm install vega/typescript-to-json-schema#VERSION.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants