duangu4980 2017-03-01 09:28
浏览 74
已采纳

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

i'm using swagger for the first time and the code below results in an "Not a valid parameter definition" error.

/**
 * @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');
    }

});

what i have found is that i need to create a definition for all my parameters (but how?):

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

i can do that in zircote using:

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

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

Except that my parameters aren't properties, it's just a POST parameter inside a static method

How to do this correctly?

added:

my complete json from all my endpoints is:

{"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"}}}

and this is the error:

{
"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条回答 默认 最新

  • doushi6947 2017-03-19 19:41
    关注

    Remove the schema from the parameters.

    Incorrect:

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

    Correct:

    {
      "name": "consumerId",
      "in": "formData",
      "description": "User ID of the consumer, consumer or merchant",
      "type": "integer"
    },
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私