155
Trying to understand JSON…
(sopuli.xyz)
Post funny things about programming here! (Or just rant about your favourite programming language.)
That sounds like an error in the specification of the client-server API or an erroneous implementation on the server side for the last version: nothing should be signaled via presence or absence of fields when using JSON exactly because, as I described in my last post, the standard with JSON is that stuff that is not present should be ignore (i.e. it has no meaning at all) for backwards compatibility, which breaks if all of the sudden presence or absence are treated as having meaning.
Frankly that there isn't a specific field signalling authorized/not-authorized leads me to believe that whomever has designed that API isn't exactly experienced at that level of software design: authorization information should be explicit, not implicit, otherwise you end up with people checking for not-in-spec side effects like you did exactly for that reason (i.e. "is the no data being returned because of user not authorized or because there was indeed no data to retunr?"), which is prone to break since not being properly part of the spec means any of the teams working on it might interpret things differently and/or change them at any moment.
The instance I was bitching about was this: There’s a lot of region-specific data coming from the backend. But the user is only authorized for certain regions. So for instance the North-American guy gets this object:
{ "CA": [/* list of stuff */], "US": [/* list of stuff */], "MX": [ /* list of stuff */ ]}
, while the US-only guy only gets{"US": [ /* list of stuff */] }
. Are you suggesting that the response should also include flagsisCaPresent
,isUsPresent
,isMxPresent
for every country?The issue with
null
vs not present surfaced because I, the frontend, checked if the returned object contained the key"CA"
and then tried to iterate over the list of stuff, which happened to be null, which is hard to iterate over. I agree that I could’ve checked if the key was present and not null.The meme, however, was lamenting that the backend developer, refuses to acknowledge that these two JSONs are different,since they only see their POJOs, where both map to
CA: null, US: [], MX: null
.If it's part of the Requirements that the frontend should handle "No results found" differently from "Not authorized", even if that's just by showing an icon, then ach list of stuff which might or not be authorized should have a flag signalling that.
(This is simply data analysis - if certain information is supposed to be shown to the user it should come from somewhere and hence the frontend must get it from somewhere, and the frontend code trying to "deduce it" from data it gets is generally prone to the kind of problem you just got because unless explicitly agreed and documented, sooner or later some deduction done by one team is not going to match what the other team is doing. Generally it's safer just to explicitly pass that info in a field for that purpose to avoid frontend-backend integration issues).
Authorization logic is almost always a responsibility of the backend (for various reasons, including proper security practices) and for the frontend it's generally irrelevant why it's authorized or not, unless you have to somehow display per-list the reason for a it being authorized or not, which would be a strange UI design IMHO - generally there's but a flag in the main part of the UI and a separate page/screen with detailed authorization information - if the user really wants to dig down into the "why" - which would be using different API call just to fill in that page/screen.
So if indeed it is required that the frontend knows if an empty result is due to "Not Authorized" rather than "No results found" (a not uncommon design, though generally a good UI design practice is to simply not even give the user access to listing things the user is not authorized to see rather than let the user chose them and then telling them they're not authorized to do it, as the latter design is more frustrating for users) that info should be an explicit entry in what comes from the backend.
The JSON is indeed different in both cases, but if handled correctly it shouldn't matter.
That said, IMHO, if all those 3 fields in your example should be present, the backend should be putting a list on all 3 fields even if for some the list is empty, rather than a null in some - it doesn't matter what the JSON is since even at the Java backend level, a List variable with a "null" is not the same as a List variable with a List of length 0 - null vs empty list is quite a common source of mistakes even within the code of just the one tier, though worse if it ends up in API data.
Who is wrong or right ultimately depends on the API design having marked those fields as mandatory or optional.