-
Notifications
You must be signed in to change notification settings - Fork 618
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
SmartCompositeMessageConverter swallows conversion errors for simple types #1168
Comments
@LukeKynaston Can you paste the unit test here? That would make things easier to debug. Thanks! |
Hi @sobychacko, I meant that you can write a unit test in the
|
I think this issue belongs in the Spring Cloud Function repo. Moving the issue from Spring Cloud Stream to here. cc @LukeKynaston |
This is related to #739 and is an unfortunate choice of default behavior. In my case I'm using function routing based on a message header and have each function accept a specific record that's deserialized from JSON. If the payload is malformed I want the message to not even touch the function and go to the configured error handler (I'm using Spring Cloud Stream) but instead I'm receiving a Message<byte[]> which gets me ClassCastException as soon as I call getPayload(). |
Well, this is a default behavior and the intention is to allow user to receive a raw message by changing function signature (even temporarily), primarily to investigate. This is because conversion errors are usually development-type errors and could be fixed in many different ways including by adding your own MessageConverter to the stack - https://docs.spring.io/spring-cloud-function/reference/spring-cloud-function/programming-model.html#user-defined-message-converters So what would you propose to change? |
I think the better thing to do is to let the configured error handler (as in Consumer) process those, which is what happens anyway once the casting exception happens. The main difference is that the user-defined function wouldn't get called with incompatible argument type. |
@LukeKynaston Please look at this commit. The issue has been resolved similarly to what you are describing -
We provide a default one, but as you can see the Please let me know if that is sufficient or you were looking for something else |
@olegz, Thanks I think this will resolve our issue :). I'll check with the team before closing this, just to make sure everyone is happy with this solution. |
Actual Behaviour:
When a message is received and the consumer accepts a simple type i.e. a raw Map without generics information, any conversion errors will be swallowed.
org.springframework.cloud.function.context.catalog.SimpleFunctionRegistry.FunctionInvocationWrapper#convertInputMessageIfNecessary
will callSmartCompositeMessageConverter#fromMessage(Message<?> message, Class<?> targetClass)
when no convertionHint is required. This results in errors thrown by any of the converters being swallowed as the Exception is caught, a debug message logged, and null is returned. Once the list of converters is exhausted, null is returned to the caller and theMessage#payload
is passed to the consumer as is (e.g. byte[]), this then results in aClassCastException
being raised.Expected Behaviour:
SmartCompositeMessageConverter#fromMessage(Message<?> message, Class<?> targetClass)
should propagate the Exception just likeSmartCompositeMessageConverter#fromMessage(Message<?> message, Class<?> targetClass, @Nullable Object conversionHint)
so that message conversion errors are handled consistently.Affects versions: 3.x, 4.x
The issue can be recreated with a simple unit test.
The text was updated successfully, but these errors were encountered: