Open Sectan opened 11 months ago
The reason for this is that the BaseMoney is an interface and it has two implementations Money and HighPrecisionMoney. The Deserializer can't distinguish them as you didn't "cast" the request to either money or highprecision money
projectRoot
.graphql()
.query(GraphQL.products(q -> q.limit(8))
.projection(p -> p.total().results().id().masterData().current().masterVariant().prices().value().onMoney().centAmount()))
.executeBlocking()
.getBody();
Adding onMoney()
to the projection will request the __typeName field so the ObjectMapper can deserialize correctly.
Another possibility is to declare a DefaultImplementation for the ObjectMapper. This can be done by creating a Mixin class
import com.commercetools.graphql.api.types.Money;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
@JsonTypeInfo(
use = JsonTypeInfo.Id.NAME,
include = JsonTypeInfo.As.PROPERTY,
property = "__typename",
defaultImpl = Money.class // use this implementation if no subtype implementation can be found
)
public interface BaseMoneyMixin {
}
Then create Jackson module in your application:
import com.commercetools.graphql.api.types.BaseMoney;
import com.fasterxml.jackson.databind.module.SimpleModule;
public class BaseMoneyModule extends SimpleModule {
private static final long serialVersionUID = 0L;
public BaseMoneyModule() {
setMixInAnnotation(BaseMoney.class, BaseMoneyMixin.class);
}
}
And registering a service locator entry in your resources folder (resources/META-INF/services/com.fasterxml.jackson.databind.module.SimpleModule
):
com.commercetools.graphql.api.BaseMoneyModule
The ObjectMapper of the SDK will load this module and register it, so that in case no projection implementation was choosen it defaults to Money.
Describe the bug Querying price information of a product with the graphql-api results in deserialization errors:
Caused by: com.fasterxml.jackson.databind.exc.InvalidTypeIdException: Could not resolve subtype of [simple type, class com.commercetools.graphql.api.types.BaseMoney]: missing type id property '__typename' (for POJO property 'value')
To Reproduce
Expected behavior Reproducer code should run and price information should be deserialized without an error.
Screenshots/Code snippet Following error is thrown: Caused by: com.fasterxml.jackson.databind.exc.InvalidTypeIdException: Could not resolve subtype of [simple type, class com.commercetools.graphql.api.types.BaseMoney]: missing type id property '__typename' (for POJO property 'value')
Stack information (please complete the following information):
Additional context NA