Skip to content
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

ClassCastException with JsonView and Optional in controller #11169

Closed
goeh opened this issue Jul 15, 2024 · 0 comments · Fixed by #11170
Closed

ClassCastException with JsonView and Optional in controller #11169

goeh opened this issue Jul 15, 2024 · 0 comments · Fixed by #11170
Labels
type: bug Something isn't working

Comments

@goeh
Copy link
Contributor

goeh commented Jul 15, 2024

Expected Behavior

A controller method with the following signature should return data:

    @JsonView(Views.Public.class)
    @Get("/optional")
    public Optional<User> getUserOptional() {
        return Optional.of(new User(1, "Joe", "secret"));
    }
public class Views {
    public static class Public {}
    public static class Secret extends Public {}
}

Actual Behaviour

java.lang.ClassCastException: class com.example.User cannot be cast to class java.util.Optional (com.example.User is in unnamed module of loader 'app'; java.util.Optional is in module java.base of loader 'bootstrap')
	at io.micronaut.serde.support.serdes.OptionalSerde$1.serialize(OptionalSerde.java:53)
	at io.micronaut.serde.jackson.JacksonJsonMapper.writeValue(JacksonJsonMapper.java:187)
	at io.micronaut.serde.jackson.JacksonJsonMapper.writeValueAsBytes(JacksonJsonMapper.java:285)
	at io.micronaut.json.codec.MapperMediaTypeCodec.encode(MapperMediaTypeCodec.java:265)
	at io.micronaut.http.server.netty.jackson.JsonViewServerFilter.lambda$doFilter$1(JsonViewServerFilter.java:102)
	at reactor.core.publisher.MonoCallable.call(MonoCallable.java:72)
	at reactor.core.publisher.FluxSubscribeOnCallable$CallableSubscribeOnSubscription.run(FluxSubscribeOnCallable.java:228)

If I comment out the JsonView annotation, the exception is not thrown.

Steps To Reproduce

  1. Annotate an optional controller method with JsonView
  2. Send a http request to the endpoint
  3. See ClassCastException

Environment Information

  • MacOS 14.5
  • JDK "21.0.2" 2024-01-16 LTS

Example Application

https://github.com/goeh/micronaut-jsonview-issue

Version

4.5.0

@dstepanov dstepanov transferred this issue from micronaut-projects/micronaut-serialization Sep 9, 2024
graemerocher pushed a commit that referenced this issue Sep 9, 2024
@graemerocher graemerocher added the type: bug Something isn't working label Sep 9, 2024
@graemerocher graemerocher moved this to Done in 4.6.2 Release Sep 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug Something isn't working
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

2 participants