Search code examples
swaggerswagger-2.0swagger-editorswagger-codegen

Is it possible to specify Swagger Routes based on the body parameter


I have an existing API which is not quite RESTful. I was trying to use Swagger to quickly put together a Mock Server so that I can test the App without relying on the server. As I started writing the yaml rules, I realized that for a subset of routes, the same endpoint is being used and the server parses the body json to determine exactly what the request is. Is there a way to model such a behavior using Swagger?

(Please don't comment on the API architecture, it already exists and in productions and I wasn't involved in the design)


Solution

  • Not possible to define multiple endpoints/behaviors that share the same URL.

    The swagger.yml is assuming that you have a RESTful API. Swagger will define behavior and valid payloads for each route, and each route must have a unique path. You cannot represent multiple routes through the same URL with Swagger, unless you were to merge them all and represent them as a generic endpoint. This would require you to describe the behavior using documentation and you would lose many of the benefits of swagger (a pleasant UI and automatic validation). If you have distinct input payloads for each route, then all fields would need to be defined in the same payload and the documentation would describe which fields are required in each scenario.

    If possible, consider defining multiple endpoints and then proxy the requests to the same backend single route.