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

Export a Swagger JSON/YAML file #158

Closed
Dominic-Preap opened this issue Nov 4, 2018 · 11 comments
Closed

Export a Swagger JSON/YAML file #158

Dominic-Preap opened this issue Nov 4, 2018 · 11 comments

Comments

@Dominic-Preap
Copy link

I'm submitting a...


[ ] Regression 
[ ] Bug report
[x] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Current behavior

Do we have an ability to export a Swagger JSON/YAML file from Swagger UI? Question from Statckoverflow

Expected behavior

Minimal reproduction of the problem with instructions

What is the motivation / use case for changing the behavior?

It could be better if I can export a Swagger JSON/YAML file and send it to someone rather than hosting the doc on the server.

Environment


Nest version: 5.3.11

 
For Tooling issues:
- Node version: 8.4.0  
- Platform: Windows 

Others:

@kamilmysliwiec
Copy link
Member

If your Swagger doc is available behind, let's say, https://localhost:3000/api simply use https://localhost:3000/api-json to get JSON object.

@Dominic-Preap
Copy link
Author

Would you mind update this in the document? Thanks

@webhacking
Copy link

@kamilmysliwiec /api-json path is not found.

@webhacking
Copy link

Write it to the file system like this:

const app = await NestFactory.create(ApplicationModule);
const options = new DocumentBuilder()
    .setTitle("Title")
    .setDescription("description")
    .setVersion("1.0")
    .build();
const document = SwaggerModule.createDocument(app, options);

fs.writeFileSync("./swagger-spec.json", JSON.stringify(document));
SwaggerModule.setup("/api", app, document);

@JobaerAhamed
Copy link

Visiting /api/json seems to serve the json file instead of /api-json . [Given that the swagger documentation is available at /api]

@cjmyles
Copy link

cjmyles commented Dec 23, 2020

Is there a way to generate this as a build task rather than a) it being served from the -json url mentioned above, or b) it being generated when the server is running (which requires the app instance)? I want to generate the .json spec then have it consumed by an openapi-generator command in order to generate an axios client.

@RezaRahmati
Copy link

If your Swagger doc is available behind, let's say, https://localhost:3000/api simply use https://localhost:3000/api-json to get JSON object.

Is there anyway to get Yaml version?

@jmcdo29
Copy link
Member

jmcdo29 commented Apr 29, 2021

If your Swagger doc is available behind, let's say, https://localhost:3000/api simply use https://localhost:3000/api-json to get JSON object.

Is there anyway to get Yaml version?

@RezaRahmati it depends on which http adapter you're using. Check the docs for those to find more info.

@RezaRahmati
Copy link

If your Swagger doc is available behind, let's say, https://localhost:3000/api simply use https://localhost:3000/api-json to get JSON object.

Is there anyway to get Yaml version?

@RezaRahmati it depends on which http adapter you're using. Check the docs for those to find more info.

Thanks @jmcdo29 for fast response,
I am using swagger-ui-express , I checked the doc before (https://docs.nestjs.com/openapi/introduction), it only said

document (returned by the SwaggerModule#createDocument() method) is a serializable object conforming to OpenAPI Document. Instead of hosting it via HTTP, you could also save it as a JSON/YAML file, and consume it in different ways.

If that is not the right page, would you please point me to the correct page

@RezaRahmati
Copy link

If your Swagger doc is available behind, let's say, https://localhost:3000/api simply use https://localhost:3000/api-json to get JSON object.

Is there anyway to get Yaml version?

@RezaRahmati it depends on which http adapter you're using. Check the docs for those to find more info.

I found an article here
https://dev.to/darraghor/add-aws-openapi-extensions-to-your-swagger-specification-on-nestjs-1ea6

npm install yaml

 const yamlString: string = yaml.stringify(document, {});
 fs.writeFileSync( "./open-api-spec/with-extensions.yaml"), yamlString);

@jmcdo29
Copy link
Member

jmcdo29 commented Apr 29, 2021

As I said, it depends on the underlying library, not the wrapper Nest creates. For express the library repo is here

@nestjs nestjs locked and limited conversation to collaborators Apr 30, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants