3

I had been using Swagger with custom configuration and directory structure in node application.

Here is snippet of my app.js

var swaggerConfig = {
  appRoot: config.get('app_root'),
  configDir: 'swagger/config',
  swaggerFile: 'swagger/api.yaml'
};

SwaggerExpress.create(swaggerConfig, function(err, swaggerExpress) {
  if (err) { throw err; }
  swaggerExpress.register(app);
});

Under config swagger/config directory having standard YAML configuration default.yml.

# swagger configuration file

# values in the swagger hash are system configuration for swagger-node
swagger:

  fittingsDirs: [ app/fittings ]
  defaultPipe: null
  swaggerControllerPipe: swagger_controllers  # defines the standard processing pipe for controllers

  # values defined in the bagpipes key are the bagpipes pipes and fittings definitions
  # (see https://github.com/apigee-127/bagpipes)
  bagpipes:

    _router:
      name: swagger_router
      mockMode: false
      mockControllersDirs: [ app/mocks ]
      controllersDirs: [ app/controllers ]

    _swagger_validate:
      name: swagger_validator
      validateResponse: true

    # pipe for all swagger-node controllers
    swagger_controllers:
      - onError: json_error_handler
      - cors
      - swagger_security
      - _swagger_validate
      - express_compatibility
      - _router

    # pipe to serve swagger (endpoint is in swagger.yaml)
    swagger_raw:
      name: swagger_raw

# any other values in this file are just loaded into the config for application access...

Now having issue that on controller req.swagger.params always return null.

halfer
  • 19,824
  • 17
  • 99
  • 186
Nazar Hussain
  • 5,102
  • 6
  • 40
  • 67

1 Answers1

4

This is unclear from the documentation, but you also need to add swagger_params_parser to swagger_controllers section in your default.yaml.

  bagpipes:

    _router:
      name: swagger_router
      mockMode: false
      mockControllersDirs: [ api/mocks ]
      controllersDirs: [ api/controllers ]

    _swagger_validate:
      name: swagger_validator
      validateResponse: true

    # pipe for all swagger-node controllers
    swagger_controllers:
      - swagger_params_parser
      - cors
      - swagger_security      
      - _swagger_validate      
      - _router
Vsevolod Goloviznin
  • 12,074
  • 1
  • 49
  • 50
  • Really thanks, that solved the issue. Can you provide me any reference wiki/code from where you find this layer on config. So that I should read it for future issue. And one question is, Why `swagger_params_parser` is not present in sample helloworld application and still params works. – Nazar Hussain Dec 16 '15 at 11:58
  • @NazarHussain In the sample that I've found they've omitted `swagger_controllers` at all, so I debugged the code and found all the bagpipes that are used and found this one :) – Vsevolod Goloviznin Dec 16 '15 at 12:07
  • 2
    Sorry that you found you had to debug this. The guide with this information is here: https://github.com/theganyo/swagger-node-runner/releases/tag/v0.6.0. – Scott Ganyo Dec 16 '15 at 12:57
  • @ScottGanyo awesome! And thanks for the great tool, btw :) – Vsevolod Goloviznin Dec 16 '15 at 13:19