swagger-node
swagger-node copied to clipboard
Change default location of swagger.yml and api controllers
HI,
I have been banging my head against the wall with this. How can I change the default location of the swagger.yaml file for the "swagger project edit" and other swagger commands to work? The docs say that you can supply the directory as a parameter to the "swagger project edit" but it does not seem to work. Also it is not clear on how to configure these paths when running the app.
Thank you in advance for your help
You can override the location of the swaggerFile in your app.js for your swagger-express-mw
var config = {
appRoot: __dirname, // required config
swaggerFile: __dirname + '/config/swagger.yaml'
};
For swagger-node it's totally undocumented but digging in the code I found that you can use envvar
export swagger_swagger_fileName='config/swagger.yaml'
I created a task in package.json
"scripts": {
"edit": "swagger_swagger_fileName=config/swagger.yaml swagger project edit",
}
You can override any config values, the 1st swagger_ is a prefix you must use to override, the second swagger is the node you want to start overriding. You can also use swagger_project_
Hope that help
Oh, I wasted so much time to figure this out. @sylwit, thank you!
I just moved my package.json
file out of the project dir. Tried to feed directory
parameter, but nothing happened. Edit command sticked to package.json
. What's the point?
I still having issues with getting swagger project edit
to open a different swagger file.
I tried setting swagger_swagger_fileName=api/swagger/index.json
and tried using the npm script version too. I get this error
I found this other issue and it looks like my problem might be that I am using a JSON swagger file, is that the case?
https://github.com/apigee-127/swagger-tools/issues/319
This still doesn't answer how the default location of api/swagger
can be changed. Am I missing something?
@akoskm check out @sylwit comment above mine, that was how I was able to change the location of the swagger file.
@stv8 thanks, but if I'm correct that's how you change the location of the swagger configuration file which by default is located in config/default.yaml
.
@sylwit's answer is correct but maybe a little confusing as the example has put the swagger yaml inside the config dir.
updated examples for anyone in the future
package.json
"scripts": {
"edit": "swagger_swagger_fileName=path/to/my/swagger.yaml swagger project edit"
}
app.js
var config = {
configDir: `${__dirname}/config/swagger`, // move the config file
swaggerFile:`${__dirname}/path/to/my/swagger.yaml`, // move the swagger spec file
appRoot: __dirname // required config
};
Hi guys,
the @sylwit and @stephencoe solution doesn't work for me. When I run npm script I got an error on "swagger_swagger_filename not recognized as internal or external command". I've also changed the swagger config:
startSwaggerExpress(app) { SwaggerExpress.create( { appRoot: __dirname, configDir: "${__dirname}\\config", swaggerFile: "${__dirname}\\src\\api\\swagger\\swagger.yaml", controllersDirs: ["${__dirname}\\src\\api\\controllers"] }, (err, swaggerExpress) => { if (err) console.error(err); swaggerExpress.register(app); } ); }
You can override the location of the swaggerFile in your app.js for your swagger-express-mw
var config = { appRoot: __dirname, // required config swaggerFile: __dirname + '/config/swagger.yaml' };
For swagger-node it's totally undocumented but digging in the code I found that you can use envvar
export swagger_swagger_fileName='config/swagger.yaml'
I created a task in package.json
"scripts": { "edit": "swagger_swagger_fileName=config/swagger.yaml swagger project edit", }
You can override any config values, the 1st swagger_ is a prefix you must use to override, the second swagger is the node you want to start overriding. You can also use swagger_project_
Hope that help
Thank you bro. That's extremely helpful!
Hi, sorry but solution with npm stripts not working for me because npm says 'Command 'swagger_swagger_fileName' not known. Hope someone have other solution / idea thanks ivtm86
Hi, sorry but solution with npm stripts not working for me because npm says 'Command 'swagger_swagger_fileName' not known. Hope someone have other solution / idea thanks ivtm86
late response, but may be useful for the people someday or in the future, you can try it with cross-env.
"swagger:edit": "cross-env swagger_swagger_fileName=./documentation/swagger/swagger.yaml swagger project edit",
Hi, sorry but solution with npm stripts not working for me because npm says 'Command 'swagger_swagger_fileName' not known. Hope someone have other solution / idea thanks ivtm86
late response, but may be useful for the people someday or in the future, you can try it with cross-env.
"swagger:edit": "cross-env swagger_swagger_fileName=./documentation/swagger/swagger.yaml swagger project edit",