Skip to content

fix: incorrect relative path generation in $ref during OpenAPI document splitting#175

Merged
thim81 merged 1 commit intothim81:mainfrom
tark-ai:main
Sep 3, 2025
Merged

fix: incorrect relative path generation in $ref during OpenAPI document splitting#175
thim81 merged 1 commit intothim81:mainfrom
tark-ai:main

Conversation

@SaranshChaudhary
Copy link
Copy Markdown
Contributor

Problem Description
When using the openapiSplit() function to split an OpenAPI document into multiple files, component references in $ref would generate incorrect relative paths, causing parseFile() to fail during bundling.
Example error:

Cannot resolve: test-split/components/schemas/schemas/NotificationPreferences.json

Note the duplicate schemas directory.


Root Cause
In utils/split.js (line 63), the writeComponents() function passed an incorrect currentFileDir parameter to convertComponentsToRef():

Before (incorrect):

convertComponentsToRef(components[componentType][componentName], ext, 'components')
  • A component like User.json in components/schemas/ referencing NotificationPreferences.json would yield:
    • Expected: "NotificationPreferences.json"
    • Actual: "schemas/NotificationPreferences.json"
  • This resulted in bundler looking for components/schemas/schemas/NotificationPreferences.json (extra schemas in the path).

Solution
After (fixed):

convertComponentsToRef(components[componentType][componentName], ext, path.join('components', componentType))
  • Now the function receives the full directory path (e.g., components/schemas), producing correct relative paths.

Test Results

  • All existing tests pass (no regression)
  • Split operation works correctly (proper file structure created)
  • Bundle operation works correctly (all $ref resolved)
  • End-to-end verification: Large documents can be split & bundled

Testing Steps

  1. Split Test:
    node -e "const {openapiSplit, parseFile} = require('./openapi-format.js');
    parseFile('./test-spec.json').then(obj => openapiSplit(obj, {output: './test-split/api.json'}))"
  2. Bundle Test:
    node -e "const {parseFile} = require('./openapi-format.js');
    parseFile('./test-split/api.json', {bundle: true}).then(obj => console.log('Success!'))"

Impact

  • Fixes broken functionality: Split documents can be properly bundled
  • No breaking changes: Existing functionality intact
  • Improves reliability: Corrects path resolution for complex references
  • Future-proof: Ensures path generation works for all component types

Summary
This PR resolves a critical bug with $ref resolution after splitting OpenAPI documents, improving reliability and ensuring smooth end-to-end workflows.

1

Fixes issue where split OpenAPI documents could not be bundled back
due to incorrect relative path generation in component $ref references.

**Problem:**
When splitting an OpenAPI document, components that reference other
components (e.g., User schema referencing NotificationPreferences)
would generate incorrect relative paths. This caused bundling to fail
with errors like:
"Cannot resolve: components/schemas/schemas/NotificationPreferences.json"

**Root Cause:**
In writeComponents(), the convertComponentsToRef() function was passed
'components' as currentFileDir instead of the full path 'components/{type}'.
This caused relative path calculation to be incorrect.

**Fix:**
Changed utils/split.js line 63 to pass the correct directory path:
- Before: convertComponentsToRef(..., 'components')
- After: convertComponentsToRef(..., path.join('components', componentType))

**Result:**
- Split documents now generate correct relative paths
- Bundling works correctly for all component references
- All existing tests continue to pass

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
@thim81
Copy link
Copy Markdown
Owner

thim81 commented Sep 3, 2025

hi @SaranshChaudhary

Thanks for reporting the issue.

The PR looks good. I'll add a test case so that we can monitor any regression in the future.

@thim81 thim81 merged commit 38f2bdc into thim81:main Sep 3, 2025
2 checks passed
@thim81
Copy link
Copy Markdown
Owner

thim81 commented Sep 12, 2025

hi @SaranshChaudhary

FYI: We just released openapi-format 1.28.0 which includes your PR.
Thanks again your contribution 🙌 .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants