OpenAPITools / OpenAPITools/openapi-generator
[BUG][TYPESCRIPT-FETCH] Enum not generated when schema name matches ModelField pattern in oneOf
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 26.8k
- Forks
- 7.7k
- PR merge metrics
- PR metrics pending
Description
Bug Report Checklist
- Have you provided a full/minimal spec to reproduce the issue?
- Have you validated the input using an OpenAPI validator?
- Have you tested with the latest master to confirm the issue still exists?
- Have you searched for related issues/PRs?
- What's the actual output vs expected output?
- [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description
When using the TypeScript-Fetch generator, enum schemas are not properly generated when:
- The enum schema name follows the pattern
{ModelName}{FieldName}(e.g.,BuggyOneOfExampleSourcefor modelBuggyOneOfExamplewith fieldsource) - The enum is referenced within a
oneOfconstruct
Instead of generating the enum constant, the generator creates a circular type reference, making the generated code unusable.
openapi-generator version
- @openapitools/openapi-generator-cli: 2.23.4
- openapi generator: 7.15.0
OpenAPI declaration file content or url
openapi: 3.0.3
info:
title: Example
version: 1.0.0
paths:
/broken-oneof:
get:
summary: Broken oneOf scenario - enum not generated
responses:
'200':
description: Successful response
content:
application/json:
schema:
$ref: '#/components/schemas/BuggyOneOfExample'
components:
schemas:
# BROKEN: This demonstrates the bug where enum is not generated
# when schema name matches ModelField pattern and is used in oneOf
BuggyOneOfExample:
type: object
properties:
source:
oneOf:
- $ref: '#/components/schemas/BuggyOneOfExampleSource'
- $ref: '#/components/schemas/EmptyValue'
# This enum will NOT be generated due to the bug (name matches ModelField pattern)
BuggyOneOfExampleSource:
type: string
enum:
- MANUAL
- SCANNER
- OTHER
# Empty value enum for oneOf scenarios
EmptyValue:
enum:
- ''
Generation Details
I've created a simple repository to replicate the issue https://github.com/MrMarble/openapi-typescript-generator-one-of
# Install dependencies
npm install
# Generate TypeScript client
npm run generate
Configuration (openapitools.json):
{
"$schema": "./node_modules/@openapitools/openapi-generator-cli/config.schema.json",
"spaces": 2,
"generator-cli": {
"version": "7.15.0",
"generators": {
"Example": {
"generatorName": "typescript-fetch",
"output": "src/generated",
"inputSpec": "openapi.yml",
"additionalProperties": {
"supportsES6": "true",
"withoutRuntimeChecks": true
}
}
}
}
}
Steps to reproduce
- Create an OpenAPI spec with a model that has a field using
oneOf - Reference an enum schema where the schema name follows the pattern
{ModelName}{FieldName} - Generate TypeScript code using the typescript-fetch generator
- Observe that the enum constant is not generated, only a circular type reference
Related issues/PRs
https://github.com/OpenAPITools/openapi-generator/issues/14763
Suggest a fix
I tried playing around with the templates and was unable to make it work, so maybe the issue is in the logic that parses the schemas?
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the typescript-fetch generator and reproduce the case using the linked repository and its npm run generate command. Trace how the oneOf schema references are parsed and compare the generated output with the expected enum constant; done means the enum is generated without a circular type reference.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java, openapi, typescript
- Domain
- api, tooling
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100