hey-api / openapi-ts

🚀 The OpenAPI to TypeScript codegen. Generate clients, SDKs, validators, and more. Support: @mrlubos
https://heyapi.dev
Other
1.43k stars 106 forks source link

Duplicate remote `$ref` paths not resolved correctly #1135

Open SimonLudwigNeteleven opened 1 month ago

SimonLudwigNeteleven commented 1 month ago

Description

The command "yarn generate" should create all generated files including "types.gen.ts" without any issues. The files are generated, but the response type of “B” is broken after the command finishes. This only happens, if both path references in the yaml file are the same for both paths. The first correct one wins, if both are the same.

This behavior breaks the type generation and I could only work around it, by making one path beginning with “http” instead of “https” in my project. This workaround is obviously more hack then a solution and works only for two paths maximum. You can’t test the hack solution with “http” in stackblitz, because only “https” is allowed for the ref path, but you can reproduce the error.

Steps to reproduce the behavior

  1. Go to my stackblitz example
  2. open the terminal
  3. enter “yarn”
  4. enter “yarn generate”
  5. inspect the generated files (types.gen.ts) in packages/test-api/navigation-api/src-gen

Expected behavior 
AResponse and BResponse should be “(unknown)”.

Reproducible example or configuration

https://stackblitz.com/edit/vitejs-vite-6tvzvt?file=packages%2Ftest-api%2Fopenapi-spec%2Fnavigation-api.yaml

OpenAPI specification (optional)

No response

System information (optional)

No response

stackblitz[bot] commented 1 month ago

Fix this issue in StackBlitz Codeflow Start a new pull request in StackBlitz Codeflow.

mrlubos commented 1 month ago

Nice catch, thanks for reporting @SimonLudwigNeteleven!

mrlubos commented 2 weeks ago

@SimonLudwigNeteleven quick update, I found that this is most likely an issue with the underlying dependency https://github.com/APIDevTools/json-schema-ref-parser. Maybe there's a configuration for this, but a quick search didn't return anything.

For context, this is what happens internally. The first reference gets correctly bundled, for example:

{
  "type": "object",
  "properties": { "foo": { "enum": [], "type": "string" } }
}

while the second one produces this faulty reference.

{
  "$ref": "#/paths/~1a/get/responses/default/content/application~1json/schema"
}

I do plan to vertically integrate this tool eventually. Most people don't compose their schemas like this so it has not been reported before. Could you provide more context about your setup? How would you expect the remote reference to be resolved? Inline them or should they also create a reusable component and reference that?