2014-11-24 27 views
2

我正在爲移動應用程序編寫一個小API。 爲了方便我使用Swagger的移動開發者。 到目前爲止,除了一個GET請求外,一切正常。在我的Zend應用程序中的PHP Swagger查詢參數

當我在瀏覽器中調用/ user/messages/{sessionToken}?numMessages = {numMessages} & pageNr = {pageNr}時,我得到了我想要的結果,但是當我嘗試讓Swagger執行此請求時, sessionToken被傳送。我試過Swagger的這個註釋:

/** 
* @SWG\Api(path="/user/messages/{sessionToken}?numMessages={numMessages}&pageNr={pageNr}", 
* @SWG\Operation(
*  method="GET", 
*  summary="Gets messages paged", 
*  notes="", 
*  type="string", 
*  nickname="getUsermessagesPaged", 
*  authorizations={}, 
*  @SWG\Parameter(
*  name="sessionToken", 
*  description="The token from an active user session", 
*  required=true, 
*  type="string", 
*  paramType="path", 
*  allowMultiple=true 
* ), 
*  @SWG\Parameter(
*  name="numMessages", 
*  description="number of messages on page (numMessages & pageNr are ignored if not both are set)", 
*  required=true, 
*  type="string", 
*  paramType="query", 
*  allowMultiple=true 
* ), 
*  @SWG\Parameter(
*  name="pageNr", 
*  description="pagenumber (numMessages & pageNr are ignored if not both are set)", 
*  required=true, 
*  type="string", 
*  paramType="query", 
*  allowMultiple=true 
* ), 
*  @SWG\ResponseMessage(code=200, message="json {messages => 'user_messages'}"), 
*  @SWG\ResponseMessage(code=400, message="json with error 'not logged in'") 
* ) 
*) 
*/ 

有沒有人看到我的錯誤?

任何幫助,歡迎。

親切的問候

rholtermann

更新:由於建議我都修改了paramTypes爲 「查詢」,改變了路徑:

