Swagger环回模型模式中的嵌入式对象

Swagger环回模型模式中的嵌入式对象,swagger,loopbackjs,Swagger,Loopbackjs,在远程方法的上下文中,我试图定义在主体中传递的参数的模型模式。此对象如下所示: { name: "Alex", credentials: { user: "alex", pass: "pass" } } 因此,我的远程方法定义中有以下代码: MyModel.remoteMethod("postSomething", { accepts: [ {arg: 'person', type: { "na

在远程方法的上下文中,我试图定义在主体中传递的参数的模型模式。此对象如下所示:

{
    name: "Alex",
    credentials: {
        user: "alex",
        pass: "pass"
    }
}
因此,我的远程方法定义中有以下代码:

  MyModel.remoteMethod("postSomething", {
    accepts: [
      {arg: 'person', type: {
          "name": "string",
          "credentials": {
            "type": "object",
            "properties": {
              "user": "string",
              "pass: "string"
            }
          }
        }, http: {source: 'body'}, required: true
      }
    ],
.....
不幸的是,生成的Swagger explorer中没有显示此嵌入对象(凭据)的详细信息。这就是我所看到的:

{
    "user": "string",
    "credentials": {}
}
我尝试了许多不同的方法,但无法显示凭据对象的属性


有什么想法吗?

环回招摇器只拾取外部对象,而忽略对象的属性。 如果要在请求正文的swagger文档中显示嵌套对象,则必须创建嵌套模型

假设你有一个叫person的模型。您必须创建另一个名为“凭据”的模型,该模型具有用户和密码属性。然后在个人模型的配置中定义关系

{
  "name": "Person",
  "base": "PersistedModel",
  "idInjection": true,
  "options": {
    "validateUpsert": true
  },
  "properties": {
    "name": {
      "type": "string",
      "required": true
    }
  },
  "validations": [],
  "relations": {
    "credentials": {
      "type": "embedsOne",
      "model": "credentials",
      "property": "credentials",
      "options": {
        "validate": true,
        "forceId": false
      }
    }
  },
  "acls": [],
  "methods": {}
}
并添加对此模型的引用,在其中定义远程方法

MyModel.remoteMethod("postSomething", {
    accepts: [
      {arg: 'person', type: {Person}, 
       http: {source: 'body'}, required: true
      }
    ],

为避免“处理未知远程处理类型”警告,请确保您的模型在“model config.json”中标记为“public”

环回2.x

编辑:注意以下内容仅适用于环回2.x,因为类型注册表在3.x中发生了更改

问题在于,您提供的数据需要位于嵌套值的
type
属性上。这应该起作用:

MyModel.remoteMethod('postSomething', {
accepts: [
  {
    arg: 'person',
    type: {
      name: 'string',
      credentials: {
        type: {
          user: 'string',
          pass: 'string'
        }
      }
    },
    http: {
      source: 'body'
    },
    required: true
  }
],
//...
这也适用于阵列:

 accepts: [
  {
    arg: 'Book',
    type: {
      title: 'string',
      author: 'string',
      pages: [{
        type: {
          pageNo: 'number',
          text: 'string'
        }
      }]
    }
  }
],
// ...
环回3.x

由于Loopback 3.x中的模型注册表和强远程处理更改为只允许字符串或数组类型,因此您无法真正避免创建新模型。如果您想快速“内联”模型,而无需完成添加模型json文件的整个过程,请将其添加到
model config.json
等。您可以直接在应用程序上注册它:

app.registry.createModel('Person', {
  firstName: 'string',
  lastName: 'string'
}, { base: 'Model' });
如果要扩展现有模型(例如,添加仅在给定远程方法中接受的另一个属性),可以将基础设置为其他模型之一

如果要创建模型而不使模型注册表混乱,可以在
loobpack
本身上调用createModel:

const loopback = require('loopback')
const modl = loopback.createModel({
    name: 'Person',
    base: null,
    properties: {
      firstName: {
        type: 'string',
        id: true // means it won't have an id property
      }
    }
  });
在上述两个示例中,您都按名称引用模型以将其附加到远程方法:

accepts: [
  {
    arg: 'Person',
    type: 'Person'
  }
],
// ...

注意:您将需要为每个子属性(例如凭据)创建一个子模型。

这在环回3后不再起作用。类型只接受字符串或数组。这是正确的。类型注册表的工作方式在环回3中更改。如果您想执行OP请求的操作,您需要注册您自己的自定义类型,或者按照@abhinav的建议创建另一个模型。如何注册自定义类型而不定义模型?在环回3(特别是最新版本的强远程处理)中,您不能。但我已经用几种方法更新了我的原始响应,让您可以在lb3中在线创建模型定义,而无需设置model.json或将其附加到您的应用程序等。我一直在按照您关于loopback 3的说明进行操作,但如果我在loopback上创建模型,我会得到“将未知远程处理类型“person”视为“any”。你有什么解决办法吗?