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

minor improvement to DefaultKafkaHeaderMapper #2940

Conversation

NathanQingyangXu
Copy link
Contributor

see self-comments for the rationale behind the changes. Feel free to modify and revert back inappropriate change.

@@ -286,20 +290,19 @@ public void fromHeaders(MessageHeaders headers, Headers target) {
}
if (!encodeToJson && valueToAdd instanceof String) {
target.add(new RecordHeader(key, ((String) valueToAdd).getBytes(getCharset())));
className = JAVA_LANG_STRING;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems the previous logic has ensured the className is assigned JAVA_LANG_STRING if valueToAdd has been String.

private Map<String, String> decodeJsonTypes(Headers source) {
Map<String, String> types = null;
Map<String, String> types = Collections.emptyMap();
Copy link
Contributor Author

@NathanQingyangXu NathanQingyangXu Dec 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is common good practise to return empty colletion as opposed to null to avoid NPE as per Effective Java. There is exception, but in this scenario it seems perfectly valid

}
catch (IOException e) {
logger.error(e, () -> "Could not decode json types: " + new String(jsonTypes.value()));
logger.error(e, () -> "Could not decode json types: " + new String(jsonTypes.value(), StandardCharsets.UTF_8));
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

json serialization result is always of UTF-8 charset

@@ -355,8 +358,7 @@ private void populateJsonValueHeader(Header header, String requestedType, Map<St
}
catch (IOException e) {
logger.error(e, () ->
"Could not decode json type: " + new String(header.value()) + " for key: "
+ header.key());
"Could not decode json type: " + requestedType + " for key: " + header.key());
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems the intention was type, not header value

@@ -321,7 +324,7 @@ else if (headerName.equals(KafkaHeaders.LISTENER_INFO) && matchesForInbound(head
headers.put(headerName, new String(header.value(), getCharset()));
}
else if (!(headerName.equals(JSON_TYPES)) && matchesForInbound(headerName)) {
if (jsonTypes != null && jsonTypes.containsKey(headerName)) {
if (jsonTypes.containsKey(headerName)) {
String requestedType = jsonTypes.get(headerName);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we might well improve perf by using the following pattern:

String jsonType = jsonTypes.get(headerName);
if (jsonType != null) {
    ... ...
}

for we end up one map query, rather than twice (containsKey() and get()).

@@ -142,7 +139,7 @@ public DefaultKafkaHeaderMapper(ObjectMapper objectMapper) {
* @see org.springframework.util.PatternMatchUtils#simpleMatch(String, String)
*/
public DefaultKafkaHeaderMapper(String... patterns) {
this(new ObjectMapper(), patterns);
this(JacksonUtils.enhancedObjectMapper(), patterns);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems we should change above to maintain consistency in the class

@sobychacko sobychacko added this to the 3.1.1 milestone Dec 15, 2023
@sobychacko sobychacko merged commit a57c319 into spring-projects:main Dec 15, 2023
2 checks passed
@sobychacko
Copy link
Contributor

Thanks, @NathanQingyangXu, for the PR. It is now merged upstream.

@NathanQingyangXu NathanQingyangXu deleted the improve-DefaultKafkaHeaderMapper branch December 15, 2023 20:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants