Skip to content

$ref to array item leads to incorrect TypeScript type #308

@yekver

Description

@yekver

I noticed a bug with the OpenAPI code generation output where a property that references an array item schema is being typed as an array in the generated TypeScript.

After bundling with swagger-parser bundler (because the original schema used an relative YAML file reference), the final JSON OpenAPI schema looks like this:

"components": {
  "schemas": {
    "DogNames": {
      "type": "array",
      "items": {
        "type": "object",
        "properties": {
          "name": {
            "type": "string"
          }
        }
      }
    },
    "Dog": {
      "type": "object",
      "properties": {
        "dogName": {
          "$ref": "#/components/schemas/DogNames/items"
        }
      }
    }
  }
}

In the Dog schema, the property dogName references #/components/schemas/DogNames/items, which means it should be typed as a single object that matches the item structure from the DogNames array.

export type DogNames = {
  name?: string;
}[];

export type Dog = {
  dogName?: DogNames; // <-- This is incorrect
};

Expected output:

export type DogNames = {
  name?: string;
}[];

export type Dog = {
  dogName?: DogNames[number];
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions