Open hossain-khan opened 2 years ago
I think using the default adapter in that case seems reasonable. @swankjesse @JakeWharton thoughts?
I have talked to server-side engineers to make sure the labelKey
is always present for polymorphic type responses.
In case of UnknownMessage
, server-side could likely implement a specific typed object to represent it.
{
"type": "unknown"
}
I am going to close this request for now until a new use-case comes in from different individual.
@hossain-khan Hi, do you want to reopen this, so we don't have multiple Issues tracking the same thing? I'm interested.
Three years ago I did something along the lines of
/**
* <p>Eugen changed: Support {@code null} label and absent label key.
*/
public final class PolymorphicJsonAdapterFactory<T> implements JsonAdapter.Factory {
private static final String FAKE_NULL_LABEL = "__FAKE_NULL_LABEL__";
// ...
}
PolymorphicJsonAdapterFactory.of(JsonBasicPlan::class.java, "version")
.withSubtype(JsonBasicPlan1::class.java, null)
.withSubtype(JsonBasicPlan2::class.java, "2")
Reopening this as I think this is a valid use case
Hi all! Is there any workaround or plan to achieve this? I'm facing this issue and we do not control what the backend sends us. Sometimes, they send an empty object without the key and our decode fails.
I am wondering if this is still something we'd like to support in the lib
Summary
I was trying to use the PolymorphicJsonAdapterFactory, and the usage is very straightforward, as seen in test:
However, in non-ideal world, the backend server may not always send the expected
labelKey
. In that case, instead of failing, I wanted to leverage thewithFallbackJsonAdapter
API to return a different subtype.However, when the
labelKey
is missing, it throwsJsonDataException: Missing label for type
, which would result in failing the API request.Provided a unit-test below that explains this further. I was wondering if allowing the provided
withFallbackJsonAdapter
to handle such case would be an acceptable use case?Test that reflects the ask
I have added a test in PolymorphicJsonAdapterFactoryTest to showcase what is expected