Skip to content

Commit 4767b3d

Browse files
committed
fix: replace backslashes with forward slashes in refs on Windows when splitting
1 parent e1c065b commit 4767b3d

File tree

3 files changed

+33
-7
lines changed

3 files changed

+33
-7
lines changed

.changeset/eighty-dolls-sniff.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@redocly/cli": patch
3+
---
4+
5+
Fixed a problem when the `split` command produced backslashes instead of forward slashes in $refs on Windows.

.github/workflows/smoke.yaml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,27 @@ jobs:
283283
node-version: 18
284284
- run: bash ./__tests__/smoke/run-smoke.sh "yarn add ./redocly-cli.tgz" "yarn"
285285

286+
run-smoke--split--windows:
287+
needs: prepare-smoke
288+
runs-on: windows-latest
289+
steps:
290+
- uses: actions/cache@v3
291+
with:
292+
path: __tests__/smoke/
293+
key: cache-${{ github.run_id }}-${{ github.run_attempt }}
294+
enableCrossOsArchive: true
295+
- uses: actions/setup-node@v3
296+
with:
297+
node-version: 22
298+
- name: Run split smoke
299+
run: |
300+
npm i -g redocly-cli.tgz
301+
redocly --version
302+
redocly split resources/pets.yaml --outDir __tests__/smoke/__split-pets
303+
git diff
304+
redocly split resources/museum.yaml --outDir __tests__/smoke/__split-museum
305+
git diff
306+
286307
run-smoke--docker-image:
287308
runs-on: ubuntu-latest
288309
steps:

packages/cli/src/commands/split/index.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -176,9 +176,9 @@ function replace$Refs(obj: unknown, relativeFrom: string, componentFiles = {} as
176176
const groupName = splittedNode[2];
177177
const filesGroupName = componentFiles[groupName];
178178
if (!filesGroupName || !filesGroupName[name!]) return;
179-
let filename = path.relative(relativeFrom, filesGroupName[name!].filename);
179+
let filename = slash(path.relative(relativeFrom, filesGroupName[name!].filename));
180180
if (!filename.startsWith('.')) {
181-
filename = '.' + path.sep + filename;
181+
filename = './' + filename;
182182
}
183183
node[key] = filename;
184184
}
@@ -192,11 +192,11 @@ function implicitlyReferenceDiscriminator(
192192
) {
193193
if (!obj.discriminator) return;
194194
const defPtr = `#/${COMPONENTS}/${OPENAPI3_COMPONENT.Schemas}/${defName}`;
195-
const implicitMapping = {} as any;
195+
const implicitMapping: Record<string, string> = {};
196196
for (const [name, { inherits, filename: parentFilename }] of Object.entries(schemaFiles) as any) {
197197
if (inherits.indexOf(defPtr) > -1) {
198-
const res = path.relative(path.dirname(filename), parentFilename);
199-
implicitMapping[name] = res.startsWith('.') ? res : '.' + path.sep + res;
198+
const res = slash(path.relative(path.dirname(filename), parentFilename));
199+
implicitMapping[name] = res.startsWith('.') ? res : './' + res;
200200
}
201201
}
202202

@@ -258,7 +258,7 @@ function extractFileNameFromPath(filename: string) {
258258
}
259259

260260
function getFileNamePath(componentDirPath: string, componentName: string, ext: string) {
261-
return path.join(componentDirPath, componentName) + `.${ext}`;
261+
return slash(path.join(componentDirPath, componentName) + `.${ext}`);
262262
}
263263

264264
function gatherComponentsFiles(
@@ -273,7 +273,7 @@ function gatherComponentsFiles(
273273
inherits = (
274274
(components?.[componentType]?.[componentName] as Oas3Schema | Oas3_1Schema)?.allOf || []
275275
)
276-
.map(({ $ref }) => $ref)
276+
.map(({ $ref }) => $ref && slash($ref))
277277
.filter(isTruthy);
278278
}
279279
componentsFiles[componentType] = componentsFiles[componentType] || {};

0 commit comments

Comments
 (0)