OpenAPITools / OpenAPITools/openapi-generator

[BUG][Typescript-fetch] Nested list with union types breaks modelPropertyNaming between Python backend and NodeJS frontend

Open
#19,384 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)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
Description

When generating

openapi-generator version

7.6.0.

I tried with the latest version (7.7.0), but that caused null problems during maps. That may be opened as a separate bug.

OpenAPI declaration file content or url

--> https://gist.github.com/cajmorgan/5e1f138562afa39701607c68b124a1a8

Unfortunately, I can't provide the complete OpenAPI declaration file, but here is the details for the problematic model. Maybe this information is not enough.

Generation Details

The openapi schema is generated with FastAPI, and I have validated it and all seems fine. The problem is most likely due to a bug in typescript-fetch.

The models are generated using npm:

    "generate-api": "npx openapi-generator-cli generate -i http://localhost:3001/openapi.json  -g typescript-fetch -o api",

Steps to reproduce

I've seen multiple PRs with similar errors, but nothing exactly like this, I can't provide all details as the code is not open source.

Briefly:

  1. run the generate-api command, all generates fine
  2. Fetch the api where the problematic model resides
  3. from snake_case -> camelCase conversion is ignore on the union[NestedModel1, NestedModel2] in the nested list, therefore the .some_attribute is accessible but the typed .someAttribute is not.
  4. When a request is made to the server again, with the same problematic model in the requestParams, it doesn't get converted back to snake_case, so the backend receives the model with camelCase attributes, which then produces an error.
Related issues/PRs

https://github.com/OpenAPITools/openapi-generator/issues/2976

Suggest a fix

There is something going on with the Union[] python type and typescript-fetch that produces these ambiguous behaviors. I'd suggest to handle Union types differently so that they don't get compressed into the same model, which currently happens. List[List[Union[Model1, Model2]]] -> ModelStrangeNamingInnerInner, produces one single model in the frontend. Something here then breaks the snake_case -> camelCase -> snake_case automatic conversion

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 with the linked gist and reproduce using the typescript-fetch generator command shown in the issue. Inspect the generated nested-list union model and its request/response naming conversions; done means snake_case to camelCase and back works for the affected model without collapsing the union incorrectly.

Written by the indexing model from the issue text.

Assessment

Tech stack
fastapi, openapi, typescript
Domain
api, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.