如何在swagger zircote学说中正确添加参数而不会出错

时间:2017-03-01 09:28:32

标签: php doctrine swagger slim swagger-php

我第一次使用招摇,下面的代码导致“无效的参数定义”错误。

/**
 * @SWG\Post(
 *   path="/pub/user/login",
 *   summary="Login",
 *   tags={"User"},
 *   @SWG\Parameter(
 *         name="phone",
 *         in="formData",
 *         description="The phone from the database",
 *         required=true,
 *         type="string"
 *   ),
 *   @SWG\Parameter(
 *         name="token",
 *         in="formData",
 *         description="The token for this device and phone",
 *         required=true,
 *         type="string"
 *   ),
 *   @SWG\Response(
 *     response=200,
 *     description="JWT token"
 *   ),
 *   @SWG\Response(
 *     response=401,
 *     description="Wrong user credentials"
 *   ),
 *   @SWG\Response(
 *     response="default",
 *     description="an ""unexpected"" error"
 *   )
 * )
 */
$app->post('/pub/user/login', function() use($app) {
    $data = $app->request->post();

    $user = UserService::login($data['phone'], $data['token']);
    if ($user != false) {
        ApiService::success(SecurityService::getToken($user));
    } else {
        ApiService::forbidden('Wrong user credentials');
    }

});

我发现我需要为我的所有参数创建一个定义(但是如何?):

