Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
Clear All
new posts

    RESTHandler / REST API - how to update a field to null? (12.0p)

    SmartGWT version: v12.0p_2020-01-09/Enterprise Deployment

    Hello, I am using the builtinds sample and attempting to set a field to null via the REST API. It seems to be dropping any incoming fields with a null value and not applying them to the record. Setting the field to other values via the API works fine, but I have not been able to clear the field.

    Here is a screenshot of me setting the Alligator's lifespan to 123 years in Postman:
    Click image for larger version  Name:	restRequest1.jpg Views:	0 Size:	75.8 KB ID:	266426


    That change shows up correctly on the web app:
    Click image for larger version  Name:	restRequest2.jpg Views:	0 Size:	77.4 KB ID:	266427




    However, if I then try to set the "lifeSpan" field to null, no update is made, and the lifeSpan remains 123.
    Click image for larger version  Name:	restRequest3.jpg Views:	0 Size:	74.8 KB ID:	266428


    I'm able to null out the field just fine through the web app, but is there some way to do it via a PUT request from the API?

    Thanks!

    #2
    When you hit the RESTHandler servlet, your request should match the format documented here:
    https://www.smartclient.com/smartcli...RestDataSource
    (See 'server inbound formats')

    So we'd expect to see operationType:"update" and see the actual values specified as the "data" attribute.

    You're also running an older version of SmartClient 12.0 from Jan 2020. We'd recommend you try the latest nightly build in case you're seeing an already-fixed issue, and if possible we'd recommend updating to 12.1 rather than 12.0 (the latest stable branch).

    If the problem persists after these steps, it's worth checking the server logs (https://www.smartclient.com/smartcli....serverLogging) to verify the request is actually being handled by the RESTHandler servlet.
    If this is the case, we'd recommend sharing the full request, the server response and any server logs that seem relevant

    Regards

    Comment


      #3

      Alright, I've upgraded to "v12.1p_2021-09-09/Enterprise Deployment" and tried the builtinds sample again, using the request format specified on the RestDataSource page. However, I see the same issue. I initially wanted to use Raw REST mode for this (as described in the "Raw REST mode" section here: https://www.smartclient.com/smartgwt...STHandler.html) instead of the full RestDataSource request format, but I've tested it both ways now for good measure.

      (Note: to get the sample working in 12.1 I had to add "freemarker-2.3.28.jar" to the builtinds project classpath, but no other changes were needed).

      Here is a successful request and response updating the alligator's lifespan to 100 years:

      Click image for larger version  Name:	12.1_restRequest1.jpg Views:	0 Size:	90.9 KB ID:	266439


      And here is the failing request where I attempt to set lifeSpan to null, and the server response showing that the value is still 100.
      Click image for larger version  Name:	12.1_restRequest2.jpg Views:	0 Size:	91.7 KB ID:	266440


      I've attached the server logs for both requests (see attachment "null_restAPI_test.txt"). The logs show that RESTHandler is picking up the request, and I can see that the lifespan attribute is being retained for the first request, but being dropped when the value is null.

      === 2021-09-10 12:04:24,025 [3-46] DEBUG RestRequestParser - Parsing json object: '{
      "transaction": {
      "operations": [{
      "dataSource":"animals",
      "operationType":"update",
      "data": {
      "scientificName": "Allligator mississippiensis",
      "lifeSpan": 100
      }
      }]
      }
      }'
      === 2021-09-10 12:04:24,026 [3-46] DEBUG RestRequestParser - Parameter:'isc_dataFormat'. Value:'json'.
      === 2021-09-10 12:04:24,028 [3-46] INFO RESTHandler - Performing 1 operation(s)
      === 2021-09-10 12:04:24,029 [3-46] INFO DSRequest - Executing animals.update with
      criteria: {scientificName:"Allligator mississippiensis"}
      values: {scientificName:"Allligator mississippiensis", lifeSpan:100}
      .
      .
      .

      === 2021-09-10 12:04:24,031 [3-46] INFO SQLDriver - Executing SQL query on 'HSQLDB' using connection '1442130334':
      UPDATE
      animals
      SET
      lifeSpan=100
      WHERE
      (
      animals.scientificName='Allligator mississippiensis'
      )
      === 2021-09-10 12:04:27,424 [3-70] DEBUG RestRequestParser - Parsing json object: '{
      "transaction": {
      "operations": [{
      "dataSource":"animals",
      "operationType":"update",
      "data": {
      "scientificName": "Allligator mississippiensis",
      "lifeSpan": null
      }
      }]
      }
      }'
      === 2021-09-10 12:04:27,424 [3-70] DEBUG RestRequestParser - Parameter:'isc_dataFormat'. Value:'json'.
      === 2021-09-10 12:04:27,426 [3-70] INFO RESTHandler - Performing 1 operation(s)
      === 2021-09-10 12:04:27,426 [3-70] INFO DSRequest - Executing animals.update with
      criteria: {scientificName:"Allligator mississippiensis"}
      values: {scientificName:"Allligator mississippiensis"}
      .
      .
      .
      === 2021-09-10 12:04:27,429 [3-70] INFO SQLDriver - Executing SQL query on 'HSQLDB' using connection '1442130334':
      UPDATE
      animals
      SET
      scientificName='Allligator mississippiensis'
      WHERE
      (
      animals.scientificName='Allligator mississippiensis'
      )

      After this test I tried again with the simpler rawREST "PUT" requests as shown in my original post, and saw the same thing in the logs.
      Attached Files
      Last edited by jbrasee_iqs; 10 Sep 2021, 08:33.

      Comment

      Working...
      X