Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generated zod schema causes ReferenceError in case of self-referencing property #717

Closed
TheSumm opened this issue Dec 13, 2023 · 1 comment · Fixed by #718 or #744
Closed

Generated zod schema causes ReferenceError in case of self-referencing property #717

TheSumm opened this issue Dec 13, 2023 · 1 comment · Fixed by #718 or #744
Labels
bug Something isn't working

Comments

@TheSumm
Copy link
Contributor

TheSumm commented Dec 13, 2023

What version of kubb is running?

kubb/2.0.1 linux-x64 node-v20.10.0

What platform is your computer?

Linux

What version of external packages are you using(@tanstack-query, MSW, React, Vue, ...)

No response

What steps can reproduce the bug?

Use @kubb/swagger-zod in combination with an OpenAPI schema that includes a component with self-referencing property (see example schema).

The schema that is generated by swagger-zod causes the following error:
ReferenceError: Cannot access 'exampleSchema' before initialization

Zod Schema:

export const exampleSchema = z.object({ nestedExamples: z.lazy(() => exampleSchema).schema.optional() })

Playground URL:
https://play.kubb.dev/?version=2.0.0&tanstack_version=4&msw_version=1&config=H4sIAAAAAAAAA12LMQ6EIBQF7%2FJq1N7KO2xpKDD%2BZY3IJ%2FDJZpdwd7Ux0W4ymSmIzIIeLRQWH%2FLBBcHI53RdIHkJR2p%2FZnOoCpzl3ljypw8u28Un9OOIYc3T1KWvsZYiVLmmt3GJqlaPpPnzDK3rDmTfWUCMAAAA&code=H4sIAAAAAAAAA51SsU7EMAzd%2BxVVYESXAtvtNzMwIoaQ89EcaWLFBoFO%2FXectFxLi25giez3%2FGznJaeqrlVECAad2tbqftNsbtVNRl04RIFOEkvGjj3kit2n6dBD%2FWhb6IwSti%2FlBOkDEknJU1EMOiHek8%2B6lhm3WvtojW8jsValoJfzuTRAwy1NAzUMg86IYK%2FAs1SABIQxENAvWIi7pllAAu6BbHLILoa80cNbueiswMbAEHilFMogemdN1uojlQbLmmzCYMpfnLDXCQ558JW2sZO9ZRTpQUJ69FWthH11KZ9nU%2FwT9WePh0ea5k5GjwvMfd6trM8%2F4AvLB4gvR7A8s05hkh%2BU2K2fIQAx7Md2S%2FYfjly8YdV%2FAypZNrbOAgAA

How often does this bug happen?

None

What is the expected behavior?

The zod schema can be used / imported without causing a reference error

Swagger/OpenAPI file?

{
  "openapi": "3.0.1",
  "info": {
    "title": "Example Schema"
  },
  "servers": [
    {
      "url": "http://localhost/"
    }
  ],
  "paths": {
    "/example": {
      "get": {
        "responses": {
          "200": {
            "description": "Ok",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Example"
                }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "Example": {
        "type": "object",
        "properties": {
          "nestedExamples": {
            "$ref": "#/components/schemas/Example"
          }
        }
      }
    }
  }
}

Additional information

It looks like the schema works as expected if .schema is removed e.g.

export const exampleSchema = z.object({ nestedExamples: z.lazy(() => exampleSchema).optional() })
@TheSumm
Copy link
Contributor Author

TheSumm commented Dec 17, 2023

I wanted to let you know that this issue still persists and I noticed that it also occurs when two schemas reference each other.
It works in the ZodGenerator test case due to the resolvePath mock.

I can offer to work on a fix for this issue. It would require to check for self references or circular dependencies between two or more schemas. Let me know if removing .schema from the zod schemas is an option you would consider or if you prefer another approach.

@stijnvanhulle stijnvanhulle reopened this Dec 22, 2023
@stijnvanhulle stijnvanhulle linked a pull request Dec 22, 2023 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
2 participants