Search code examples
javarestswaggerswagger-editor

Swagger Editor creates wrong path for Rest endpoint


So, I need to recreate my rest endpoints using Swagger. For that I am using the Swagger editor at editor.swagger.io

To call my actual rest endpoint, I need this path: http://localhost:8080/phonenumbersmanagement/api/v1/areacodes/1

Sadly, Swagger Editor creates a similar path, which I cannot use: http://localhost:8080/phonenumbersmanagement/api/v1/areacodes?id=1

It is a GET-request. I am getting a 405 - Method not allowed

My code in the Swagger editor looks like this:

/areacodes:
    post:
      tags:
      - "areacode"
      summary: "Add AreaCode"
      description: ""
      operationId: "addAreaCode"
      consumes:
      - "application/json"
      produces:
      - "application/json"
      parameters:
      - in: "body"
        name: "body"
        description: "add areacode"
        required: true
        schema:
          $ref: "#/definitions/AreaCode"
      responses:
        "405":
          description: "Invalid input"
    get:
      tags:
      - "areacode"
      summary: "Get Areacode"
      description: ""
      operationId: "getAreaCodeById"
      produces:
      - "application/json"
      parameters:
      - name: "id"
        in: "query"
        description: "Status values that need to be considered for filter"
        required: true
        type: "integer"
        format: "int32"
      responses:
        "200":
          description: "successful operation"
          schema:
            type: "array"
            items:
              $ref: "#/definitions/AreaCode"
        "400":
          description: "Invalid status value"

Does anoyone know how to fix this?


Solution

  • In .../areacodes/1, 1 is a path parameter, so the parameter must be defined as in: path rather than in: query. Also, endpoints with path parameters must be defined using path templates – .../areacodes/{id}, where {id} represents the path parameter named id.

    With this in mind, your GET operation needs to be defined as follows:

    paths:
      /areacodes/{id}:  # <------
        get:
          ...
          parameters:
          - name: "id"
            in: path    # <------
            description: "Status values that need to be considered for filter"
            required: true
            type: "integer"
            format: "int32"