首页 > 解决方案 > 远程方法在资源管理器(Swagger)中没有登录示例

问题描述

据我了解,用户/登录是一种内置的远程方法。在 explorer (swagger) 上,它的外观包含所有需要的细节:

在此处输入图像描述

但是在我的远程方法上,我没有所有好的信息,例如示例等等:

在此处输入图像描述

我如何为接受对象的方法添加示例值

这是我的json:

 "methods": {
"specifyGurdianPhone": {
  "accepts": [
    {
      "arg": "guardianPhone",
      "type": "Object",
      "required": true,
      "description": "{guardianPhone: \"+97255111111\"}",
      "http": {
        "source": "body"
      }
    }
  ],
  "returns": [
    {
      "arg": "success",
      "type": "Object",
      "root": true
    }
  ],
  "description": "",
  "http": {
    "verb": "post"
  }

标签: loopbackjs

解决方案


这是因为您的参数和响应具有“对象”类型。Swagger 不知道它长什么样。要获得这样的视图,您需要将模型名称指定为类型或一一描述可能的属性。

示例 1:

{
   "specifyGurdianPhone": {
   "accepts": [
     {
       "arg": "guardianPhone",
       "type": "string", // !!! Now, swagger know the exact type of "guardianPhone" property
       "required": true
       "http": {
         "source": "form" // !!! Having the "form" here we say that it a property inside an object 
                          //     (it allows us to have the "string" type of the "object")
       }
     }
   ],
   "returns": [
     {
       "arg": "success",
       "type": "GuardianPhone", // !!! For example, let's return the full "GuardianPhone" instance
       "root": true
     }
   ],
   "description": "",
   "http": {
     "verb": "post"
   }
}

示例 2:

 {
   "specifyGurdianPhone": {
   "accepts": [
     {
       "arg": "guardianPhone",
       "type": "object", 
       "model": "GuardianPhone" // !!! Another way to let swagger know the type of the body
                                    //     (the same will be true, if you make the type "GuardianPhone" instead of "object" and delete "model" property)
       "required": true
       "http": {
         "source": "body"
       }
     }
   ],
   "returns": [
     {
       ...
   ]
 }

示例 3:

 {
   "specifyGurdianPhone": {
   "accepts": [
     {
       "arg": "guardianPhone",
       "type": "object", 
       "model": "GuardianPhone" // !!! Another way to let swagger know the type of the body
                                    //     (the same will be true, if you make the type "GuardianPhone" instead of "object" and delete "model" property)
       "required": true
       "http": {
         "source": "body"
       }
     }
   ],
   "returns": [
     {
       "arg": "success",

       // !!! Instead of a model name you can describe properties one by one, 
       //     but this trick will not work with arrays (it's true for "accepts" also)
       // !!! WARNING You need strong-remoting v3.15.0 or higher due to https://github.com/strongloop/loopback/issues/3717 for this approach
       "type": { 
         "id": {"type": "string", "description": "An id property"},
         "guardianPhone": {"type": "string"}
       },

       "root": true
     }
   ],
   "description": "",
   "http": {
     "verb": "post"
   }
 }

推荐阅读