Swagger 2.0: Multiple Path objects with different paths but same request and response

让人想犯罪 __ 提交于 2019-12-17 20:49:00

问题


Due to some backward compatibility reasons, I need to support both the paths /ab and /a-b.

The request and response objects are going to be the same for both of the paths.

Can I have something like the following in my Swagger spec so that I do not have to repeat the request and response object definitions for both the paths.

paths:
  /ab:
  /a-b:
    post:
    ...

回答1:


Yes, you can have a path item reference another path item, like this:

paths:
  /ab:
    ...
  /a-b:
    $ref: '#/paths/~1ab'

Here, ~1ab is an escaped version of /ab.

In JSON pointers, there are two special characters that need to be escaped to be interpreted literally: ~ becomes ~0 and / becomes ~1:

  • /ab~1ab$ref: '#/paths/~1ab'
  • /foo/bar~1foo~1bar$ref: '#/paths/~1foo~1bar'
  • /ab~cd~1ab~0cd#/paths/~1ab~0cd
  • /ab/{c}/{d}~1ab~1{c}~1{d}#/paths/~1ab~1{c}~1{d}


One limitation of this approach is that you cannot have operationId in all operations of the referenced path item. This is because the copy of the path ends up with the same operationId values, but operationId must be unique.



来源:https://stackoverflow.com/questions/44150758/swagger-2-0-multiple-path-objects-with-different-paths-but-same-request-and-res

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!