APIDevTools / swagger-parser

Swagger 2.0 and OpenAPI 3.0 parser/validator
https://apitools.dev/swagger-parser
MIT License
1.08k stars 153 forks source link

Import JSON Schema as File Not Over HTTP Request #246

Closed sam3k closed 1 year ago

sam3k commented 1 year ago

I'm trying to use the Swagger parser in the browser and need to import a JSON schema file in a relative path instead of a HTTP get request. It will be served next to the app.js file to begin with and later i'll be loading from a NPM package that generates the JSON schema via some CI/CD build. (e.g. node_modules/some_package/schema.json);

App structure (Typical React app):

src/
  - app.js
  - schema.json

Here is what I've tried so far:

  const api = await SwaggerParser.bundle(path.join('schema.json', ''), {
    resolve: {
      file: true,
      http: false,
    }
  });

Per the docs:

Swagger Parser comes with built-in support for HTTP and HTTPS, as well as support for local files (when running in Node.js). You can configure or disable either of these built-in resolvers.

Is this not possible in browser mode?

sam3k commented 1 year ago

I may have overthought this.

import schema from './apiv3.json';

const api = await SwaggerParser.bundle(schema);

console.log(api); // works!! 

I will keep this issue open till I have the functionality I have in mind implemented.

sam3k commented 1 year ago

Closing this issue as my previous comment is the way to solve it.