fix(mock): use exported element aliases for top-level array responses in arrayItems factories - #3709
Conversation
… in arrayItems factories (orval-labs#3706) getArrayItemFactoryNames fabricated `${pascal(operationId)}${pascal(propertyName)}Item` for top-level array responses (no parentName), producing type names the schema generator never emits — unexported/undeclared identifiers that fail tsc. Now the mock generator derives the real emitted alias instead: for inline top-level arrays, reuse the alias before the trailing `[]` (bailing on non-identifier bases like unions or `readonly`); for `$ref`'d array schemas, reuse `<RefName><itemSuffix>` (bailing when the items are a multi-ref `allOf` with no direct properties, since core emits no alias there). All previously-working branches (itemsRef, parentName) are byte-identical. Fixes orval-labs#3706 Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Run ID: ⛔ Files ignored due to path filters (6)
📒 Files selected for processing (3)
🚧 Files skipped from review as they are similar to previous changes (1)
📝 WalkthroughWalkthroughTop-level array faker factories now reuse derivable item aliases, reject ambiguous aliases, and add documentation, tests, OpenAPI fixtures, and MSW configurations covering referenced, inline, and nullable responses. ChangesTop-level array item aliases
Estimated code review effort: 3 (Moderate) | ~20 minutes Possibly related PRs
Suggested labels: Suggested reviewers: 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Warning There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure. 🔧 ESLint
ESLint install failed. For unrecoverable errors, disable the tool in CodeRabbit configuration. Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Pull request overview
Fixes MSW (arrayItems: true) mock generation for top-level array response schemas by aligning extracted array-item factory type names with the schema generator’s exported element aliases, preventing TypeScript compile failures from phantom/unexported imports (issue #3706).
Changes:
- Updates array-item factory name/type derivation for top-level array responses, with conservative bail-outs when an alias can’t be derived reliably.
- Adds a new repro specification + config targets and commits new snapshot fixtures (normal + strict mock modes).
- Documents the top-level array response behavior in the Faker guide.
Reviewed changes
Copilot reviewed 23 out of 23 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| tests/specifications/issue-3706-msw-unexported-item-aliases.yaml | Adds a repro OpenAPI spec covering top-level $ref and inline array responses plus a nested nullable-array guard. |
| tests/configs/mock.config.ts | Adds two new mock generation targets for the new spec (normal + strict). |
| tests/snapshots/mock/issue-3706-msw-array-item-aliases/model/itemsItem.ts | New snapshot: generated element alias for Items. |
| tests/snapshots/mock/issue-3706-msw-array-item-aliases/model/items.ts | New snapshot: generated array alias Items = ItemsItem[]. |
| tests/snapshots/mock/issue-3706-msw-array-item-aliases/model/index.ts | New snapshot: barrel exports for the new fixture’s model types. |
| tests/snapshots/mock/issue-3706-msw-array-item-aliases/model/getNullableList200RowsItem.ts | New snapshot: element alias for nested nullable array property. |
| tests/snapshots/mock/issue-3706-msw-array-item-aliases/model/getNullableList200.ts | New snapshot: wrapper object with nullable array property typing. |
| tests/snapshots/mock/issue-3706-msw-array-item-aliases/model/getCatalogItemsInline200Item.ts | New snapshot: emitted alias for inline top-level array element type. |
| tests/snapshots/mock/issue-3706-msw-array-item-aliases/model/catalogItemsItem.ts | New snapshot: element alias for $ref’d array schema CatalogItems. |
| tests/snapshots/mock/issue-3706-msw-array-item-aliases/model/catalogItems.ts | New snapshot: generated array alias CatalogItems = CatalogItemsItem[]. |
| tests/snapshots/mock/issue-3706-msw-array-item-aliases/endpoints.ts | New snapshot: MSW handlers + extracted array-item factories using exported element aliases. |
| tests/snapshots/mock/issue-3706-msw-array-item-aliases-strict/model/itemsItem.ts | New snapshot: strict variant model output. |
| tests/snapshots/mock/issue-3706-msw-array-item-aliases-strict/model/items.ts | New snapshot: strict variant array alias. |
| tests/snapshots/mock/issue-3706-msw-array-item-aliases-strict/model/index.ts | New snapshot: strict variant barrel exports. |
| tests/snapshots/mock/issue-3706-msw-array-item-aliases-strict/model/getNullableList200RowsItem.ts | New snapshot: strict variant element alias. |
| tests/snapshots/mock/issue-3706-msw-array-item-aliases-strict/model/getNullableList200.ts | New snapshot: strict variant wrapper typing. |
| tests/snapshots/mock/issue-3706-msw-array-item-aliases-strict/model/getCatalogItemsInline200Item.ts | New snapshot: strict variant inline element alias. |
| tests/snapshots/mock/issue-3706-msw-array-item-aliases-strict/model/catalogItemsItem.ts | New snapshot: strict variant $ref array element alias. |
| tests/snapshots/mock/issue-3706-msw-array-item-aliases-strict/model/catalogItems.ts | New snapshot: strict variant array alias. |
| tests/snapshots/mock/issue-3706-msw-array-item-aliases-strict/endpoints.ts | New snapshot: strict variant MSW output + strict mock helper typings. |
| packages/mock/src/faker/getters/array-item-factory.ts | Core fix: derives typeName from exported element aliases for top-level array responses; adds bail-outs for ambiguous expressions. |
| packages/mock/src/faker/getters/array-item-factory.test.ts | Adds failing-first unit coverage for the fixed top-level array response cases and bail-out shapes. |
| docs/content/docs/guides/faker.mdx | Documents how top-level array responses are handled (alias reuse + bail-outs). |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| if (propertyName.endsWith('[]')) { | ||
| const base = propertyName.slice(0, -2); | ||
| if (!/^[A-Za-z_$][A-Za-z0-9_$]*$/.test(base)) { | ||
| return undefined; | ||
| } | ||
| typeName = base; | ||
| } else { | ||
| const schema = items as OpenApiSchemaObject; | ||
| if (schema.allOf && !schema.properties && schema.type !== 'object') { | ||
| return undefined; | ||
| } | ||
| typeName = `${pascal(propertyName)}${itemSuffix}`; | ||
| } |
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@orval/angular
@orval/axios
@orval/core
@orval/effect
@orval/fetch
@orval/hono
@orval/mcp
@orval/mock
orval
@orval/query
@orval/solid-start
@orval/swr
@orval/zod
commit: |
Copilot review: nullable top-level arrays reach the no-parentName branch
with a ' | null'-suffixed definition ('CatalogItems | null'), which fell
into the bare-ref-name path and fabricated phantom names. The suffix is
now stripped before branch selection, and the bare-name branch gained an
identifier guard that bails to the always-correct inline body for any
non-identifier shape. Fixture gains a nullable top-level array operation.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
|
Copilot's nullable-array finding is fixed in fb4c14d: the ' | null' suffix is stripped before branch selection (so 'CatalogItems | null' resolves to the real |
Summary
Closes #3706.
With
mock.generators: [{ type: 'msw', arrayItems: true }], mocks for top-level array responses imported type names orval never emits — e.g.import type { GetCatalogItemsItemsItem } from './model'when onlyItemsItemexists. The generated module then failstscas soon as it's exported from a library's public API (reproduced live at HEAD with three phantom shapes).Root cause
getArrayItemFactoryNames(packages/mock/src/faker/getters/array-item-factory.ts) fabricates${pascal(operationId)}${pascal(propertyName)}${itemSuffix}wheneverparentNameis undefined — i.e. whenever the array is the top-level response schema — instead of reusing the element alias the schema generator actually emits (<RefName><itemSuffix>for$ref'd arrays, the pre-[]alias for inline ones). The nested variant of this class was already fixed via theisAmbiguousInlineItemContextguard, confirming the intended contract: mock type names must equal schema-generator names.Fix (pure bug fix, no config surface)
In the no-
parentNamebranch only:Foo[]definition) → reuse the pre-[]alias, bailing out to the always-correct inline.map()body when the base isn't a plain identifier ((A | B)[],readonly X[]);$ref'd array schema → reuse${pascal(refName)}${itemSuffix}(sameitemSuffixconfig core uses), bailing out for multi-refallOfitems with no direct properties (core emits no alias there);factoryNameformulas unchanged (outputs hitting this branch never compiled, so they're not a compat surface);itemsRefbranch,parentNamebranch, and ambiguity guard byte-identical.Because every output that hit this branch failed
tsc, no working project can observe a behavior change.Verification
tests/__snapshots__/mock/issue-3706-msw-array-item-aliases{,-strict}/**, zero modified existing snapshots (canariesfaker-array-items,msw-array-items,issue-3590*,issue-3574*untouched)Get…ItemsItemphantom plus inline/nullable/parentName regression guards; the strict variant (override.mock.required/nonNullable) exercises${typeName}Mocknaming over the corrected aliasestestsbuild gate) — the phantom import is exactly what this gate catches; lint cleanFollow-up (out of scope, discovered during research)
output.schemas: { type: 'zod' }barrels export no synthetic inline aliases at all (e.g.GetCatalogItemsInline200Itemis imported by the plain fetch client itself yet missing from the zod model dir). That's a separate defect in the zod schema writer / import-rerouting path (packages/orval/src/write-specs.ts:294-330) affecting non-mock output too — it deserves its own issue rather than being folded in here; this PR's fix is correct independent of it. I can file that issue with a minimal repro if wanted.Related issues
#3513 (reusable array-item mock factories — the feature this hardens), #3514 (arrayItems origin), #3269 / #3656 (same class: MSW missing imports), #3108 (zod + tags-split phantom exports), #3612 (faker import path), #3574 / #3590 (strict mock naming). Sibling cluster: #3702, #3704, #3705.
🤖 Generated with Claude Code
Summary by CodeRabbit