Forum Discussion

SteMMo's avatar
SteMMo
Occasional Visitor
6 years ago

Define a parameter for both path and body (in different paths)

Hi all,
I'm just starting with SwaggerHub.
In my definition I'd like to share the definition of a parameter that I need to use both in a path and body position.

I wrote something like but I've got always some kind of error:

/root/{deviceId}/config:
    post:
      tags:
      - "Parameters"
      parameters:
      - $ref: "#/definitions/DeviceID"
  DeviceID:
    type: string
    description: Generato a partire dal MAC address
    example: AABBCCEE

Declared path parameter "deviceId" needs to be defined as a path parameter at either the path or operation level

If I add:

in: path

I get the error that sibling values are not allowed ..

I'd like to define a simple type that defines and documents the parameter used in path like:

/root/{deviceID}/config

and used in body ilke:

/root/ann

{
...
 "deviceID": "AAABBB",
...
}

Thanks!

  • HKosova's avatar
    HKosova
    SmartBear Alumni (Retired)

    Hi SteMMo,

     

    This is not possible in OpenAPI 2.0 - you'll need to refine the DeviceID type inline both in the path parameter and in the body parameter schema.

     

    However, this is possible in OpenAPI 3.0 should you decide to convert your definition to the new spec version.

    openapi: 3.0.0
    ...
    paths:
      /root/{deviceId}/config:
        post:
          parameters:
          - in: path
            name: deviceId
            required: true
            schema:
              $ref: '#/components/schemas/DeviceID'
          requestBody:
            content:
              application/json:
                schema:
                  $ref: '#/components/schemas/RequestBodySchema'
          responses:
            '200':
              description: OK
    
    components:
      schemas:
        DeviceID:
          type: string
          description: Generato a partire dal MAC address
          example: AABBCCEE
    
        RequestBodySchema:
          type: object
          properties:
            deviceID:
              $ref: '#/components/schemas/DeviceID'
            ...