Skip to content

Commit 0a278b2

Browse files
fix: use forward slashes on $ref paths generated on windows (#82)
* fix: user forward slashes on $ref paths generated on windows * chore: add comment explaining a method and referencing the source
1 parent 282a8bb commit 0a278b2

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

Diff for: lib/migrate-2-3.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const {
1313
traverseFolderDeep,
1414
replace$Refs,
1515
implicitlyReferenceDiscriminator,
16+
ensureForwardSlashesInPath,
1617
OPENAPI3_COMPONENTS,
1718
OPENAPI3_METHODS
1819
} = require('./utils');
@@ -96,7 +97,7 @@ module.exports = async function migrate() {
9697
const encodedPath = basename(file, '.yaml');
9798
const path = encodedPath.replace(/@/g, '/');
9899
paths['/' + path] = {
99-
$ref: './' + join('paths', file)
100+
$ref: ensureForwardSlashesInPath('./' + join('paths', file))
100101
};
101102

102103
const pathData = readYaml(join('spec/paths', file));

Diff for: lib/split-definition.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const {
1010
implicitlyReferenceDiscriminator,
1111
replace$Refs,
1212
traverseFolderDeep,
13+
ensureForwardSlashesInPath,
1314
OPENAPI3_COMPONENTS,
1415
OPENAPI3_METHODS
1516
} = require('./utils');
@@ -69,7 +70,7 @@ module.exports = function(openapi, openapiDir) {
6970

7071
writeYaml(pathData, pathFile);
7172
openapi.paths[oasPath] = {
72-
$ref: path.relative(openapiDir, pathFile)
73+
$ref: ensureForwardSlashesInPath(path.relative(openapiDir, pathFile))
7374
};
7475
}
7576
}

Diff for: lib/utils.js

+14
Original file line numberDiff line numberDiff line change
@@ -225,3 +225,17 @@ function crawl(object, visitor) {
225225
crawl(object[key], visitor);
226226
}
227227
}
228+
229+
/**
230+
* Convert Windows backslash paths to slash paths: foo\\bar ➔ foo/bar
231+
* Copied form openapi-cli:
232+
* https://github.com/Redocly/openapi-cli/blob/b389661f91202174d5c46766ef2be73bfe9faf0d/packages/core/src/utils.ts#L161
233+
*/
234+
exports.ensureForwardSlashesInPath = (path) => {
235+
const isExtendedLengthPath = /^\\\\\?\\/.test(path)
236+
if (isExtendedLengthPath) {
237+
return path
238+
}
239+
240+
return path.replace(/\\/g, '/');
241+
}

0 commit comments

Comments
 (0)