Inclusion of Null data values in serialized API output #3
-
Current API standards indicate that if a data value is null, it shouldn't be included in the deserialized output that a client would receive. This definitely has advantages and disadvantages, but there are few things that could be tricky:
One way to handle some of these concerns, per Nick Clarity (@nickclarity) is to allow for deserializing objects into different typed objects (using a custom parser and factory) based on what properties are in available in the payload. I'd love to get everyone's thoughts on this one, thanks a ton! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
I've talked about a few examples before in slack about why you might sometimes want to include null properties, but I can add an expanded use-case here. Example ProblemThe one case that I find most interesting is in testing API expansion. So let's consider an endpoint that fetches a dc4 company. {
"id": 10,
"name": "MyCoolCompany Inc",
"active": true
} But after a while of NCS being released, there was a desire to tie Identity orgs and dc4 companies together. They had been tied somewhat through Identity, but there was an architectural plan that showed that it worked better if the dc4 company itself had an {
"id": 10,
"name": "MyCoolCompany Inc",
"active": true,
"org_id": "121312341412442"
} But there's a problem, So, in trying to write tests that confirm the output of
The answer is that they are identical, there is actually no possible way to detect the serialization bug when org_id=null. {
"id": 10,
"name": "MyCoolCompany Inc",
"active": true
} Follow up thoughtsThe use-case above alone makes me think that if we had any general recommendation for APIs it would be that they always include nulls when it's reasonable. |
Beta Was this translation helpful? Give feedback.
I've talked about a few examples before in slack about why you might sometimes want to include null properties, but I can add an expanded use-case here.
Example Problem
The one case that I find most interesting is in testing API expansion.
We can use a real example from
network-configuration-service
as to why (for one reason at least) it returns nulls.So let's consider an endpoint that fetches a dc4 company.
So you make your
GET /company
endpoint, which has a few properties:But after a while of NCS being released, there was a desire to tie Identity orgs and dc4 companies together. They had been tied somewhat through Iden…