Skip to content

Commit

Permalink
feat(cli): use forward-slash characters in spec URL on Windows
Browse files Browse the repository at this point in the history
When creating a new OpenAPI-backed datasource and the spec URL is a
relative path, normalize the value on Windows to use forward-slash
characters (e.g. `../foo/bar.yaml`) instead of backslash-characters
(e.g. `..\foo\bar.yaml`).

Signed-off-by: Miroslav Bajtoš <[email protected]>
  • Loading branch information
bajtos committed Apr 21, 2020
1 parent ad7732c commit ca9f4f4
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 5 deletions.
19 changes: 18 additions & 1 deletion packages/cli/generators/openapi/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const utils = require('../../lib/utils');
const {parse} = require('url');
const path = require('path');
const semver = require('semver');
const slash = require('slash');
const {getControllerFileName, getServiceFileName} = require('./spec-helper');

const updateIndex = require('../../lib/update-index');
Expand All @@ -21,6 +22,8 @@ const DATASOURCE = 'datasources';
const SERVICE = 'services';
const g = require('../../lib/globalize');

const isWindows = process.platform === 'win32';

module.exports = class OpenApiGenerator extends BaseGenerator {
// Note: arguments and options should be defined in the constructor.
constructor(args, opts) {
Expand Down Expand Up @@ -285,8 +288,22 @@ module.exports = class OpenApiGenerator extends BaseGenerator {
async _generateDataSource() {
let specPath = this.url;
const parsed = parse(this.url);
if (parsed.protocol == null) {
if (
// Relative paths and UNIX paths don't have any protocol set
parsed.protocol == null ||
// Support absolute Windows paths, e.g. "C:\some\dir\api.yaml"
// When such path is parsed as a URL, we end up with the drive ("C:")
// recognized as the protocol.
(isWindows && parsed.protocol.match(/^[a-zA-Z]:$/))
) {
specPath = path.relative(this.destinationRoot(), this.url);
if (isWindows && !path.parse(specPath).root) {
// On Windows, convert the relative path to use Unix-style separator
// We need this behavior for our snapshot-based tests, but @bajtos
// thinks it is also nicer for users - at the end of the day,
// this is a spec URL and URLs always use forward-slash characters
specPath = slash(specPath);
}
}
this.dataSourceInfo.specPath = specPath;
const dsConfig = {
Expand Down
13 changes: 10 additions & 3 deletions packages/cli/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
"pluralize": "^8.0.0",
"regenerate": "^1.4.0",
"semver": "^7.3.2",
"slash": "^3.0.0",
"spdx-license-list": "^6.2.0",
"stringify-object": "^3.3.0",
"strong-globalize": "^6.0.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const {TestSandbox} = require('@loopback/testlab');
const {assertFilesToMatchSnapshot} = require('../../snapshots');

const generator = path.join(__dirname, '../../../generators/openapi');
const specPath = path.join(
const specPath = path.resolve(
__dirname,
'../../fixtures/openapi/3.0/petstore-expanded.yaml',
);
Expand All @@ -21,6 +21,15 @@ const SANDBOX_FILES = require('../../fixtures/openapi/3.0').SANDBOX_FILES;
const sandbox = new TestSandbox(path.resolve(__dirname, '../.sandbox'));
const testUtils = require('../../test-utils');

/*
const props = {
// Use a relative path in Unix style to ensure we can match the same snapshot
// both on Windows and Unix (Linux, MacOS).
url: path.relative(sandbox.path, specPath).replace(/\\/g, '/'),
dataSourceName: 'petStore',
};
*/

const props = {
url: specPath,
dataSourceName: 'petStore',
Expand Down

0 comments on commit ca9f4f4

Please sign in to comment.