@SWG\Api(path="/user/messages/{sessionToken}", 

,但它沒有工作eighter。

在eclipse PDT的XDebug示出:

requestURI => /ias/public/user/messages/{sessionToken} 

- queryParams => Zend\\Stdlib\\Parameters 
    - *ArrayObject*storage => Array[0] 
     - => <Uninitialized> 

招搖JSON是:

{ 
    "apiVersion": "1.0.0", 
    "swaggerVersion": "1.2", 
    "apis": [ 
     { 
      "path": "\/user", 
      "description": "Operations about users" 
     } 
    ], 
    "info": { 
     "title": "Mobile access api", 
     "description": "This is the xxx mobile access api.", 
     "termsOfServiceUrl": null, 
     "contact": "xxx", 
     "license": null, 
     "licenseUrl": null, 
     "_partialId": null, 
     "_partials": [ ], 
     "_context": { 
      "comment": "\/**\ * @SWG\\Info(\ * title="Mobile access api",\ * description="This is the xxx mobile access api.",\ * contact="xxx",\ *)\ *\/", 
      "line": 3 
     } 
    } 
} 

這裏是/用戶的個輸出:

{ 
    "basePath": "http://localhost/ias/public", 
    "swaggerVersion": "1.2", 
    "apiVersion": "1.0.0", 
    "resourcePath": "/user", 
    "apis": [ 
     { 
      "path": "/user/balance/{sessionToken}", 
      "operations": [ 
       { 
        "method": "GET", 
        "summary": "Gets userbalance", 
        "nickname": "getUserdata", 
        "type": "string", 
        "parameters": [ 
         { 
          "paramType": "path", 
          "name": "sessionToken", 
          "type": "string", 
          "required": true, 
          "allowMultiple": false, 
          "description": "The token from an active user session" 
         } 
        ], 
        "responseMessages": [ 
         { 
          "code": 200, 
          "message": "json {balance => 'user_balance'}" 
         }, 
         { 
          "code": 400, 
          "message": "json with error 'not logged in'" 
         } 
        ], 
        "notes": "", 
        "authorizations": {} 
       } 
      ] 
     }, 
     { 
      "path": "/user/login", 
      "operations": [ 
       { 
        "method": "POST", 
        "summary": "Logs user into the system", 
        "nickname": "loginUser", 
        "type": "string", 
        "parameters": [ 
         { 
          "paramType": "form", 
          "name": "email", 
          "type": "string", 
          "required": true, 
          "allowMultiple": false, 
          "description": "The user email for login" 
         }, 
         { 
          "paramType": "form", 
          "name": "password", 
          "type": "string", 
          "required": true, 
          "allowMultiple": false, 
          "description": "The password for login in clear text" 
         } 
        ], 
        "responseMessages": [ 
         { 
          "code": 200, 
          "message": "json with session_id, user_id, user_balance" 
         }, 
         { 
          "code": 400, 
          "message": "json with error 'no user with given email and password'" 
         }, 
         { 
          "code": 400, 
          "message": "json with error 'invalid input'" 
         }, 
         { 
          "code": 400, 
          "message": "json with error 'no post request'" 
         } 
        ], 
        "notes": "", 
        "authorizations": {} 
       } 
      ] 
     }, 
     { 
      "path": "/user/logout", 
      "operations": [ 
       { 
        "method": "POST", 
        "summary": "Logs user out", 
        "nickname": "logoutUser", 
        "type": "string", 
        "parameters": [ 
         { 
          "paramType": "form", 
          "name": "sessionToken", 
          "type": "string", 
          "required": true, 
          "allowMultiple": false, 
          "description": "The token from an active user session" 
         } 
        ], 
        "responseMessages": [ 
         { 
          "code": 200, 
          "message": "json {result => 'deleted'}" 
         }, 
         { 
          "code": 400, 
          "message": "json with error 'no user_session with given sid'" 
         }, 
         { 
          "code": 400, 
          "message": "json with error 'invalid input'" 
         }, 
         { 
          "code": 400, 
          "message": "json with error 'no post request'" 
         } 
        ], 
        "notes": "", 
        "authorizations": {} 
       } 
      ] 
     }, 
     { 
      "path": "/user/messages/{sessionToken}", 
      "operations": [ 
       { 
        "method": "GET", 
        "summary": "Gets new messages", 
        "nickname": "getUsermessages", 
        "type": "string", 
        "parameters": [ 
         { 
          "paramType": "path", 
          "name": "sessionToken", 
          "type": "string", 
          "required": true, 
          "allowMultiple": false, 
          "description": "The token from an active user session" 
         } 
        ], 
        "responseMessages": [ 
         { 
          "code": 200, 
          "message": "json {messages => 'user_messages'}" 
         }, 
         { 
          "code": 400, 
          "message": "json with error 'not logged in'" 
         } 
        ], 
        "notes": "", 
        "authorizations": {} 
       }, 
       { 
        "method": "GET", 
        "summary": "Gets messages paged", 
        "nickname": "getUsermessagesPaged", 
        "type": "string", 
        "parameters": [ 
         { 
          "paramType": "path", 
          "name": "sessionToken", 
          "type": "string", 
          "required": true, 
          "description": "The token from an active user session" 
         }, 
         { 
          "paramType": "query", 
          "name": "numMessages", 
          "type": "string", 
          "required": true, 
          "description": "number of messages on page (numMessages & pageNr are ignored if not both are set)" 
         }, 
         { 
          "paramType": "query", 
          "name": "pageNr", 
          "type": "string", 
          "required": true, 
          "description": "pagenumber (numMessages & pageNr are ignored if not both are set)" 
         } 
        ], 
        "responseMessages": [ 
         { 
          "code": 200, 
          "message": "json {messages => 'user_messages'}" 
         }, 
         { 
          "code": 400, 
          "message": "json with error 'not logged in'" 
         } 
        ], 
        "notes": "", 
        "authorizations": {} 
       } 
      ] 
     }, 
     { 
      "path": "/user/userdata", 
      "operations": [ 
       { 
        "method": "POST", 
        "summary": "Posts userdata", 
        "nickname": "postUserdata", 
        "type": "string", 
        "parameters": [ 
         { 
          "paramType": "form", 
          "name": "sessionToken", 
          "type": "string", 
          "required": true, 
          "allowMultiple": false, 
          "description": "The token from an active user session" 
         }, 
         { 
          "paramType": "form", 
          "name": "password", 
          "type": "string", 
          "required": false, 
          "allowMultiple": false, 
          "description": "new password" 
         }, 
         { 
          "paramType": "form", 
          "name": "address", 
          "type": "string", 
          "required": false, 
          "allowMultiple": false, 
          "description": "new address" 
         }, 
         { 
          "paramType": "form", 
          "name": "housenr", 
          "type": "string", 
          "required": false, 
          "allowMultiple": false, 
          "description": "new housenr" 
         }, 
         { 
          "paramType": "form", 
          "name": "zip", 
          "type": "string", 
          "required": false, 
          "allowMultiple": false, 
          "description": "new zip" 
         }, 
         { 
          "paramType": "form", 
          "name": "city", 
          "type": "string", 
          "required": false, 
          "allowMultiple": false, 
          "description": "new city" 
         }, 
         { 
          "paramType": "form", 
          "name": "email", 
          "type": "string", 
          "required": false, 
          "allowMultiple": false, 
          "description": "new email" 
         } 
        ], 
        "responseMessages": [ 
         { 
          "code": 200, 
          "message": "json {user => 'userdata'}" 
         }, 
         { 
          "code": 400, 
          "message": "json with error 'not logged in'" 
         } 
        ], 
        "notes": "", 
        "authorizations": {} 
       } 
      ] 
     }, 
     { 
      "path": "/user/userdata/{sessionToken}", 
      "operations": [ 
       { 
        "method": "GET", 
        "summary": "Gets userdata", 
        "nickname": "getUserdata", 
        "type": "string", 
        "parameters": [ 
         { 
          "paramType": "path", 
          "name": "sessionToken", 
          "type": "string", 
          "required": true, 
          "allowMultiple": false, 
          "description": "The token from an active user session" 
         } 
        ], 
        "responseMessages": [ 
         { 
          "code": 200, 
          "message": "json {user => 'userdata', user_limit => 'userLimits'}" 
         }, 
         { 
          "code": 400, 
          "message": "json with error 'not logged in'" 
         } 
        ], 
        "notes": "", 
        "authorizations": {} 
       } 
      ] 
     } 
    ], 
    "produces": [ 
     "application/json" 
    ] 
} 

錯誤似乎是,我的swagger-ui不發出任何查詢參數? 這是一個只有一個查詢PARAM爲例,sessionToken: (由螢火蟲2.0.6監控)

GET /ias/public/user/balance HTTP/1.1 
Host: localhost 
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:33.0) Gecko/20100101 Firefox/33.0 
Accept: application/json 
Accept-Language: de,en-US;q=0.7,en;q=0.3 
Accept-Encoding: gzip, deflate 
Content-Type: application/json 
Referer: http://localhost/ias/swagger/ 
Cookie: __utma=111872281.581414660.1366700677.1394721873.1394723866.255; uvts=sB5Dda3cZBNdaTk; searchpanel-close=set 
Connection: keep-alive 

