7

I have multiple parameters that I want to reference, but I do not want to specify them one by one.

This snippet does not make the parameters show up:

{
    ...
    "paths": {
        "/stuff": {
            "get": {
                "description": "Gets stuff",
                "operationId": "getStuff",
                "parameters": {
                    "$ref": "#/definitions/set1"
                }
            }
        }
    },
    "parameters": {
        "a": {
            "name": "a",
            "in": "query",
            "description": "Param A",
            "required": false,
            "type": "string"
        },
        "b": {
            "name": "b",
            "in": "query",
            "description": "Param B",
            "required": false,
            "type": "string"
        }
    },
    "definitions": {
        "set1": [
            {
                "$ref": "#/parameters/a"
            },
            {
                "$ref": "#/parameters/b"
            }
       ],
       "set2": ...
    }
}

Is this possible or do I have to specify each parameter like set1, for each request?

Arc
  • 11,143
  • 4
  • 52
  • 75
  • Possible duplicate of [Swagger/OpenAPI - use $ref to pass a reusable defined parameter](https://stackoverflow.com/questions/27005105/swagger-openapi-use-ref-to-pass-a-reusable-defined-parameter) – robertwbradford Jun 13 '17 at 16:39
  • @robertwbradford don't think so, rather my question looks like an answer to your referenced question. It's about referencing a set. – Arc Jun 13 '17 at 17:43
  • I see now. Everyone, as you were... :) – robertwbradford Jun 13 '17 at 21:09

1 Answers1

2

Indeed that's not a valid definition and as you suggested, you'd have to specify each parameter separately by referencing the global one. If your parameters are shared for all operations under a specific path, you can define those at the path level and they would be applied to all operations.

For an individual operation, you'd define it as:

"paths": {
  "/stuff": {
    "get": {
      "description": "Gets stuff",
      "operationId": "getStuff",
      "parameters": [
        {
          "$ref": "#/parameters/a"
        },
        {
          "$ref": "#/parameters/b"
        }
      ]
    }
  }
}
Ron
  • 14,160
  • 3
  • 52
  • 39
  • 3
    Too bad that this is not supported as I have different parameter sets which depend on the HTTP verb/method and paths for multiple entities that share those methods. However, I've also tried defining a `parameters` array on the same level as the `get` in the example, which should work according to the Swagger spec, but in [swagger-ui](https://github.com/swagger-api/swagger-ui) the parameters do not show up unless I define them below the `get`. Any idea why? – Arc Apr 30 '15 at 07:53
  • Hm, looks like [the fix for this](https://github.com/swagger-api/swagger-ui/issues/617) is related to [the fix for top-level params](https://github.com/swagger-api/swagger-ui/issues/621) in [`develop-2.0` branch](https://github.com/swagger-api/swagger-ui/commit/21d8a89316ffbb8e5156f1ba1bdcd91a2140dbcf), but apparently it has been merged into `master` already which I just checked out yesterday. Strange. – Arc Apr 30 '15 at 08:22
  • It should be available in master in a few days, but yes, for now you can use the develop_2.0 branch. – Ron Apr 30 '15 at 08:59
  • The thing is, it looks like that commit is in `master` already which I have checked out but it still does not seem to consider parameters in the path item object. – Arc Apr 30 '15 at 09:42
  • Feel free to open a new ticket ;) – Ron Apr 30 '15 at 09:50