astahmer / openapi-zod-client

Generate a zodios (typescript http client with zod validation) from an OpenAPI spec (json/yaml)

Home Page:openapi-zod-client.vercel.app

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Circular references with dereferenced doc

ArthurGoupil opened this issue · comments

Hello!

I have an issue with some specs using circular references.
With dereferenced docs, it leads to a RangeError: Maximum call stack size exceeded.

Without dereferencing docs, circular refs works but it looks like deep refs are not all resolved (TypeError: Cannot read properties of undefined (reading 'content'))

Are you aware of this?

Thanks a lot!

no I'm not aware of this
can you share your openapi or a minimal repro ?

Here is a simple repro:

My yaml specs:

openapi: 3.0.3

info:
  title: Test APIs
  version: 0.0.1

paths:
  /example:
    get:
      responses:
        "200":
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/NavigationItem"

components:
  schemas:
    NavigationItem:
      type: object
      properties:
        items:
          type: array
          items:
            $ref: "#/components/schemas/NavigationItem"

The way I use openapi-zod-client:

const dereferencedOpenApiDoc = await SwaggerParser.dereference("path-to-my-specs.yml")

await generateZodClientFromOpenAPI({
    openApiDoc: dereferencedOpenApiDoc,
    distPath: "some/path",
  })

can reproduce indeed. this looks more like a bug from SwaggerParser.dereference than from openapi-zod-client since that doesn't happen when directly pasting your code in the online playground, not sure what to do about it


(kinda) unrelated, but if you only/mostly care in having a typesafe API client you might be interested in https://typed-openapi-astahmer.vercel.app/

I'm feeling like you're using dereference because of issues with openapi-zod-client, that may not happen with typed-openapi ? feel free to try

It looks like doing

const dereferencedOpenApiDoc = await SwaggerParser.dereference(specPath, {
    dereference: { circular: 'ignore' },
  })

fixes the issue, and the zod schemas are well generated even for circular refs. 🎉

(kinda) unrelated, but if you only/mostly care in having a typesafe API client you might be interested in https://typed-openapi-astahmer.vercel.app/

This looks indeed really nice but we've already implemented our codegen with openapi-generator, and plugged openapi-zod-client on top of it.

If we run into more issues in the future, I'll have a deeper look.
Thanks for sharing anyway