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

how can I use a spec file which only available local #1471

Closed
jinchenbravo opened this issue Sep 12, 2019 · 2 comments
Closed

how can I use a spec file which only available local #1471

jinchenbravo opened this issue Sep 12, 2019 · 2 comments

Comments

@jinchenbravo
Copy link

Q&A

  • OS: [macOS]
  • Environment: [Node.js v10.0.0]
  • Method of installation: [npm]
  • Swagger-Client version: [3.9.4]
  • Swagger/OpenAPI version: [OpenAPI 3.0]

How can we help?

For some authentication reason , the spec file can only be downloaded from somewhere to local,
and I need to use Swagger-Client to read the spec file and send the request to a remote host.
Looks like I must use url : http.... to get the client

Swagger({ url: "http://petstore.swagger.io/v2/swagger.json" }).then((client) => { ...

so the question is there a solution to read the spec from local ?

@jonathan-meyer
Copy link

I tired building a url with the node.js URL object.

const bitBucketSpecUrl = url.pathToFileURL(path.resolve(__dirname, "..", "lib", "bit-bucket-api.yaml"));
debug(bitBucketSpecUrl);
SwaggerClient({ url: bitBucketSpecUrl.toString() })
  .then(client => {
    debug(client.spec); 
    debug(client.originalSpec); 
    debug(client.errors); 
  })
  .catch(err => debug(err));

But it didn't work. 😩

According to the code it looks like only http urls are accepted.

if (specUrl && startsWith(specUrl, 'http')) {

@shockey
Copy link
Contributor

shockey commented Sep 27, 2019

Quoting myself from #1044 (comment):

file:// URLs are not really useful, since they're very difficult to use in browsers due to Same-Origin Policy and very few Node request libraries support them.

As for loading in a definition that you already have in memory, @c4milo is correct: you can pass your definition as a JS object under the spec key.

There's a workaround, though: #1410 (comment). Essentially, if you're using a local file, it's your responsibility to provide your data to Swagger Client as a JavaScript object.

@shockey shockey closed this as completed Sep 27, 2019
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

No branches or pull requests

3 participants