kubb-labs / kubb

OpenAPI to TypeScript, React-Query, Zod, Zodios, Faker.js, MSW and Axios.
https://kubb.dev
MIT License
568 stars 40 forks source link

Error generating TS client from the GitHub API: stringify is unexpectedly receiving null. #1025

Closed andreterron closed 1 month ago

andreterron commented 1 month ago

What version of kubb is running?

10.2.4

What platform is your computer?

MacOS

What version of external packages are you using(@tanstack-query, MSW, React, Vue, ...)

"react": "^18.2.0", "react-dom": "^18.2.0", "zod": "^3.23.8"

What steps can reproduce the bug?

✔ 🔍 Config loaded(kubb.config.ts)
✖ 🚀 Build failed ./github.json
                                                                                                                                                                              10:58:43 PM

  Plugins:      3 failed, 3 total
   Failed:      plugin-oas, plugin-ts, plugin-client
Generated:      1721 files
     Time:      12.431s
    Ended:      Fri May 31 2024 22:58:43 GMT-0700 (Pacific Daylight Time)
   Output:      .

 ERROR  Cannot read properties of null (reading 'toString')                                                                                                                   10:58:43 PM

  at Object.stringify (node_modules/@kubb/core/dist/chunk-UCKFXEKM.js:110:42)
  at node_modules/@kubb/plugin-oas/dist/chunk-3NSBUDVM.js:552:32
  at Array.map (<anonymous>)
  at SchemaGenerator2.parseSchemaObject_fn (node_modules/@kubb/plugin-oas/dist/chunk-3NSBUDVM.js:551:44)
  at SchemaGenerator2.parse (node_modules/@kubb/plugin-oas/dist/chunk-3NSBUDVM.js:102:92)
  at SchemaGenerator2.schema (node_modules/@kubb/swagger-ts/dist/chunk-3MFJGUFM.js:357:23)
  at node_modules/@kubb/plugin-oas/dist/chunk-3NSBUDVM.js:222:44
  at Array.reduce (<anonymous>)
  at SchemaGenerator2.build (node_modules/@kubb/plugin-oas/dist/chunk-3NSBUDVM.js:220:46)
  at Object.buildStart (node_modules/@kubb/swagger-ts/dist/chunk-3MFJGUFM.js:574:49)

 ERROR  Cannot read properties of null (reading 'toString') (plugin: plugin-ts, hook: buildStart) (plugin: plugin-ts, hook: buildStart)                                       10:58:43 PM

  at EventEmitter.<anonymous> (node_modules/@kubb/core/dist/chunk-7UMWFPXG.js:126:19)
  at EventEmitter.emit (node:events:518:28)
  at EventEmitter.emit (node_modules/@kubb/core/dist/chunk-7UMWFPXG.js:82:34)
  at Object.emit (node_modules/@kubb/core/dist/chunk-7UMWFPXG.js:139:21)
  at PluginManager.catcher_fn (node_modules/@kubb/core/dist/index.js:623:15)
  at node_modules/@kubb/core/dist/index.js:425:53
  at Array.forEach (<anonymous>)
  at PluginManager.hookParallel (node_modules/@kubb/core/dist/index.js:422:13)
  at async safeBuild (node_modules/@kubb/core/dist/index.js:797:5)
  at async generate (node_modules/@kubb/cli/dist/generate-SXM56MPF.js:353:36)

Here's my kubb.config.ts file:

import { defineConfig } from "@kubb/core";
import { pluginTs } from "@kubb/swagger-ts";
import { pluginOas } from "@kubb/plugin-oas";
import { pluginClient } from "@kubb/swagger-client";

export default defineConfig({
  root: ".",
  input: {
    path: "./github.json",
  },
  output: {
    path: "./src/gen",
    clean: true,
  },
  plugins: [pluginOas(), pluginTs(), pluginClient()],
});

How often does this bug happen?

Every time

What is the expected behavior?

The expected behavior is that the client would be generated successfully. It worked well with a pet store example.

Swagger/OpenAPI file?

https://github.com/github/rest-api-description/blob/main/descriptions/api.github.com/api.github.com.2022-11-28.json

Additional information

I fixed my issue by updating the stringify function on @kubb/core to also handle null.

These lines: https://github.com/kubb-labs/kubb/blob/22ca5b17c61a62c25bc4f095e0593616f68a2b37/packages/core/src/transformers/stringify.ts#L3-L9

-   return JSON.stringify(trimQuotes(value.toString()));
+   return JSON.stringify(trimQuotes(value?.toString() ?? 'null'));

From the function signature, it looks like it doesn't expect it to be null, but it seems like something in the GitHub OpenAPI spec is breaking that expectation.