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

Multiple queries not handled correctly

AntoineThibi opened this issue · comments

I believe there is an issue when generating zod scheme for a multiple query link.

My openapi json is something like :

"QueryParamsDto": {
        "type": "object",
        "properties": {
          "endDate": { "type": "integer", "format": "int64" },
          "startDate": { "type": "integer", "format": "int64" },
          "label": { "type": "string" },
        }
      },


"/my/path/{id}": {
   "get": {
        "parameters": [
          { "name": "id", "in": "path", "required": true, "schema": { "type": "string" } },
          {
            "name": "params",
            "in": "query",
            "required": true,
            "schema": { "$ref": "#/components/schemas/QueryParamsDto" }
          }
        ],
}

When running generateZodClientFromOpenAPI I get a zod scheme :

const params = z
  .object({
    endDate: z.number().int(),
    startDate: z.number().int(),
    label: z.string(),
  })
  .partial();

{
    method: "get",
    path: "/accounts/secured/accounts/:id/movements",
    requestFormat: "json",
    parameters: [
      {
        name: "id",
        type: "Path",
        schema: z.string(),
      },
     {
       name: "params",
       type: "Query",
       schema: params
      },
}

Instead, I believe I should have the queries outside of the object, shouldn't I ?

{
    method: "get",
    path: "/accounts/secured/accounts/:id/movements",
    requestFormat: "json",
    parameters: [
      {
        name: "id",
        type: "Path",
        schema: z.string(),
      },
      {
        name: "endDate", 
        type: "Query", 
        schema: z.number().int().optional(), 
      },
      {
        name: "startDate", 
        type: "Query", 
        schema: z.number().int().optional(),
      },
      {
        name: "account",
        type: "Query",
        schema: z.string().optional(), 
      },
      {
        name: "label", 
        type: "Query", 
        schema: z.string().optional(),
      },
    ],
}

I'm willing to look to make a PR to fix it if it is indeed an issue.

hey, I'm no openapi expert but I don't think it should behave as you describe
I think your QueryParamsDto schema.properties should be spread into the parameters array instead

I might be wrong and I'm definitely open to having that behaviour if you find a relevant part of the openapi spec that matches what you described