Skip to content

Commit

Permalink
GH-2805: StreamBridge send and custom content-type
Browse files Browse the repository at this point in the history
 - When StreamBridge#send is called with binder-name and custom content-type,
   it does not honor the content-type value, but default to application/json.
   Fixing this issue for this call path by explicitly checking for any custom
   content-type provided on the binding.

Resolves #2805
Resolves #2813
  • Loading branch information
sobychacko authored and olegz committed Sep 20, 2023
1 parent 316d393 commit 7e7688c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2020-2022 the original author or authors.
* Copyright 2020-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -268,10 +268,27 @@ void testWithOutputContentTypeWildCardBindings() throws Exception {
.isEqualTo(MimeType.valueOf("application/json+foo"));
assertThat(output.receive(1000, "bar").getHeaders().get(MessageHeaders.CONTENT_TYPE))
.isEqualTo(MimeType.valueOf("application/blahblah+non-registered-foo"));
}
}

// See this issue for more details: https://github.com/spring-cloud/spring-cloud-stream/issues/2805
@Test
void testStreamBridgeSendWithBinderNameAndCustomContentType() throws Exception {
try (ConfigurableApplicationContext context = new SpringApplicationBuilder(TestChannelBinderConfiguration
.getCompleteConfiguration(ConsumerConfiguration.class, EmptyConfigurationWithCustomConverters.class))
.web(WebApplicationType.NONE).run(
"--spring.cloud.stream.bindings.foo.content-type=application/*+foo")) {
StreamBridge bridge = context.getBean(StreamBridge.class);
bridge.send("foo", "test-binder", "hello foo");

OutputDestination output = context.getBean(OutputDestination.class);

assertThat(output.receive(1000, "foo").getHeaders().get(MessageHeaders.CONTENT_TYPE))
.isEqualTo(MimeType.valueOf("application/json+foo"));
}
}


@SuppressWarnings("unchecked")
@Test
void testNoCachingOfStreamBridgeFunction() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
import org.springframework.cloud.stream.binder.ProducerProperties;
import org.springframework.cloud.stream.binding.BindingService;
import org.springframework.cloud.stream.binding.NewDestinationBindingCallback;
import org.springframework.cloud.stream.config.BindingProperties;
import org.springframework.cloud.stream.config.BindingServiceProperties;
import org.springframework.cloud.stream.messaging.DirectWithAttributesChannel;
import org.springframework.context.ConfigurableApplicationContext;
Expand Down Expand Up @@ -136,8 +135,7 @@ protected boolean removeEldestEntry(Map.Entry<String, MessageChannel> eldest) {

@Override
public boolean send(String bindingName, Object data) {
BindingProperties bindingProperties = this.bindingServiceProperties.getBindingProperties(bindingName);
MimeType contentType = StringUtils.hasText(bindingProperties.getContentType()) ? MimeType.valueOf(bindingProperties.getContentType()) : MimeTypeUtils.APPLICATION_JSON;
var contentType = determineContentType(bindingName, this.bindingServiceProperties);
return this.send(bindingName, data, contentType);
}

Expand All @@ -147,7 +145,14 @@ public boolean send(String bindingName, Object data, MimeType outputContentType)
}
@Override
public boolean send(String bindingName, @Nullable String binderName, Object data) {
return this.send(bindingName, binderName, data, MimeTypeUtils.APPLICATION_JSON);
var contentType = determineContentType(bindingName, this.bindingServiceProperties);
return this.send(bindingName, binderName, data, contentType);
}

private static MimeType determineContentType(String bindingName, BindingServiceProperties bindingServiceProperties) {
var bindingProperties = bindingServiceProperties.getBindingProperties(bindingName);
return StringUtils.hasText(bindingProperties.getContentType()) ?
MimeType.valueOf(bindingProperties.getContentType()) : MimeTypeUtils.APPLICATION_JSON;
}

@Override
Expand Down

0 comments on commit 7e7688c

Please sign in to comment.