-
Notifications
You must be signed in to change notification settings - Fork 789
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Inconsistent Pageable Sort serialization between openfeign and spring mvc #675
Comments
Hello @philippeu, thanks for reporting this. I was able to reproduce the issue. Will work on it. |
@philippeu Have looked into this further. Actually, the data that comes from the sort json of this kind: |
Hi Olga, I have a service returning Pageable.
This service is calling another one via a feign client which is also returning a Pageable
once feign jackson is enabled, it registered the the
so services having feign jackson enabled do not provide the same response format (sort is serialized as an array) than the ones having feign jackson disabled (sort is serialized as an object). I work around that by providing a |
@philippeu Could you please provide the code of |
Here they are: public class CustomSortJacksonModule extends SortJacksonModule {
@Override
public String getModuleName() {
return "SortModule";
}
@Override
public Version version() {
return new Version(0, 1, 0, "", null, null);
}
@Override
public void setupModule(SetupContext context) {
var serializers = new SimpleSerializers();
serializers.addSerializer(Sort.class, new CustomSortJsonComponent.SortSerializer());
context.addSerializers(serializers);
var deserializers = new SimpleDeserializers();
deserializers.addDeserializer(Sort.class, new CustomSortJsonComponent.SortDeserializer());
context.addDeserializers(deserializers);
}
} public class CustomSortJsonComponent {
public static class SortSerializer extends JsonSerializer<Sort> {
private record CustomSort(boolean empty, boolean sorted, boolean unsorted) {};
@Override
public void serialize(Sort value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
gen.writeObject(new CustomSort(value.isEmpty(), value.isSorted(), value.isUnsorted()));
}
@Override
public Class<Sort> handledType() {
return Sort.class;
}
}
public static class SortDeserializer extends JsonDeserializer<Sort> {
@Override
public Sort deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
var treeNode = jsonParser.getCodec().readTree(jsonParser);
if (treeNode.isArray()) {
var arrayNode = (ArrayNode) treeNode;
var orders = new ArrayList<Sort.Order>();
arrayNode.forEach(jsonNode -> orders.add(new Sort.Order(Sort.Direction.valueOf(jsonNode.get("direction").textValue()), jsonNode.get("property").textValue())));
return Sort.by(orders);
}
return null;
}
@Override
public Class<Sort> handledType() {
return Sort.class;
}
}
} |
a better solution would be to have an specific object mapper for feign not interfering with spring one maybe. |
@philippeu - thanks for providing the samples. I thought you meant deserialising, cause when a |
ok, thanks Olga! |
Hi @philippeu @OlgaMaciaszek I am facing this same issue. Is there any other development here -
|
spring serializes
Sort
as an object"sort": { "empty": true, "sorted": false, "unsorted": true }
when
SortJacksonModule
is enabled (feign.autoconfiguration.jackson.enabled: true
)Sort
is serialize as an array bySortJsonComponent.SortSerializer
."sort": []
since
SortJacksonModule
is in the spring context the controller response includes the Sort array creating inconsistency between the controller response with and withoutSortJacksonModule
The text was updated successfully, but these errors were encountered: