Patching Null++

Here’s a curiosity.

In my current project we’re looking at making HTTP PATCH requests. If we’re strictly following the REST standards, we might do things differently, but in our use case, a PATCH is a partial update, caused by discovering some fields of our object to have changed.

Imagine the object:

{
  "id": 123,
  "status": "ACTIVE",
  "isInternational": false,
  "customer": {
      "name": "Mr Foo",
      "rank": "A",
      "nextOrder": 987
  }
}

If the status changed, you might imagine an update that reads like this:

{
  "id": 123,
  "status": "INACTIVE",
  "customer": {
    "nextOrder": null
  }
}

This partial object can be merged with the existing object to produce the change we want.

Null vs Not Mentioned

If we loaded the partial object into a strongly typed object (in Java, say), then the object would have null all over the place where bits were missing.

If we wanted to take that loaded partial object and apply it to the data, we’d have a problem. Any null in that loaded version would be ambiguous. Would it mean:

  • Please set this field to null?
  • The request didn’t mention the field

Clearly you can’t load a partial into strong type, or a template of the whole object, and keep its meaning as a delta to an existing object.

Boolean?

This leads to another weird thought. Many people incorrectly think that boolean has two states:

  • True
  • False

While this is true, it’s quite common to find situations where there’s a hidden third state of null. Tri-state booleans are an anti-pattern, but if assembling data from multiple sources, or in cases where some fields are themselves conditional on others, it may be unsurprising to find a boolean value set to null.

It turns out that with the above, there’s a fourth state – not mentioned.

That’s ridiculous!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s