spring-cloud-stream icon indicating copy to clipboard operation
spring-cloud-stream copied to clipboard

Cannot Remove Headers in Message Handler Function

Open Nephery opened this issue 2 years ago • 0 comments

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:

  1. Go to https://github.com/spring-cloud/spring-cloud-stream-samples/tree/main/processor-samples/uppercase-transformer
  2. Upgrade spring-boot-starter-parent to 2.7.2
  3. Upgrade spring-cloud.version to 2021.0.3
  4. 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();
}
  1. 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();
	};
}
  1. Change the receive() sink function to:
@Bean
public Consumer<Message<String>> receive() {
	return msg -> logger.info("Data received: " + msg);
}
  1. Run the app with Rabbit MQ
  2. See in the receive() logs that the remove-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.

Nephery avatar Aug 12 '22 15:08 Nephery