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

support for `nullable` as per openapi

WickyNilliams opened this issue · comments

seems that nullable is not supported right now.

consider the following schema:

paths:
  /pets:
    patch:
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/nulltype'
      responses:
        '200':
          description: Updated
components:
  schemas:
    nulltype:
      type: object
      nullable: true

this generates a zod schema like:

const nulltype = z.object({}).partial();

but i think it should be:

const nulltype = z.union([z.object({}).partial(), z.null()])

apparently openapi does not have an explicit null type, you instead mark a type as nullable.

happy to make a PR for this, just thought i'd create an issue first

const getZodChainablePresence = (schema: SchemaObject, meta?: CodeMetaData) => {
if (schema.nullable && !meta?.isRequired) {
return "nullish()";
}
if (schema.nullable) {
return "nullable()";
}
if (!meta?.isRequired) {
return "optional()";
}
return "";
};

it should be handled here
but I remember fixing something that looks like your issue here but wasn't sure if it was needed,

would this PR fix your issue and so should I update + merge it ?

Ah I didn't realise zod had nullable. In which case that would be the ideal! I'll take a look at that PR tomorrow and get back to you

excuse the delay! that looks correct to me. though i think it will clash with my recent fix for anyOf in the case where one of the types is nullable since it will output:

z.union([
  nulltype.passthrough(), // Property 'passthrough' does not exist on type 
  someOtherType.passthrough()
])

i guess it will need to check if the type is nullable before adding passthrough()?

i also notice nullable gets lost when used with anyOf on your previous PR: https://openapi-zod-client-git-fix-missing-zod-chains-astahmer.vercel.app/?doc=AQ4BwQwFwCwZwFwChSgPRgKZUS1pIoBjGZffAJ0wEcBXTOKAIQHsATATzPPyJYDsomQdx74IYMABsAlkWgyBaAFZwBosfjglMAWwgbN4-hwDyAM0NH8AWmAASKpeAByAMRo%2BusAOE402jB6EHBo-LRSUlAcWC541jx2jpjO7p4s3r6CoYHBoQAK2EwcAIIA5phxRlRwPvxwDFbkLgBMAAxtLk08bAxEFDJgUIr8CMAAqmBs0Jhs8cBedX645Ln6KzzhkdFY3SA7mGMsAEbKmERQ8%2BRbUhDHUofAUBT0V-i95hARUGM3byCFZilCp7J4xR4nM4Xf4ECgsLAUYaNYAw1AQEEohL7cFjGSCTAVChAA&prettier=EQbwOgdgBDVsAHATgSwgFwOooCboBbwBcUAjAEwAMANJLHMOgIYBG2ehwJALLdLPCZIkAewDuABSEBTCAGdiDJgBsxTAJ4LgfevACuc6QBVWWkgDMVhnQOCGAtikXoke6TZjw5aAObLpAIp6IujSipbK1nS2Lkwoyr4AwiL29kyK8NJyAKzwkAC%2BwEA

yeah that previous PR is outdated and wrong

i guess it will need to check if the type is nullable before adding passthrough()?

yup ! makes sense

are you able to make these changes ? i'll be a bit busy these next days

would that require rebasing your old PR on main? i tried to do that locally at least, but i ran into conflicts which i wasn't able to resolve. if you're able to bring that branch up to date, i can add the nullable check for sure (though i will likely have to branch off and make a fresh PR)

ehh I think it'll be easier to just start fresh and manually take the few changes that were in this PR, rebasing is a bit cursed after a few weeks/months 😅

haha no worries. i'll give it a crack tomorrow. cheers