答案是:

HTTP/1.1 400 Bad Request 
Date: Tue, 25 Nov 2014 14:58:20 GMT 
Server: Apache/2.4.9 (Win32) PHP/5.5.12 
X-Powered-By: PHP/5.5.12 
Content-Length: 25 
Connection: close 
Content-Type: application/json; charset=utf-8 

的答案是正確的,因爲沒有sessionToken被傳送。

這就要求的作品,但它不會從招搖的UI來:

GET /ias/public/user/balance?sessionToken=54275cc6358e42c4b1fb1d8daf850b52 HTTP/1.1 
Host: localhost 
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:33.0) Gecko/20100101 Firefox/33.0 
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 
Accept-Language: de,en-US;q=0.7,en;q=0.3 
Accept-Encoding: gzip, deflate 
Cookie: __utma=111872281.581414660.1366700677.1394721873.1394723866.255; uvts=sB5Dda3cZBNdaTk; searchpanel-close=set 
Connection: keep-alive 
+0

有一件事還不清楚是當你觀察到的問題。它是什麼時候你的應用程序啓動?它是否在運行Swagger-UI操作? – Ron 2014-11-25 11:47:54

+0

當我運行Swagger-UI操作時,它不起作用,當我使用Chrome郵遞員執行請求時,一切都很好。 – rholtermann 2014-11-25 12:22:01

+0

好的。主要的問題是/ user/messages/{sessionToken}上有兩個GET操作。我假設你使用了我並不熟悉的swagger-php,所以我不能告訴你爲什麼這麼做,不幸的是。 – Ron 2014-11-25 12:26:07

回答

0

問題解決了,解決辦法是更新swagger-ui。

更新後,查詢參數已發送到應用程序,一切工作正常。

感謝大家試圖解決我的問題。

親切的問候 rholtermann

0

我認爲你缺少

paramType="query", 

paramType="path", 

我認爲這將解決您的問題

+0

謝謝你的回答,但那並不奏效,我之前曾嘗試過。我將所有三個參數類型都設置爲路徑,但參數「numMessages」和「pageNr」在兩種情況下均未傳輸。 – rholtermann 2014-11-24 12:38:48

+0

你是否在控制檯中檢查了什麼傳遞? – 2014-11-24 12:41:02

+0

allowMultiple = true刪除這個,然後嘗試 – 2014-11-24 12:44:06

1

更改第一行爲:

* @SWG\Api(path="/user/messages/{sessionToken}", 

Swagger不支持將查詢參數作爲路徑本身的一部分。它們將被自動添加到請求中(來自UI),因爲它們被描述爲query類型。

+0

謝謝你的回答,但它並不奏效。我檢查了xdebug中的請求參數並沒有提交任何參數。你有其他想法嗎? – rholtermann 2014-11-25 10:14:54

+0

是的。你能修改你的問題並添加你從應用程序中獲得的Swagger JSON嗎? – Ron 2014-11-25 10:19:02

+0

改變了它。但我有點困惑的API調用不顯示? – rholtermann 2014-11-25 10:42:50

相關問題