spring-cloud-stream
spring-cloud-stream copied to clipboard
Cannot Remove Headers in Message Handler Function
Describe the issue
Headers removed within a Function<Message<String>, Message<String>>
are re-added to the output message by SimpleFunctionRegistry#enrichInvocationResultIfNecessary()
.
To Reproduce Steps to reproduce the behavior:
- Go to https://github.com/spring-cloud/spring-cloud-stream-samples/tree/main/processor-samples/uppercase-transformer
- Upgrade
spring-boot-starter-parent
to2.7.2
- Upgrade
spring-cloud.version
to2021.0.3
- Change the
sendTestData()
source function to:
@Bean
public Supplier<Message<String>> sendTestData() {
return () -> MessageBuilder.withPayload(this.semaphore.getAndSet(!this.semaphore.get()) ? "foo" : "bar")
.setHeader("remove-me", "foo")
.build();
}
- Change the
transform()
processor function to:
@Bean
public Function<Message<String>, Message<String>> transform() {
return msg -> {
logger.info("transform: " + msg);
return MessageBuilder.withPayload(msg.getPayload().toUpperCase())
.copyHeaders(msg.getHeaders())
.removeHeader("remove-me")
.build();
};
}
- Change the
receive()
sink function to:
@Bean
public Consumer<Message<String>> receive() {
return msg -> logger.info("Data received: " + msg);
}
- Run the app with Rabbit MQ
- See in the
receive()
logs that theremove-me
header was not removed from the message.
Version of the framework Spring Boot: 2.7.2 Spring Cloud: 2021.0.3
Expected behavior
Input message headers should not be inherited by the output message if the function's return type is a Message
.
Additional context The issue also occurs between functions when using functional composition.