apigee-127 / swagger-tools

A Node.js and browser module that provides tooling around Swagger.
MIT License
701 stars 373 forks source link

How to use x-swagger-router-handle-subpaths #572

Open sauravpandit opened 6 years ago

sauravpandit commented 6 years ago

I have seen x-swagger-router-handle-subpaths option in router middleware. It is acting like a wildcard after url. In my swagger json I have defined a path "/dog". If I use url http://localhost:3000/api/dog/3/ it is hitting the proper controller but problem is I am not able fetch argument/parameter from path. req.swagger.params is not giving me the full path. Is there any way I can get the full path or I can fetch the parameter. It will be great if you provide some example of how to use this x option.

{ status:
   { path: [ 'paths', '/dog', 'get', 'parameters', '0' ],
     schema:
      { name: 'status',
        in: 'query',
        description: 'The status to filter by',
        type: 'string' },
     originalValue: undefined,
     value: undefined } }
"/dog": {
        "x-swagger-router-controller": "dogs",
        "x-swagger-router-handle-subpaths": true,
        "get":{
          "tags": [ "Dog Operations" ],
          "operationId": "getAllDogs",
          "parameters":[
            {
              "name": "status",
              "in": "query",
              "description": "The status to filter by",
              "type": "string"
            }
          ],
          "summary": "Finds all dogs in the system",
          "responses": {
            "200": {
              "description": "Dog response",
              "schema": {
                "type": "array",
                "items": {
                  "$ref": "#/definitions/dogprop"
                }
              }
            },
            "default": {
              "description": "Unexpected error",
              "schema": {
                "$ref": "#/definitions/Error"
              }
            }
          }
        }
      }
clocked0ne commented 6 years ago

Your Swagger path declaration should be along the lines:

"/dog/{id}": {
        "x-swagger-router-controller": "dogs",
        "x-swagger-router-handle-subpaths": true,
        "get":{
          "tags": [ "Dog Operations" ],
          "operationId": "getAllDogs",
          "parameters":[
            {
              "name": "id",
              "in": "path",
              "description": "The identifier of the dog",
              "type": "number"
            },
            {
              "name": "status",
              "in": "query",
              "description": "The status to filter by",
              "type": "string"
            }
          ],
          "summary": "Finds all dogs in the system",
          "responses": {
            "200": {
              "description": "Dog response",
              "schema": {
                "type": "array",
                "items": {
                  "$ref": "#/definitions/dogprop"
                }
              }
            },
            "default": {
              "description": "Unexpected error",
              "schema": {
                "$ref": "#/definitions/Error"
              }
            }
          }
        }
      }

Access this in your controller as req.swagger.params.id.value