Я использую swagger в первый раз, а приведенный ниже код приводит к ошибке «Недействительный параметр».
/** * @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":"Mobuyou API","description":"","termsOfService":"","contact":{"email":"ricardo@documentready.nl"},"version":"1.0.0"},"host":"api.mobuyou.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.mobuyou.com/api/demo\"", "schema": { "loadingURI": "#", "pointer": "/properties/host" }, "instance": { "pointer": "/host" } }] }
Удалите схему из параметров.
Неправильно:
{ "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" },