Skip to content

Incorrect typescript schema when using anyOf with properties #2380

@joundy

Description

@joundy

openapi-typescript version

7.8.0

Node.js version

24.3.0

OS + version

Fedora release 39

Description

When generating from specs that use "anyOf" with properties, I'm getting an incorrect schema. The object properties are included inside the union type, which is not the expected result because the object is in the outside of the anyOf schemas

Contact: {
            contact_method: {
                name?: string;
            } | components["schemas"]["Email"] | components["schemas"]["Phone"];
};

Is this intentional behavior or is it a bug? It seems weird.

Reproduction

Here's my sample openapi spec

{
  "openapi": "3.0.0",
  "servers": [
    {
      "url": "https://api.contact-service.com"
    }
  ],
  "info": {
    "title": "Contact API",
    "version": "1.0.0",
    "description": "A simple API for contact information",
    "license": {
      "name": "MIT",
      "url": "https://opensource.org/licenses/MIT"
    }
  },
  "security": [
    {
      "apiKey": []
    }
  ],
  "paths": {
    "/contact": {
      "post": {
        "operationId": "addContact",
        "summary": "Add a contact method",
        "security": [
          {
            "apiKey": []
          }
        ],
        "requestBody": {
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/Contact"
              }
            }
          }
        },
        "responses": {
          "201": {
            "description": "Contact added successfully"
          },
          "400": {
            "description": "Bad request - invalid input"
          },
          "401": {
            "description": "Unauthorized - invalid or missing API key"
          }
        }
      }
    }
  },
  "components": {
    "securitySchemes": {
      "apiKey": {
        "type": "apiKey",
        "in": "header",
        "name": "X-API-Key"
      }
    },
    "schemas": {
      "Contact": {
        "type": "object",
        "required": [
          "name",
          "contact_method"
        ],
        "properties": {
          "contact_method": {
            "anyOf": [
              {
                "$ref": "#/components/schemas/Email"
              },
              {
                "$ref": "#/components/schemas/Phone"
              }
            ],
            "properties": {
              "name": {
                "type": "string"
              }
            }
          }
        }
      },
      "Email": {
        "type": "object",
        "required": [
          "type",
          "address"
        ],
        "properties": {
          "type": {
            "type": "string",
            "enum": [
              "email"
            ]
          },
          "address": {
            "type": "string",
            "format": "email"
          }
        }
      },
      "Phone": {
        "type": "object",
        "required": [
          "type",
          "number"
        ],
        "properties": {
          "type": {
            "type": "string",
            "enum": [
              "phone"
            ]
          },
          "number": {
            "type": "string"
          }
        }
      }
    }
  }
}

npx openapi-typescript anyof-example.json -o ./openapi.ts

Expected result

        Contact: {
            contact_method: {
                name?: string;
            } & (components["schemas"]["Email"] | components["schemas"]["Phone"]);
        };

Required

  • My OpenAPI schema is valid and passes the Redocly validator (npx @redocly/cli@latest lint)

Extra

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingopenapi-tsRelevant to the openapi-typescript library

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions