Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

file.url in resolver is invalid #135

Closed
dawidgarus opened this issue Sep 10, 2019 · 3 comments
Closed

file.url in resolver is invalid #135

dawidgarus opened this issue Sep 10, 2019 · 3 comments

Comments

@dawidgarus
Copy link

When you use custom file resolver, say in bundle method:

swaggerParser.bundle(spec, {
  parse: {
    json: true,
  },
  resolve: {
    file: {
      order: 1,
      canRead(file) {
        console.log(path.normalize(file.url));
        try {
          path.resolve(file.url);
          return true;
        } catch (e) {
          return false;
        }
      },
      read(file) {
        const relativePath = path.relative(process.cwd(), path.resolve(file.url));
        const fixedPath = path.join(specDir, relativePath);
        return fse.readFile(fixedPath, 'utf8');
      },
    },
  },
});

You get "%20" instead of space if path in file.url contains space.
The file.url should contain valid path that can be used with node's path module

@JamesMessinger
Copy link
Member

Per the spec, all $refs are URLs, not file paths. So special characters (including spaces) are URL-encoded. If you want to treat the URL as a file path, then you can decode it using decodeURI() or something similar. Though I find that a better approach is to not use any spaces or special characters in my file names to avoid the problem altogether.

@dawidgarus
Copy link
Author

But it's not an URL either, because it doesn't contain scheme: "c:\foo\bar" in not a valid URL.

@JamesMessinger
Copy link
Member

It uses the base URL that you specify when calling .bundle(). If you specify something like C:\foo\bar then that's the base URL, and all $refs will be resolved relative to that. If you want to use special characters (and thus, URL decoding), then I recommend that you specify a valid base URL, like file:https://c/foo/bar instead.

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

No branches or pull requests

2 participants