Swagger 2.0: Multiple Path objects with different

2020-02-07 02:02发布

问题:

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.