Incorrect type generation when using a `$ref` with `discriminator` inside `allOf`
Author: EnergyCubeCreated Nov 28, 2023Updated Aug 27, 2026
Labelsbugopenapi-ts
Preamble
Thank you for your work on this project, keep up the good work!
Description
A wrong type is generated when using a $ref from schemas that contains a discriminator inside an allOf.
An invalid Omit occurs and removes the discriminator to force a "json" value string.
| Name | Version |
|---|---|
openapi-typescript |
6.7.1 |
| Node.js | 18.18.2 |
| OS + version | Windows 11 |
Reproduction
This following OpenAPI json will produce the following invalid type:
content: {
"application/json": {
my_type: "json";
} & Omit<components["schemas"]["allData"], "my_type">;
};{
"openapi": "3.0.0",
"info": {
"version": "1.0.0",
"title": "openapi-typescript bug reproduction",
"license": {
"name": "DWTFYW",
"url": "https://en.wikipedia.org/wiki/WTFPL"
}
},
"servers": [
{
"url": "n/a"
}
],
"paths": {
"/performRequest": {
"post": {
"security": [],
"operationId": "performRequest",
"summary": "POST /performRequest",
"parameters": [],
"responses": {
"200": {
"description": "POST /performRequest",
"content": {
"application/json": {
"schema": {
"title": "performRequest Response",
"allOf": [
{
"$ref": "#/components/schemas/allData"
}
]
}
}
}
},
"400": {
"description": "Bad Request"
}
}
}
}
},
"components": {
"schemas": {
"allData": {
"type": "object",
"discriminator": {
"propertyName": "my_type"
},
"required": ["my_type"],
"oneOf": [
{ "$ref": "#/components/schemas/data1" },
{ "$ref": "#/components/schemas/data2" }
]
},
"data1": {
"type": "object",
"properties": {
"my_type": {
"type": "string",
"enum": ["data_type_ex_1"]
}
},
"required": ["my_type"]
},
"data2": {
"type": "object",
"properties": {
"my_type": {
"type": "string",
"enum": ["data_type_ex_2"]
}
},
"required": ["my_type"]
}
}
}
}Expected result
content: {
"application/json": components["schemas"]["allData"];
};Remark
Taking the content of the $ref and putting it in the allOf works correctly, but the problem seems to be specific to the use of a $ref in a allOf.
The bug seems to occur from 6.6.1.
Checklist
- My OpenAPI schema passes the Redocly validator (
npx @redocly/cli@latest lint) - I’m willing to open a PR (see CONTRIBUTING.md)
Source: openapi-ts/openapi-typescript