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

JsonView with ExceptionHandler throws 500 error in 4.1.2 #9975

Closed
kevind-wgu opened this issue Oct 12, 2023 · 0 comments
Closed

JsonView with ExceptionHandler throws 500 error in 4.1.2 #9975

kevind-wgu opened this issue Oct 12, 2023 · 0 comments

Comments

@kevind-wgu
Copy link

Expected Behavior

I call a rest method with the @JSONVIEW annotation. The method throws an error. The error gets handled by my error handler and returns the appropriate response status and body.

Actual Behaviour

Instead of my handled error response being returned I get a 500 error instead. In the logs I see an exception is thrown saying that the the JsonError is incompatible with the expected return type.

09:07:08.770 [io-executor-thread-3] ERROR i.m.http.server.RouteExecutor - Unexpected error occurred: Error encoding object [Not Found] to JSON: Incompatible types: declared root type ([simple type, class test_email_validation.MyObj]) vs `io.micronaut.http.hateoas.JsonError`
io.micronaut.http.codec.CodecException: Error encoding object [Not Found] to JSON: Incompatible types: declared root type ([simple type, class test_email_validation.MyObj]) vs `io.micronaut.http.hateoas.JsonError`
	at io.micronaut.json.codec.MapperMediaTypeCodec.encode(MapperMediaTypeCodec.java:268)
	at io.micronaut.http.server.netty.jackson.JsonViewServerFilter.lambda$doFilter$1(JsonViewServerFilter.java:99)
	at reactor.core.publisher.MonoCallable.call(MonoCallable.java:72)
	at reactor.core.publisher.FluxSubscribeOnCallable$CallableSubscribeOnSubscription.run(FluxSubscribeOnCallable.java:227)
	at io.micronaut.core.propagation.PropagatedContext.lambda$wrap$3(PropagatedContext.java:211)
	at reactor.core.scheduler.SchedulerTask.call(SchedulerTask.java:68)
	at reactor.core.scheduler.SchedulerTask.call(SchedulerTask.java:28)
	at io.micronaut.core.propagation.PropagatedContext.lambda$wrap$4(PropagatedContext.java:228)
	at io.micronaut.core.propagation.PropagatedContext.lambda$wrap$4(PropagatedContext.java:228)
	at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
	at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136)
	at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)
	at java.base/java.lang.Thread.run(Thread.java:833)
Caused by: com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Incompatible types: declared root type ([simple type, class test_email_validation.MyObj]) vs `io.micronaut.http.hateoas.JsonError`
	at com.fasterxml.jackson.databind.exc.InvalidDefinitionException.from(InvalidDefinitionException.java:77)
	at com.fasterxml.jackson.databind.SerializerProvider.reportBadDefinition(SerializerProvider.java:1308)
	at com.fasterxml.jackson.databind.SerializerProvider._reportIncompatibleRootType(SerializerProvider.java:1399)
	at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:380)
	at com.fasterxml.jackson.databind.ObjectWriter$Prefetch.serialize(ObjectWriter.java:1568)
	at com.fasterxml.jackson.databind.ObjectWriter._writeValueAndClose(ObjectWriter.java:1273)
	at com.fasterxml.jackson.databind.ObjectWriter.writeValueAsBytes(ObjectWriter.java:1163)
	at io.micronaut.jackson.databind.JacksonDatabindMapper.writeValueAsBytes(JacksonDatabindMapper.java:209)
	at io.micronaut.json.codec.MapperMediaTypeCodec.encode(MapperMediaTypeCodec.java:265)
	... 12 common frames omitted

Steps To Reproduce

  1. Create a GET rest endpoint
  2. Annotate rest endpoint with JSON view and the appropriate view class
  3. rest endpoint should declare a return type other than void.
  4. Throw a custom exception
  5. Create a ExceptionHandler for the custom exception
  6. ExceptionHandler should generate a response with a body of type JsonError.
  7. Startup the application, hit the endpoint that throws the custom exception. This should result in a 500 error because JsonError is not of the same type as the rest endpoint return type.

Environment Information

  • Mac
  • Java 17
  • Maven 3.9.4
  • Micronaut 4.1.2

Example Application

https://github.com/kevind-wgu/micronaut-jsonview-bug

Version

4.1.2

yawkat added a commit that referenced this issue Oct 25, 2023
As the failed response contains a body not returned by the controller method, don't try to apply the controller's JsonView to it.

Fixes #9975
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant