OpenAPITools / OpenAPITools/openapi-generator

[BUG] Error generating TypeScript when request body includes discriminated union

Open
#20,879 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
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 (example)? (used Redocly struct linter)
  • 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

Spec with a discriminated union in a request produces error:

[main] ERROR o.o.codegen.DefaultCodegen - Error in generating example for the property SchemaForRequestBodyApplicationJson. Default to ERROR_TO_EXAMPLE_VALUE. Enable debugging for more info.

openapi-generator version

7.12.0

OpenAPI declaration file content or url

Link to minimum reproducible spec

Generation Details

I've put up a full reproduction at https://github.com/collinsauve/testing-openapi-generator-cli that includes the above linked spec and a few others that demonstrate a bit more of the problem space.

Without using that, the command is:

npx @openapitools/openapi-generator-cli generate -g typescript -i "spec.yaml" -o "output"
Steps to reproduce
  1. Clone https://github.com/collinsauve/testing-openapi-generator-cli
  2. Run bin/test test3-union-with-two-types
Related issues/PRs
Suggest a fix

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the bin/test test3-union-with-two-types reproduction from the linked repository, or invoke the TypeScript generator with the linked test3-union-with-two-types.yaml spec. Trace the DefaultCodegen example-generation error and compare the generated output with the expected handling of the discriminated union; done means generation completes without the reported error.

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
Clearly specified
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.