Open digEmAll opened 2 years ago
I am also trying to find out whether swagger parser supports this. As far as I can understand from reading the document, swagger-parser parse or dereference input only accepts file name or URL.
the parse call can take a file or a object
https://apitools.dev/swagger-parser/docs/swagger-parser.html#parseapi-options-callback
I have passed it a json object and it parsed no issue.
const swagger = SwaggerParser.parse(jsonSwagger);
console.log(await swagger);
Not sure if this helps.
What about a YAML?
Reading a YAML file directly with the validator works fine, but reading a YAML as a string, then parsing it like this:
const main = async (): Promise<void> => {
try {
const apiPath = './path/to/openAPI3File.yaml'
const myApi = fs.readFileSync(apiPath, 'utf8')
const parsedAPI = await SwaggerParser.parse(myApi)
const api = await SwaggerParser.validate(parsedAPI)
console.log('API name: %s, Version: %s', api.info.title, api.info.version)
} catch (err) {
console.error(err)
console.log(' Error parsing API.')
}
}
Throws
$ tsc && node ./dist/index.js
{
stack: 'ResolverError: Error opening file "openapi: 3.0.1\n' +
...
Any input for this?
UPDATE: Fixed it using jsYAML, as used in the web example:
import SwaggerParser from '@apidevtools/swagger-parser'
import fs from 'fs'
import * as jsYAML from 'js-yaml'
const main = async (): Promise<void> => {
try {
const apiPath = './path/to/openAPI3File.yaml'
const myApi = fs.readFileSync(apiPath, 'utf8')
const jsonAPI = jsYAML.load(myApi)
const parsedAPI = await SwaggerParser.parse(jsonAPI)
const api = await SwaggerParser.validate(parsedAPI)
console.log(`API name: ${api.info.title}, Version: ${api.info.version}`)
} catch (err) {
console.error(err)
console.log(' Error parsing API.')
}
}
Would be nice to have it directly integrated for YAML though.
Hi, I'm trying to use swagger-parser in a browser application: the user should be able to paste the open-api yaml/json code into a text area and then swagger-parser library should parse the API.
As far as I understood the parse method accepts only a file url, but in this case I don't have any.
Is there any solution ? Am I missing something ?
Thanks in advance