如何在Loopback explorer中隐藏'id'属性?

时间:2022-11-25 19:24:38

Is it possible to hide the id attribute in a method in swagger-ui generated by explorer in Strongloop Loopback? I don want the user to create a new resource and send the id attribute. I know that if the user send the id it can be ignored but I want to hide it in the explorer.

是否可以在由Strongloop Loopback中的资源管理器生成的swagger-ui中的方法中隐藏id属性?我不希望用户创建新资源并发送id属性。我知道如果用户发送id,它可以被忽略但我想在资源管理器中隐藏它。

1 个解决方案

#1


10  

In order to hide the 'id' attribute, you need declare this field as hidden.

为了隐藏'id'属性,您需要将此字段声明为隐藏。

In YOUR_MODEL.json file:

在YOUR_MODEL.json文件中:

{
  "name": "YOUR_MODEL",
  .
  .
  .
  "properties": {
     // your custom properties
  },
  "hidden": ["id"], // this attribute specifies which attributes need to be hidden
  .
  .
  .
}

Be aware when a property declared as hidden:

当声明为隐藏的属性时,请注意:

  1. It's not exposed to the user
  2. 它不会暴露给用户
  3. Although hidden, if user sends provides a value with this property, the property won't be ignored by default, and will be handled with provided values. hence, need to be ignored manually.
  4. 虽然隐藏,但如果用户发送提供了具有此属性的值,则默认情况下不会忽略该属性,并且将使用提供的值处理该属性。因此,需要手动忽略。

For instance, if we have 'User' model as follows:

例如,如果我们有'User'模型如下:

{
  "name": "User",
  .
  .
  .
  "properties": {
     "id": "string",
     "name": "string",
     "password": "string",

  },
  "hidden": ["id", "password"],
  .
  .
}

/api/User GET request will provide list of Users with only 'name' attribute

/ api / User GET请求将提供仅具有“name”属性的用户列表

BUT, /api/User POST with body:

但是,/ api /用户POST身体:

{
  "user" : "USER",
  "password": "PASS",
  "id" : "USER_PROVIDED_ID"
}

the user provided in the body will be saved with the values in it.

正文中提供的用户将使用其中的值进行保存。

#1


10  

In order to hide the 'id' attribute, you need declare this field as hidden.

为了隐藏'id'属性,您需要将此字段声明为隐藏。

In YOUR_MODEL.json file:

在YOUR_MODEL.json文件中:

{
  "name": "YOUR_MODEL",
  .
  .
  .
  "properties": {
     // your custom properties
  },
  "hidden": ["id"], // this attribute specifies which attributes need to be hidden
  .
  .
  .
}

Be aware when a property declared as hidden:

当声明为隐藏的属性时,请注意:

  1. It's not exposed to the user
  2. 它不会暴露给用户
  3. Although hidden, if user sends provides a value with this property, the property won't be ignored by default, and will be handled with provided values. hence, need to be ignored manually.
  4. 虽然隐藏,但如果用户发送提供了具有此属性的值,则默认情况下不会忽略该属性,并且将使用提供的值处理该属性。因此,需要手动忽略。

For instance, if we have 'User' model as follows:

例如,如果我们有'User'模型如下:

{
  "name": "User",
  .
  .
  .
  "properties": {
     "id": "string",
     "name": "string",
     "password": "string",

  },
  "hidden": ["id", "password"],
  .
  .
}

/api/User GET request will provide list of Users with only 'name' attribute

/ api / User GET请求将提供仅具有“name”属性的用户列表

BUT, /api/User POST with body:

但是,/ api /用户POST身体:

{
  "user" : "USER",
  "password": "PASS",
  "id" : "USER_PROVIDED_ID"
}

the user provided in the body will be saved with the values in it.

正文中提供的用户将使用其中的值进行保存。