cancel
Showing results for 
Search instead for 
Did you mean: 

How to automatically save generated /swagger.json into a local file

New Contributor

How to automatically save generated /swagger.json into a local file

Hello, 

I am new here. 

I am using swagger core to generate my swagger specification files. I am also using swagger ui dist folder to access swagger ui. To view my swagger document with index.html, i have generated swagger.json using rest client, then copy the spec into a file named response.json then put that file into swagger ui dist folder. From there i can change the url in index.html into the path to the spec file. Each time i add swagger annotations on the resource classes, i build the project and a new swagger.json gets generated and i have change the contents of the response.json into my new swagger.json to view the new changes made.

Is there a way that after building my project, it will first hit the endpoint to generate swagger.json and save it as response.json in our swagger-ui folder?

3 REPLIES 3
Moderator

Re: How to automatically save generated /swagger.json into a local file

Generally speaking, swagger-core generates the API definition at runtime (just like another API endpoint). It's a bit unclear from your description how your application works. so:

  1. If your UI is intended to run alongside the API, you can simply point the UI to the generated definition at runtime. That way it's always up to date and there's no issue.
  2. If you need the UI to be completely independent from the application, you can use the maven plugin we have to generate the definition at build time and put it wherever you need. More information can be found at https://github.com/swagger-api/swagger-core/tree/master/modules/swagger-maven-plugin.
New Contributor

Re: How to automatically save generated /swagger.json into a local file

Thank you for replying @RonRatovsky 

I would like to point the UI to the generated definition at runtime but i get a "Failed to Load spec". This may be because to access the definition, there must be some authentication and custom header added. I am using a url like this on index.html 

 
 
window.onload = function() {
// Build a system
const ui = SwaggerUIBundle({
url: "https://localhost:8443/example.swagger.io/api/v1/swagger.json?Platform-TenantId=default&&Authorization=Basic bWlmb3M6cGFzc3dvcmQ=",
dom_id: '#swagger-ui',
validatorUrl : false,
presets: [
SwaggerUIBundle.presets.apis,
SwaggerUIStandalonePreset
],
plugins: [
SwaggerUIBundle.plugins.DownloadUrl
],
layout: "StandaloneLayout"
})
 
Also the try it out buttons on the swagger ui do not function when i use the path url to the locally saved swagger spec. I think it may be due to the authentication and header to be added on every request inorder to access to the server.  How can i resolve this please?
Moderator

Re: How to automatically save generated /swagger.json into a local file

This is most likely a CORS issue - please refer to https://github.com/swagger-api/swagger-ui/blob/master/docs/usage/cors.md.

New Here?
Join us and watch the welcome video:
Watch the New Interview
APITestingMistake#2
Top Kudoed Authors