astahmer / typed-openapi

Generate a headless Typescript API client from an OpenAPI spec
https://typed-openapi-web.vercel.app/
203 stars 24 forks source link

Unsupported schema type: fhirprimitiveextension #18

Open trevorpfiz opened 1 year ago

trevorpfiz commented 1 year ago

The issue is coming from a FHIRPrimitiveExtension type. What are my options here? I am having the same problem with openapi-zod-client.

Reference: fhir-extensions

Here is the OpenAPI file: openapi.json

Error: Unsupported schema type: fhirprimitiveextension
    at getZodSchema (/home/spark/.local/share/pnpm/store/v3/tmp/dlx-27449/node_modules/.pnpm/openapi-zod-client@1.13.1/node_modules/openapi-zod-client/dist/generateZodClientFromOpenAPI-3ae8a23b.cjs.dev.js:1357:9)
    at /home/spark/.local/share/pnpm/store/v3/tmp/dlx-27449/node_modules/.pnpm/openapi-zod-client@1.13.1/node_modules/openapi-zod-client/dist/generateZodClientFromOpenAPI-3ae8a23b.cjs.dev.js:1332:24
    at Array.map (<anonymous>)
    at getZodSchema (/home/spark/.local/share/pnpm/store/v3/tmp/dlx-27449/node_modules/.pnpm/openapi-zod-client@1.13.1/node_modules/openapi-zod-client/dist/generateZodClientFromOpenAPI-3ae8a23b.cjs.dev.js:1316:56)
    at getZodSchema (/home/spark/.local/share/pnpm/store/v3/tmp/dlx-27449/node_modules/.pnpm/openapi-zod-client@1.13.1/node_modules/openapi-zod-client/dist/generateZodClientFromOpenAPI-3ae8a23b.cjs.dev.js:1117:16)
    at getZodiosEndpointDefinitionList (/home/spark/.local/share/pnpm/store/v3/tmp/dlx-27449/node_modules/.pnpm/openapi-zod-client@1.13.1/node_modules/openapi-zod-client/dist/generateZodClientFromOpenAPI-3ae8a23b.cjs.dev.js:1679:26)
    at getZodClientTemplateContext (/home/spark/.local/share/pnpm/store/v3/tmp/dlx-27449/node_modules/.pnpm/openapi-zod-client@1.13.1/node_modules/openapi-zod-client/dist/generateZodClientFromOpenAPI-3ae8a23b.cjs.dev.js:2212:16)
    at _callee$ (/home/spark/.local/share/pnpm/store/v3/tmp/dlx-27449/node_modules/.pnpm/openapi-zod-client@1.13.1/node_modules/openapi-zod-client/dist/generateZodClientFromOpenAPI-3ae8a23b.cjs.dev.js:2453:20)
    at tryCatch (/home/spark/.local/share/pnpm/store/v3/tmp/dlx-27449/node_modules/.pnpm/openapi-zod-client@1.13.1/node_modules/openapi-zod-client/dist/generateZodClientFromOpenAPI-3ae8a23b.cjs.dev.js:77:17)
    at Generator.<anonymous> (/home/spark/.local/share/pnpm/store/v3/tmp/dlx-27449/node_modules/.pnpm/openapi-zod-client@1.13.1/node_modules/openapi-zod-client/dist/generateZodClientFromOpenAPI-3ae8a23b.cjs.dev.js:158:22)

Node.js v20.10.0
astahmer commented 12 months ago

looking at the JSON, it doesnt seem like it's supposed to generate anything, right ? in that case, you could parse/traverse the openapi JSON and remove any reference to that fhirprimitiveextension type before passing it to typed-openapi / openapi-zod-client

trevorpfiz commented 12 months ago

Apparently there are loads of unsupported types throughout the openapi.json. Maybe most of them are not important as you said? My first time working with OpenAPI, so I don't know if this is a "bad" spec or if they all require some cleaning. Would it make sense to not have unsupported types error out and instead make them something like unknown or nullable or any?

I am using the Canvas Medical FHIR API: https://docs.canvasmedical.com/api/

My plan right now is to either manually clean the openapi.json, or attempt to convert their Postman Collections into OpenAPI which may give a better spec?

I'm a bit lost 😂

trevorpfiz commented 12 months ago

converting the Postman Collections into OpenAPI worked well, and I was able to generate a zod client! 🎉

I am supposed to manually validate like this right? No out-of-the-box runtime validation?

const patientsData = await api.get("/Patient", { query: {} });
const validatedData = get_SearchPatient.response.parse(patientsData);
return validatedData;

Can close this issue if you are happy with how Unsupported schema type is handled.

Thanks!

astahmer commented 12 months ago

glad that you found a solution !


I am supposed to manually validate like this right? No out-of-the-box runtime validation?

yes, my intention for this library was to be "headless", by that I mean that it only provides the primitives to build your own type-safe API client (without any runtime by default !)

in the fetcher (that you provides to the createApiClient), I think there's enough infos to make this automatic by retrieving the correct schema (if not I'm 100% open to adding what's needed):

 type Fetcher = (
        method: Method,
        url: string,
        parameters?: EndpointParameters | undefined,
      ) => Promise<Endpoint["response"]>;

as a lot of people have different needs, it's easier for me (in term of maintenance) to inverse the responsibility/control than having to integrate 100 different config options (like I did with openapi-zod-client) to try to fit in everyone's specific needs

that being said, if anyone comes up with a good default API client that validates etc by default, I'm fine with merging it in (as long as it's optional and that people can still create their own)


Can close this issue if you are happy with how Unsupported schema type is handled.

I think adding a callback to handle errors should make it easier to deal with, rather than having to pre-filter the openapi document (which I think it still a fine-ish solution)

I'll leave the issue open if anyone wants to send a PR for those issues mentioned above