-
-
Notifications
You must be signed in to change notification settings - Fork 156
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
Parse from string containing the yaml/json open-api #207
Comments
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
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
The text was updated successfully, but these errors were encountered: