How can I make parameter optional in some but required in other cases in Swagger/PHP?

社会主义新天地 提交于 2020-01-04 05:43:06

问题


I'm documenting PHP REST-API with Swagger. So I defined my datamodel. My current Problem is, that the required state of some fields varies on different request types. My model looks like this:

 * @SWG\Definition(required={"firstName", "lastName"})
 *
 * @SWG\Property(property="id", type="integer", example="5")
 * @property integer $id
 *
 * @SWG\Property(property="firstName", type="string", example="Test Name")
 * @property string $firstName
 *
 * @SWG\Property(property="lastName", type="string", example="Test Name")
 * @property string $lastName
 *
 * @SWG\Property(property="created", type="string", example="2016-11-15 08:05:15")
 * @property string $created
  • On a post request firstName und lastName are required and id and created are asigned by the api.
  • On a put request id is required, but in the url path and firstName und lastName are optional while created can not be changed.
  • On a get request all fields are returned.

What a want is:

  • Show all fields for get requests.
  • Show id as required and show first name and last name as optional on put requests, but don't show created.
  • Show first name and last name as required on post request but don't show id and created.

I know I can mark fields as required with required=true but that marks them required everywhere the model is used. The only thing I figured out so far is the required id on the put request, as it is a parameter on it's own.

 *     @SWG\Parameter(
 *          name="id",
 *          in="path",
 *          required=true,
 *          type="integer"
 *      )

来源:https://stackoverflow.com/questions/40604286/how-can-i-make-parameter-optional-in-some-but-required-in-other-cases-in-swagger

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