micronaut-projects / micronaut-core

Micronaut Application Framework
http://micronaut.io
Apache License 2.0
6.07k stars 1.07k forks source link

Jackson serialiser/deserialiser improvement #2580

Open tamershahin opened 4 years ago

tamershahin commented 4 years ago

feature request coming from: https://gitter.im/micronautfw/Lobby?at=5e09b8080c3aeb107d76a4e2

Task List

Steps to Reproduce

I have POJO like:

public class MyParking {
    private MyAirport airport;
}

and a client

@Get("/v1/parkings")
    SearchResponse<MyParking> fetchParkings(..)

and

@Factory
public class AirportFactory {

    @Singleton
    TypeConverter<MyAirport, String> myAirportStringTypeConverter() {
        return (object, targetType, context) -> Optional.of(object.getI());
    }

    @Singleton
    TypeConverter<String, MyAirport> stringMyAirportTypeConverter() {
        return (object, targetType, context) -> {
            MyAirport m = new MyAirport();
            return Optional.of(m);
        };
    }
}

but the singletons are not called when it's time to do the conversion for payload marshalled by the @Client. The only way to instruct jackson about how to do the deserialisation is to register a new jackson module + serialiser/deserialiser

StackTrace

executed with error: Error decoding HTTP response body: Error decoding JSON stream for type [class com.yiluhub.search.model.SearchResponse]: Cannot construct instance of `parking.promotion.collector.searchengine.MyAirport` (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('TXL')

Expected Behaviour

the conversion should be more straight forwards (see SpringBoot approach that relies on org.springframework.core.convert.converter.Converter for all scenarios) OR better documented, it's not clear at all in current documentation (https://docs.micronaut.io/latest/guide/index.html#_beans)

Actual Behaviour

TypeConverter is not used at all for json deserialiser

Environment Information

Example Application

not needed

graemerocher commented 4 years ago

JSON serialization is not the same as type conversion. You can register Jackson serializer/deserializers for this case

tamershahin commented 4 years ago

I agree is not exactly the same, but nevertheless an annotation or base class alike TypeConverter to give the MN user the same feature present in SB would be cleaner and will avoid a bit of manual plumbing needed to register the serializer/deserializer.

if you still think it's not relevant or not a priority I'd recommend to provide more examples and explain better the difference between JSON serialization and type conversion in the doc.. for devs coming from SB background can be misleading