1

I'm writing REST APIs using Swagger api. Now, I want to write methods for insert, update and delete in following way (I've seen such paths on many APIs online):

  1. POST (for insert)

/students

  1. PUT (for update)

/students/{studentId}

  1. DELETE (for delete)

/students/{studentId}

Now, 1 is okay, but 2 and 3 are same paths (but have different methods). Swagger API gives me error when I write so. So is this really not allowed?

If it's not allowed, what are some best ways to define different paths without making them long and still look 'cool'?

Vikas
  • 720
  • 1
  • 9
  • 30
  • It's allowed in node.js and there shouldn't be any conflict.. What is said in the error? – Charles Zha Aug 05 '17 at 05:57
  • Swagger Error Equivalent path already exists – Vikas Aug 05 '17 at 06:04
  • Possible duplicate of [Swagger: "equivalent path already exists" despite different parameters](https://stackoverflow.com/questions/35478531/swagger-equivalent-path-already-exists-despite-different-parameters) – Mario Trucco Aug 05 '17 at 06:50

1 Answers1

2

To define different methods (GET/PUT/DELETE/etc.) for the same path, simply list these methods under this path, like so:

paths:
  /students/{studentId}:
    # Common parameter for all methods on this path
    parameters:
      - name: studentId
        in: path
        ...

    get:
      summary: Get a student by ID
      ...

    put:
      summary: Update a student
      ...

    delete:
      summary: Delete a student
      ...
Helen
  • 87,344
  • 17
  • 243
  • 314
Charles Zha
  • 169
  • 2
  • 4
  • 17
  • What did you just wrote? get put in same path? Could you please explain a bit more? – Vikas Aug 05 '17 at 06:48
  • Oh, I never read about that. This is what I actually wanted. But is it officially allowed to write multiple methods under same path? – Vikas Aug 05 '17 at 08:34
  • 1
    ^^ This is the correct (and official) way to define multiple HTTP verbs GET/PUT/DELETE/etc. for the same path. The discussion on GitHub is about a different thing (same paths but differently named path parameters, e.g. `/students/{id}` vs `/students/{username}`). – Helen Aug 05 '17 at 11:27
  • 1
    If possible, please mark post this as the correct answer. Thank you :D – Charles Zha Aug 11 '17 at 20:55