Unable to generate swagger-jsdoc after converting Node.js project into an executable
Closed this issue · 1 comments
NodeJs Project in Debug Mode
My SwaggerSetup
const options = { swaggerDefinition: { openapi: "3.0.0", info: { title: "Time to document that Express API you built", version: "1.0.0", description: "A test project to understand how easy it is to document and Express API", license: { name: "MIT", url: "https://choosealicense.com/licenses/mit/" }, contact: { name: "Swagger", url: "https://swagger.io", email: "Info@SmartBear.com" } }, servers: [ { url: "http://localhost:" + config.socketcon.port + "/" + config.socketcon.name +"/api/v1" } ] }, apis: [ "./Models/User.js","./Routes/index.js" ] };
To generate dynamic swagger doc
const specs = swaggerJsdoc(options);
router.get('/swagger.json', function(req, res) {
res.setHeader('Content-Type', 'application/json');
res.send(specs);
});
Output for http://localhost:4000/userservice/api/v1/swagger.json
{"openapi":"3.0.0","info":{"title":"Time to document that Express API you built","version":"1.0.0","description":"A test project to understand how easy it is to document and Express API","license":{"name":"MIT","url":"https://choosealicense.com/licenses/mit/"},"contact":{"name":"Swagger","url":"https://swagger.io","email":"Info@SmartBear.com"}},"servers":[{"url":"http://localhost:4000/userservice/api/v1"}],"paths":{"/users/":{"post":{"summary":"Create a new user","tags":["Users"],"requestBody":{"required":true,"content":{"application/json":{"schema":{"$ref":"#/components/schemas/User"}}}},"responses":{"200":{"description":"A user schema","content":{"application/json":{"schema":{"$ref":"#/components/schemas/User"}}}}}}},"/users/{userId}":{"get":{"summary":"Get a user by id","tags":["Users"],"parameters":[{"in":"path","name":"userId","schema":{"type":"string"},"required":true,"description":"Id of the user"}],"responses":{"200":{"description":"An users object","content":{"application/json":{"schema":{"$ref":"#/components/schemas/User"}}}}}}}},"components":{"schemas":{"User":{"type":"object","required":["name","email"],"properties":{"name":{"type":"string"},"email":{"type":"string","format":"email","description":"Email for the user, needs to be unique."}},"example":{"name":"Alexander","email":"fake@email.com"}}}},"tags":[{"name":"Users","description":"User management"}]}
After Converting NodeJS Project into Executables
Output for http://localhost:4000/userservice/api/v1/swagger.json
{"openapi":"3.0.0","info":{"title":"Time to document that Express API you built","version":"1.0.0","description":"A test project to understand how easy it is to document and Express API","license":{"name":"MIT","url":"https://choosealicense.com/licenses/mit/"},"contact":{"name":"Swagger","url":"https://swagger.io","email":"Info@SmartBear.com"}},"servers":[{"url":"http://localhost:4000/userservice/api/v1"}],"paths":{},"components":{},"tags":[]}
Why Swagger-jsdoc not generated swagger,json dynamically after converting in executables?
Hi @yogesh2591 I don't have any experience in packaging node scripts into binaries, but can share high-level thoughts.
I guess the best approach in your case would be to generate the API specification before building the binary. swagger-jsdoc
reads comments and yaml files. I doubt the binary would have comments, so the strategy of reading them before compiling the binary.