简体   繁体   中英

How do I specify specific parameters in a Swagger response rather than use `allOf`?

I have defined some parameters using annotations in my model (PHP) and using darkaonline/l5-swagger to compile the lot to JSON. The annotations are like this:

/** 
* @OA\Schema(schema="UserModel", required={""})
*/
class User extends Authenticatable
{

/**
 * @OA\Property(
 *   property="id",
 *   type="integer",
 *   format="int64",
 *   description="The unique identifier of a user"
 * )
 */

/**
 * @OA\Property(
 *   property="email",
 *   type="string",
 *   example="john@example.com",
 *   description="The email address of a user"
 * )
 */

/**
 * @OA\Property(
 *   property="password",
 *   type="string",
 *   description="The password of a user"
 * )
 */

/**
 * @OA\Schema(
 *   schema="CreateUser",
 *   type="object",
 *   allOf={
 *     @OA\Schema(ref="#/components/schemas/UserModel"),
 *   }
 * )
 */

In the schema that I have defined for the payload I have allOf which will include all properties. This does work as far as loading the doc goes but I obviously don't want properties like id there. I assume there's an alternative to allOf that will allow me to define which properties I do want but I can't seem to find it.

Does anyone know what it may be?

There is no alternative in openapi to allOf that will allow you to choose which properties from a different object you want to use. If you really need that functionality you can define different objects containing each field separately and then reuse the ones that you really need. Something like that:

UserIdModel:
  properties:
    id: 
      type: integer
      ...
UserEmailModel:
  properties:
    email:
      type: string
      ...
UserPasswordModel:
  properties:
    password:
      type: string
      ...
CreateUser:
  allOf:
    - $ref: '#/components/schemas/UserEmailModel'
    - $ref: '#/components/schemas/UserPasswordModel'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM