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

Parse from string containing the yaml/json open-api #207

Open
digEmAll opened this issue Mar 2, 2022 · 3 comments
Open

Parse from string containing the yaml/json open-api #207

digEmAll opened this issue Mar 2, 2022 · 3 comments

Comments

@digEmAll
Copy link

digEmAll commented Mar 2, 2022

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

@MohdmM01
Copy link

MohdmM01 commented Mar 8, 2022

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.

@gravypower
Copy link

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.

@Marcos-Barranquero
Copy link

Marcos-Barranquero commented Jan 26, 2023

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.

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

4 participants