Skip to content

fix(core): recover schema-factory imports in single-mode mock writer - #3648

Merged
melloware merged 2 commits into
orval-labs:masterfrom
aqeelat:fix/3627/single-mode-schema-factory-import-recovery
Jun 24, 2026
Merged

fix(core): recover schema-factory imports in single-mode mock writer#3648
melloware merged 2 commits into
orval-labs:masterfrom
aqeelat:fix/3627/single-mode-schema-factory-import-recovery

Conversation

@aqeelat

@aqeelat aqeelat commented Jun 24, 2026

Copy link
Copy Markdown
Contributor

Summary

single-mode.ts was the only writer missing the collectRecoveredSchemaFactoryImports + mergeGeneratorImports recovery pattern that split-mode, tags-mode, and split-tags-mode all use (#3607, 66af3e25b). On wide specs with faker schemas: true, shared-array import aggregation can strip get<X>Mock() factory imports from mockOutput.imports, producing uncompilable single-mode mock output.

Changes

Both mock branches in single-mode.ts are updated:

Inline branch (!shouldDeinlineMocks):

  • Added per-mockOutput finalizedMockImplementation computation, usesSchemaFactories check, and collectRecoveredSchemaFactoryImports + mergeGeneratorImports before filterLocalStrictMockTypeImports
  • Passes the finalized implementation to builder.importsMock instead of raw mockOutput.implementation (matches the other three writers; required for the recovery scan to see as PetMock casts added during finalization)

De-inlined branch (shouldDeinlineMocks):

  • Reordered so finalizedMockImplementation is computed before importsMockForBuilder (was computed after, making recovery impossible)
  • Added the same recovery + merge + filterLocalStrictMockTypeImports pipeline

Tests

Two regression tests in single-mode.test.ts:

  • Inline branch: verifies getPetMock appears in builder.importsMock imports when missing from mockOutput.imports
  • De-inlined branch: verifies the written .faker.ts file contains getPetMock

Closes #3627

Summary by CodeRabbit

  • Bug Fixes
    • Improved single-mode mock generation for Faker-based schemas to retain/recover required schema factory imports, even when imports are reorganized.
    • Applied mock finalization consistently across both inline mocks and separate generated mock files to ensure outputs remain complete and accurate.
  • Tests
    • Added regression coverage for single-mode mock generation to confirm recovered imports are present in both inline and de-inlined mock outputs.

…rval-labs#3627)

single-mode.ts was the only writer missing the
collectRecoveredSchemaFactoryImports + mergeGeneratorImports
recovery pattern added to split/tags/split-tags in 66af3e2.
On wide specs with faker schemas:true, shared-array import
aggregation could strip get<X>Mock() factory imports, producing
uncompilable single-mode mock output.

Inline branch: added per-mockOutput finalization, recovery scan,
and merge before importsMock. Also passes the finalized
implementation to builder.importsMock (was passing raw
mockOutput.implementation — matches the other three writers).

De-inlined branch: reordered so finalization happens before
importsMockForBuilder (was computed before, making recovery
impossible), and added the same recovery + merge pipeline.
@coderabbitai

coderabbitai Bot commented Jun 24, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 9d81131d-e883-4f58-9cf6-9045dcb2e261

📥 Commits

Reviewing files that changed from the base of the PR and between cd822b2 and a0376e1.

📒 Files selected for processing (1)
  • packages/core/src/writers/single-mode.test.ts
🚧 Files skipped from review as they are similar to previous changes (1)
  • packages/core/src/writers/single-mode.test.ts

📝 Walkthrough

Walkthrough

writeSingleMode now finalizes faker mock implementations before import generation and merges recovered schema-factory imports in both inline and de-inlined single-mode paths. Regression tests cover getPetMock recovery in the emitted imports and generated mock file.

Changes

single-mode faker schema-factory import recovery

Layer / File(s) Summary
Recovery logic in writeSingleMode
packages/core/src/writers/single-mode.ts
Imports collectRecoveredSchemaFactoryImports and mergeGeneratorImports, then applies finalized mock implementation handling and recovered-import merging in both the inline and de-inlined faker mock branches.
Regression tests for getPetMock recovery
packages/core/src/writers/single-mode.test.ts
Reformats the ../types import and adds regression coverage that verifies recovered getPetMock imports are present in inline mock generation and in the generated de-inlined faker mock file.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

  • orval-labs/orval#3253: Modifies packages/core/src/writers/single-mode.ts import-generation paths that are adjacent to the mock import recovery logic changed here.

Suggested labels

mock

Suggested reviewers

  • melloware
  • wadakatu

Poem

🐇 A bunny found a missing line,
getPetMock returned just fine.
In single-mode, the imports glow,
Through faker paths, they now can शो—
Hop hop! The mocks compile today,
With schema seeds back in the hay.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main change: recovering schema-factory imports in single-mode mock generation.
Linked Issues check ✅ Passed The changes match #3627 by adding import recovery in both single-mode branches and covering it with regression tests.
Out of Scope Changes check ✅ Passed The PR stays within scope, limited to single-mode import recovery and its regression tests.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@packages/core/src/writers/single-mode.test.ts`:
- Around line 207-211: The test in single-mode.test.ts is only asserting that
getPetMock appears in the generated file, which can pass even if import recovery
is broken. Update the assertion in the relevant test case to verify the emitted
import statement from the recovered mock module instead, using the same
mockContent check in single-mode.test.ts so the test proves the import was
actually restored.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 889db170-1b9a-46a1-a1f5-b42730a304dd

📥 Commits

Reviewing files that changed from the base of the PR and between 162a96c and cd822b2.

📒 Files selected for processing (2)
  • packages/core/src/writers/single-mode.test.ts
  • packages/core/src/writers/single-mode.ts

Comment thread packages/core/src/writers/single-mode.test.ts Outdated
…ed test

toContain('getPetMock') passes trivially since the implementation
body already calls getPetMock(). Switch to a regex that matches
the import statement, proving the import was actually recovered.
@melloware melloware added the mock Related to mock generation label Jun 24, 2026
@melloware melloware added this to the 8.19.0 milestone Jun 24, 2026
@melloware
melloware merged commit c01dfca into orval-labs:master Jun 24, 2026
5 checks passed
@aqeelat
aqeelat deleted the fix/3627/single-mode-schema-factory-import-recovery branch July 15, 2026 09:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

mock Related to mock generation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

single-mode missing faker schema-factory import recovery (asymmetric with split/tags/split-tags)

2 participants