Search code examples
javascriptnode.jsswaggerloopbackjsstrongloop

How can I hide the 'id' attribute in Loopback explorer?


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.


Solution

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

    In YOUR_MODEL.json file:

    {
      "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. 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.

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

    {
      "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

    BUT, /api/User POST with body:

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

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