Skip to content

IanVS/kubb-items-bug

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

kubb itemsSchema naming bug — minimal reproduction

Bug description

When an external schema file (e.g. RecordKeeper.yaml) is first encountered as the items of an array response, kubb generates the wrong name for that schema: itemsSchema instead of recordKeeperSchema. Any subsequent $ref to the same file (e.g. as a request body property) imports from "./itemsSchema.ts" — a file that is never generated — causing a build failure with UNRESOLVED_IMPORT.

Root cause analysis

@kubb/oas 4.27.3 switched the bundler from @redocly/openapi-core to @readme/openapi-parser. The new bundler inlines external schemas at the first JSON Pointer path where they appear rather than always registering them at #/components/schemas/<Name>.

In this reproduction:

  1. GET /record-keepers is declared first, with RecordKeeper.yaml as array items.
  2. The bundler inlines the schema at: #/paths/~1record-keepers/get/responses/200/content/application~1json/schema/items
  3. POST /proposals references the same file; the bundler resolves it to the already-inlined path ending in /items.
  4. kubb extracts the last path segment: "items"resolveName("items", "function")"itemsSchema".
  5. createProposalMutationRequestSchema.ts imports { itemsSchema } from "./itemsSchema.ts", but itemsSchema.ts is never written.

Reproduction steps

pnpm install
pnpm generate:broken

Then inspect the generated file:

cat src/gen/zod/createProposalSchema.ts

Expected

import { recordKeeperSchema } from "./recordKeeperSchema.ts";

Actual

import { itemsSchema } from "./itemsSchema.ts";

src/gen/zod/itemsSchema.ts does not exist. Any bundler (or Node's native ESM loader) will fail:

Error: UNRESOLVED_IMPORT: ./itemsSchema.ts

Affected versions

  • 4.27.3 and above — introduced @readme/openapi-parser as the bundler

Working versions

  • 4.27.2 and below — used @redocly/openapi-core, which always assigned canonical #/components/schemas/<Name> refs to named external files

To verify with a working version:

pnpm add -D @kubb/cli@4.27.2 @kubb/core@4.27.2 @kubb/plugin-oas@4.27.2 @kubb/plugin-zod@4.27.2
pnpm generate:broken  # now generates correctly
cat src/gen/zod/createProposalMutationRequestSchema.ts
# → imports { recordKeeperSchema } from "./recordKeeperSchema.ts"

Workaround

Register shared schemas explicitly in the root OpenAPI file's components.schemas section so the bundler assigns them canonical #/components/schemas/<Name> refs:

# openapi.yaml
components:
  schemas:
    RecordKeeper:
      $ref: ./components/schemas/RecordKeeper.yaml

With this in place, the bundler inlines the schema at #/components/schemas/RecordKeeper and kubb correctly derives the name recordKeeperSchema.

About

Reproduction for an issue in kubb 4.27.3

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors