Linx Main Linx Help

Returning NULL values from REST API operation

When hosting a REST API in your Linx Solution, for certain operations you may want to return NULL values as the fields for the consuming system. There are a few things you must be aware of when trying to achieve this.


For example if you are returning the below object:

      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                type: object
                properties:
                    email:
                      type: string
                    token:
                      type: string
                    username:
                      type: string
                    bio:
                      type: string
                    image:
                      type: string

  • If no value is specified in the field (no value or no explicit NULL value) then Linx will automatically return a value of the default type i.e. “” for a String field.

    image

    {
      "email":"me@example.com",
      "token":"",
      "username":"myuser",
      "bio":"",
      "image":""
    }
    
  • If you explicitly set the value as $.System.Null then Linx will not return that field.

    image

    {
      "email":"me@example.com",
      "username":"myuser",
    }
    
  • If you want to return a NULL value in the field (in this case the field token), you must ensure that your Open API definition fields have the nullable property on the field set as true as well as specifying that the field is required, like below:

       responses:
         '200':
           description: OK
           content:
             application/json:
               schema:
                 type: object
                 required:
                   - token
                 properties:
                     email:
                       type: string
                     token:
                       type: string
                       nullable: true
                     username:
                       type: string
                     bio:
                       type: string
                     image:
                       type: string
    

    Then if the response object is set like below:

    image

    The response object will contain the below values:

    {
     "email": "me@example.com",
     "token": null,
     "username": "myuser"
    }
    

More information about the difference between Open API 2 and Open API 3 with nullable fields can be found on this stackoverflow thread →

1 Like