2016-11-11 68 views
0

在遠程方法的上下文中,我試圖定義傳遞給主體的參數的模型模式。這個對象是這樣的:Swagger的迴環模型模式中的嵌入對象

{ 
    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 
     } 
    ], 
..... 

可惜的是,這種嵌入對象(憑證)的詳細信息,在生成揚鞭不顯示探險家。這是我看到:

{ 
    "user": "string", 
    "credentials": {} 
} 

我已經嘗試了很多不同的方式,但我無法顯示憑據對象的屬性。

任何想法?

回答

0

Loopback swagger只拾取忽略對象屬性的外部對象。 如果您想在請求正文的swagger文檔中顯示嵌套對象,則必須製作嵌套模型。

假設你有一個叫做人的模型。您必須創建另一個名爲「credentials」的模型,其中包含屬性user和password。然後定義你的人模型的配置

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

爲了避免「處理未知的遠程型」警告確保您的模型標記爲 「公共」 你的 「模型config.json」

+0

@Aleks我的回答是否解決了您的問題 – abhinav

3

環回2.x的

編輯裏面:注意以下僅適用於Loopback 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

由於模型註冊表和強大的遠程環回3.X改爲只允許字符串或數組類型,您無法避免創建新模型。如果你想快速「內聯」,而無需通過增加模型JSON文件的全過程中去的模型,將其添加到model-config.json等,您可以直接在應用程序註冊:

app.registry.createModel('Person', { 
    firstName: 'string', 
    lastName: 'string' 
}, { base: 'Model' }); 

您可以設置如果你想擴展一個現有的模型(例如,克,通過調用createModel上loobpack添加僅在指定的遠程方法接受他人財產)

如果你想創建不會搞亂你的模型註冊表中的模型,你可以這樣做本身:

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' 
    } 
], 
// ... 

注意,你需要創建一個子模型對每個子屬性(如證書)

+0

這在Loopback3之後不再有效。類型只接受字符串或數組。 –

+0

這是正確的。在回送3中,類型註冊表的工作方式發生了變化。如果您想要執行類似於OP請求的操作,您需要註冊自己的自定義類型或創建另一個模型,如@abhinav建議的那樣。 –

+0

如何在不定義模型的情況下注冊自定義類型? –