quicktype icon indicating copy to clipboard operation
quicktype copied to clipboard

Duplicate types when using several schemas and references

Open Rosthouse opened this issue 3 years ago • 2 comments

I'm currently trying to generate interfaces for several schema files, that reference each other. By doing that, the parser duplicates some interfaces, which leads to dead interfaces in typescript.

I produced a quick example to demonstrate the problem:

a.json

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$id": "/schemas/a",
  "type": "object",
  "title": "A",
  "properties": {
    "name": {
      "type": "string"
    },
    "in": {
      "type": "array",
      "items": {
        "$ref": "/schemas/c"
      }
    }
  },
  "required": ["name", "in"]
}

b.json

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$id": "/schemas/b",
  "type": "object",
  "title": "B",
  "properties": {
    "name": {
      "type": "string"
    },
    "out": {
      "type": "array",
      "items": {
        "$ref": "/schemas/c"
      }
    }
  },
  "required": ["name", "out"]
}

c.json

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$id": "/schemas/c",
  "type": "object",
  "title": "C",
  "properties": {
    "name": {
      "type": "string"
    }
  },
  "required": ["name"]
}

Compiling these schemas with the following command:

quicktype --src-lang schema --out test.ts --just-types .\a.json .\b.json .\c.json

Results in the following typescript file:

export interface A {
    in:   InElement[];
    name: string;
}

export interface InElement {
    name: string;
}

export interface B {
    name: string;
    out:  InElement[];
}

export interface C {
    name: string;
}

I would have expected the following file:

export interface A {
    in:   C[];
    name: string;
}

export interface B {
    name: string;
    out:  C[];
}

export interface C {
    name: string;
}

Am I doing something wrong, or is there something else I'm forgetting?

Rosthouse avatar Nov 22 '21 15:11 Rosthouse

I'm seeing this issue when I'm specifying multiple URIs into the same JSON schema.

I expected anything that resolves to the same $id to be considered the same type and used globally. Rather, it seems to treat top-level references differently.

danielsharvey avatar Jan 15 '23 14:01 danielsharvey

Possibly related to #879

danielsharvey avatar Jan 15 '23 14:01 danielsharvey