Skip to content
This repository has been archived by the owner on Sep 14, 2022. It is now read-only.

Change default location of swagger.yml and api controllers #373

Open
jusefb opened this issue Apr 29, 2016 · 12 comments
Open

Change default location of swagger.yml and api controllers #373

jusefb opened this issue Apr 29, 2016 · 12 comments

Comments

@jusefb
Copy link

jusefb commented Apr 29, 2016

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

@sylwit
Copy link

sylwit commented Jul 5, 2016

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

@evilive3000
Copy link

evilive3000 commented Dec 29, 2016

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?

@stv8
Copy link

stv8 commented Jan 3, 2017

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

image 2017-01-03 at 11 46 55 am

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?

apigee-127/swagger-tools#319

@akoskm
Copy link

akoskm commented Jan 20, 2017

This still doesn't answer how the default location of api/swagger can be changed. Am I missing something?

@stv8
Copy link

stv8 commented Jan 20, 2017

@akoskm check out @sylwit comment above mine, that was how I was able to change the location of the swagger file.

@akoskm
Copy link

akoskm commented Jan 20, 2017

@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.

@stephencoe
Copy link

@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
};

@matteoPhre
Copy link

matteoPhre commented Jun 29, 2018

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); } ); }

@dontry
Copy link

dontry commented Dec 20, 2018

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!

@ivtm86
Copy link

ivtm86 commented Nov 14, 2019

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

@fzn0x
Copy link

fzn0x commented Aug 6, 2021

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",

@fzn0x
Copy link

fzn0x commented Aug 6, 2021

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",

cross-env

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

10 participants