"definitions": {
    "ResponseDefinition": {
      "type": "object",
      "properties": {
        "text": {
          "type": "string",
          "description": ""
        }
      }
    }

我可以在zircote中使用:

/**
 * @SWG\Definition()
 */
class Product {

    /**
     * The product name
     * @var string
     * @SWG\Property()
     */
    public $name;
}

除了我的参数不是属性外,它只是静态方法中的POST参数

如何正确地做到这一点?

加入:

我所有终点的完整json是:

{"swagger":"2.0","info":{"title":"*******API","description":"","termsOfService":"","contact":{"email":"ricardo@documentready.nl"},"version":"1.0.0"},"host":"api.*******.com\/api\/demo","basePath":"\/","schemes":["https"],"paths":{"\/adm\/posdevice":{"post":{"tags":["posdevice"],"summary":"Create a new point of sale fore a merchent","parameters":[{"name":"title","in":"formData","description":"Point of sale's title","required":true,"schema":{"type":"string"},"type":"string"},{"name":"posId","in":"formData","description":"the merchant's point of sale id you want to create","required":true,"schema":{"type":"string"},"type":"string"}],"responses":{"200":{"description":"true|false"},"400":{"description":"missing parameter"}}}},"\/pub\/posdevice\/login":{"post":{"tags":["posdevice"],"summary":"Login","parameters":[{"name":"posDeviceId","in":"formData","description":"The posDeviceId from the database","required":true,"type":"string"},{"name":"token","in":"formData","description":"The token for this device and phone","required":true,"type":"string"}],"responses":{"200":{"description":"JWT token"},"401":{"description":"Wrong user credentials"},"default":{"description":"an \"unexpected\" error"}}}},"\/pub\/posdevice\/demologin":{"post":{"tags":["posdevice"],"summary":"Login","parameters":[{"name":"id","in":"formData","description":"The posDeviceId","required":true,"type":"string"}],"responses":{"200":{"description":"JWT token"},"401":{"description":"Wrong user credentials"},"default":{"description":"an \"unexpected\" error"}}}},"\/sec\/balance":{"get":{"tags":["Balance"],"summary":"Get the balance for the given user","responses":{"200":{"description":"Balance in cents"},"401":{"description":"Not allowed or wrong token"}}}},"\/adm\/merchant":{"post":{"tags":["merchant"],"summary":"Create a new merchant","parameters":[{"name":"title","in":"formData","description":"Merchants title","required":true,"schema":{"type":"string"},"type":"string"}],"responses":{"200":{"description":"true|false"},"400":{"description":"missing parameter"}}}},"\/pub\/merchantmodel\/factor":{"get":{"tags":["Merchantmodel"],"summary":"Get all the model factors","responses":{"200":{"description":"Array of all the models with factors"}}}},"\/adm\/merchantmodel\/factor":{"post":{"tags":["Merchantmodel"],"summary":"Create a new merchantmodel","parameters":[{"name":"title","in":"formData","description":"Verification code","required":true,"schema":{"type":"string"},"type":"string"},{"name":"androidFactor","in":"formData","description":"Verification code","required":true,"schema":{"type":"string"},"type":"string"},{"name":"iosFactor","in":"formData","description":"Verification code","required":true,"schema":{"type":"string"},"type":"string"}],"responses":{"200":{"description":"true|false"},"400":{"description":"missing parameter"}}}},"\/adm\/pos":{"post":{"tags":["pos"],"summary":"Create a new point of sale fore a merchent","parameters":[{"name":"title","in":"formData","description":"Point of sale's title","required":true,"schema":{"type":"string"},"type":"string"},{"name":"merchantId","in":"formData","description":"the merchant's id you want to create a pos for","required":true,"schema":{"type":"string"},"type":"string"}],"responses":{"200":{"description":"true|false"},"400":{"description":"missing parameter"}}}},"\/pub\/register\/phone":{"post":{"tags":["Register"],"summary":"Sends verification code via SMS to given phone number and register the user","parameters":[{"name":"phone","in":"formData","description":"Phone number","required":true,"schema":{"type":"string"},"type":"string"}],"responses":{"200":{"description":"the logintoken for this device"},"default":{"description":"an \"unexpected\" error"}}}},"\/sec\/register\/code":{"post":{"tags":["Register"],"summary":"Verify code from SMS","parameters":[{"name":"phone","in":"formData","description":"the phone number you rechieved the code on by sms","required":true,"schema":{"type":"string"},"type":"string"},{"name":"verificationCode","in":"formData","description":"Verification code","required":true,"schema":{"type":"string"},"type":"string"}],"responses":{"200":{"description":"true"},"401":{"description":"Not a valid verificationCode phone combination or the code is expired, this happens when the session is removed or you tried more than 5 times"},"default":{"description":"an \"unexpected\" error"}}}},"\/sec\/transaction":{"get":{"tags":["Transaction"],"summary":"Get all my transaction","responses":{"200":{"description":"Array of transactions"},"default":{"description":"an \"unexpected\" error"}}},"post":{"tags":["Transaction"],"summary":"Make a transaction to a consumer or a merchant","parameters":[{"name":"consumerId","in":"formData","description":"User ID of the consumer, consumer or merchant","schema":{"type":"integer"},"type":"integer"},{"name":"posDeviceId","in":"formData","description":"the Id of the merchants point of sale device, if the logged in user is a pos device, you can leave this empty","schema":{"type":"integer"},"type":"integer"},{"name":"senderId","in":"formData","description":"the user who pays","schema":{"type":"integer"},"type":"integer"},{"name":"amount","in":"formData","description":"Amount in cents","required":true,"schema":{"type":"integer"},"type":"integer"}],"responses":{"200":{"description":"New balance in cents"},"400":{"description":"An error with the request parameters"},"401":{"description":"Not allowed"}}}},"\/pub\/user\/login":{"post":{"tags":["User"],"summary":"Login","parameters":[{"name":"phone","in":"formData","description":"The phone from the database","required":true,"type":"string"},{"name":"token","in":"formData","description":"The token for this device and phone","required":true,"type":"string"}],"responses":{"200":{"description":"JWT token"},"401":{"description":"Wrong user credentials"},"default":{"description":"an \"unexpected\" error"}}}},"\/pub\/user\/demologin":{"post":{"tags":["User"],"summary":"Login","parameters":[{"name":"id","in":"formData","description":"","required":true,"type":"string"}],"responses":{"200":{"description":"JWT token"},"401":{"description":"Wrong user credentials"},"default":{"description":"an \"unexpected\" error"}}}}},"definitions":{},"securityDefinitions":{"Bearer":{"type":"apiKey","name":"Authorization","in":"header"}}}

这是错误:

{
"messages": ["attribute paths.'/adm/merchantmodel/factor'(post).[iosFactor].schema is unexpected", "attribute paths.'/adm/merchantmodel/factor'(post).[androidFactor].schema is unexpected", "attribute paths.'/sec/register/code'(post).[phone].schema is unexpected", "attribute paths.'/pub/register/phone'(post).[phone].schema is unexpected", "attribute paths.'/adm/posdevice'(post).[title].schema is unexpected", "attribute paths.'/sec/register/code'(post).[verificationCode].schema is unexpected", "attribute paths.'/sec/transaction'(post).[amount].schema is unexpected", "attribute paths.'/sec/transaction'(post).[senderId].schema is unexpected", "attribute paths.'/adm/merchantmodel/factor'(post).[title].schema is unexpected", "attribute paths.'/adm/posdevice'(post).[posId].schema is unexpected", "attribute paths.'/adm/merchant'(post).[title].schema is unexpected", "attribute paths.'/sec/transaction'(post).[consumerId].schema is unexpected", "attribute paths.'/sec/transaction'(post).[posDeviceId].schema is unexpected", "attribute paths.'/adm/pos'(post).[title].schema is unexpected", "attribute paths.'/adm/pos'(post).[merchantId].schema is unexpected"],
"schemaValidationMessages": [{
    "level": "error",
    "domain": "validation",
    "keyword": "pattern",
    "message": "ECMA 262 regex \"^[^{}/ :\\\\]+(?::\\d+)?$\" does not match input string \"api.******.com/api/demo\"",
    "schema": {
        "loadingURI": "#",
        "pointer": "/properties/host"
    },
    "instance": {
        "pointer": "/host"
    }
    }]

}

1 个答案:

答案 0 :(得分:1)

从参数中删除架构。

不正确:

{
  "name": "consumerId",
  "in": "formData",
  "description": "User ID of the consumer, consumer or merchant",
  "schema": {
    "type": "integer"
  },
  "type": "integer"
},

正确:

{
  "name": "consumerId",
  "in": "formData",
  "description": "User ID of the consumer, consumer or merchant",
  "type": "integer"
},