astahmer / openapi-zod-client

Generate a zodios (typescript http client with zod validation) from an OpenAPI spec (json/yaml)
openapi-zod-client.vercel.app
817 stars 89 forks source link

Tag grouping common schema import issue #309

Open prastoin opened 1 month ago

prastoin commented 1 month ago

Introduction

It seems like there's an issue while importing common models, shared between at least 2 controllers, when tag grouping apis.

For more information regarding the bug itself please refer to https://github.com/astahmer/openapi-zod-client/issues/310

The suggested solution might not be the most idiomatic to the current implementation. Maybe this could be computed along others metrics and not afterwards ? Anw do not hesitate

A solution suggestion

Previously when a zod expression was match it would early return dependency injection. But zod expression might involve schemas such as with:

z.array(z.union([FooBar.and(Bar).and(Foo), z.union([FooBar, Bar, Foo])]))

I agree that using a ref or a json response would "fix" this here, as it would be abstracted behind a "on head" schema. But it's avoidable when dealing with use case such as response body unions and so on. In this way we will iterate and try match in the zod expression each of the endpoints's zodSchemaByName

Result on an example output:

// controller_bar.ts
import { makeApi, Zodios, type ZodiosOptions } from "@zodios/core";
import { z } from "zod";

+ import { FooBar } from "./common";

const endpoints = makeApi([
  {
    method: "put",
    path: "/bar",
    description: \`Bar\`,
    requestFormat: "json",
    response: z.array(FooBar),
  },
]);

export const Controller_barApi = new Zodios(endpoints);

export function createApiClient(baseUrl: string, options?: ZodiosOptions) {
  return new Zodios(baseUrl, endpoints, options);
}

Conclusion

As always any suggestion are welcomed ! Please let know Have a great day !

ps: I might not have covered every existing use case in tests so feel free to add any

vercel[bot] commented 1 month ago

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
openapi-zod-client-rim4 ✅ Ready (Inspect) Visit Preview 💬 Add feedback Oct 14, 2024 1:48pm