Let's say I have a REST API, that has the ability to provide field mask (i.e. the API can return M out of N attributes where M is a subset of N).
If a statically typed client (example: one that's written in Java) requests data from this API, is there a way to "gracefully" handle nullability issues.
If out of N fields, M are requested, then [N-M] fields are "null"...
If the serializing class has all of the M fields; then N-M will be null. If this class is passed around to other logic, the code is going to be peppered with "null" checks in order to avoid NPEs. (
Optionalarg types only minorly alleviate the issue).
The other option is to create different "data transfer objects(DTOs)" to handle various "common" field masks, and ensure that all attributes of these DTOs are non-null, so the calling code doesn't have to do null checks before every attribute access(example: I have 3 DTOs: SuperSimple, Simple, Detailed with preselected field masks). However; this can lead to DTO pollution in the code (potentially you could have 2^N such DTOs which is a drag on maintainability)
I'd appreciate your thoughts in form of a) design patterns b) frameworks features or c) standalone libs that target this problem