spring-attic / spring-native

Spring Native is now superseded by Spring Boot 3 official native support
https://docs.spring.io/spring-boot/docs/current/reference/html/native-image.html
Apache License 2.0
2.74k stars 356 forks source link

Add support for reference chains in hints #1630

Closed edudar closed 1 year ago

edudar commented 2 years ago

I'm trying to run a native image with Kubernetes support via the fabric8 client. It loads a few model hierarchies like io.fabric8.kubernetes.api.model.Config, .Service, .EndpointsList, etc for operations. Each hierarchy is quite extensive, I tried listing classes via @SerializationHint, but it becomes too enduring and the list is way too long. I learned in #1569 that Spring Native scan classes in responses down the chain, so I added the following into one of the controllers:

    @GetMapping("/_system/_configs")
    public List<Config> configs() { return List.of(); }

    @GetMapping("/_system/_services")
    public List<Service> services() { return List.of(); }

    @GetMapping("/_system/_endpoints")
    public List<EndpointsList> endpoints() { return List.of(); }

It would be good to have such a feature in @SerializationHint(types = Config.class, chain = true) that will be false by default.

mhalbritter commented 2 years ago

I guess you need the chaining for reflection hints (@TypeHint) so that JSON serialization works, and not for @SerializationHint. @SerializationHint is for Java serialization (ObjectOutputStream, ...). Correct?

sdeleuze commented 2 years ago

FWIW Spring Framework 6 will provide some reusable helper to register the relevant type, not only in @RequestMapping context, but also applied to other ones.

edudar commented 2 years ago

@mhalbritter oops, my bad, mixed up serializations. That, however, seems to be a very niche feature for a rather generic TypeHint.

Bives89 commented 1 year ago

For example: @SerializationHint(types = Config.class, chain = false) public class MyController {

@GetMapping("/_system/_configs")
public List<Config> configs() { return List.of(); }

@GetMapping("/_system/_services")
public List<Service> services() { return List.of(); }

@GetMapping("/_system/_endpoints")
public List<EndpointsList> endpoints() { return List.of(); }

} This will serialize only the Config, Service, and EndpointsList types, and not their dependencies.

It's also worth noting that the @SerializationHint annotation is not used to specify which classes should be scanned by Spring Native. Spring Native will scan all classes that are reachable from the root object being serialized, regardless of whether they are annotated with @SerializationHint. If you want to exclude certain classes from being scanned, you can use the @NoScan annotation.

sdeleuze commented 1 year ago

Spring Native is now superseded by Spring Boot 3 official native support, see the related reference documentation for more details.

As a consequence, I am closing this issue, and recommend trying your use case with latest Spring Boot 3 version. If you still experience the issue reported here, please open an issue directly on the related Spring project (Spring Framework, Data, Security, Boot, Cloud, etc.) with a reproducer.

Thanks for your contribution on the experimental Spring Native project, we hope you will enjoy the official native support introduced by Spring Boot 3.