-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Simplify RemoveJsonAttributesResponseBodyGatewayFilterFactory #3776
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
Merged
ryanjbaxter
merged 1 commit into
spring-cloud:4.1.x
from
raccoonback:refactor-json-field-removal-filter
May 13, 2025
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
/* | ||
* Copyright 2013-2020 the original author or authors. | ||
* Copyright 2013-2025 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. | ||
|
@@ -22,7 +22,6 @@ | |
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.node.ArrayNode; | ||
import com.fasterxml.jackson.databind.node.ObjectNode; | ||
import reactor.core.publisher.Mono; | ||
|
||
|
@@ -34,6 +33,7 @@ | |
|
||
/** | ||
* @author Marta Medio | ||
* @author raccoonback | ||
*/ | ||
public class RemoveJsonAttributesResponseBodyGatewayFilterFactory extends | ||
AbstractGatewayFilterFactory<RemoveJsonAttributesResponseBodyGatewayFilterFactory.FieldListConfiguration> { | ||
|
@@ -72,14 +72,14 @@ public GatewayFilter apply(FieldListConfiguration config) { | |
RewriteFunction<String, String> rewriteFunction = (exchange, body) -> { | ||
if (MediaType.APPLICATION_JSON.isCompatibleWith(exchange.getResponse().getHeaders().getContentType())) { | ||
try { | ||
JsonNode jsonBodyContent = mapper.readValue(body, JsonNode.class); | ||
JsonNode jsonNode = mapper.readValue(body, JsonNode.class); | ||
|
||
removeJsonAttribute(jsonBodyContent, config.getFieldList(), config.isDeleteRecursively()); | ||
removeJsonAttributes(jsonNode, config.getFieldList(), config.isDeleteRecursively()); | ||
|
||
body = mapper.writeValueAsString(jsonBodyContent); | ||
body = mapper.writeValueAsString(jsonNode); | ||
} | ||
catch (JsonProcessingException e) { | ||
throw new RuntimeException(e); | ||
return Mono.error(new IllegalStateException("Failed to process JSON of response body.", e)); | ||
} | ||
} | ||
return Mono.just(body); | ||
|
@@ -93,22 +93,12 @@ public GatewayFilter apply(FieldListConfiguration config) { | |
|
||
private ObjectMapper mapper = new ObjectMapper(); | ||
|
||
private void removeJsonAttribute(JsonNode jsonBodyContent, List<String> fieldsToRemove, boolean deleteRecursively) { | ||
if (deleteRecursively) { | ||
for (JsonNode jsonNode : jsonBodyContent) { | ||
if (jsonNode instanceof ObjectNode) { | ||
((ObjectNode) jsonNode).remove(fieldsToRemove); | ||
removeJsonAttribute(jsonNode, fieldsToRemove, true); | ||
} | ||
if (jsonNode instanceof ArrayNode) { | ||
for (JsonNode node : jsonNode) { | ||
removeJsonAttribute(node, fieldsToRemove, true); | ||
} | ||
} | ||
} | ||
private void removeJsonAttributes(JsonNode jsonNode, List<String> fieldNames, boolean deleteRecursively) { | ||
if (jsonNode instanceof ObjectNode objectNode) { | ||
objectNode.remove(fieldNames); | ||
} | ||
if (jsonBodyContent instanceof ObjectNode) { | ||
((ObjectNode) jsonBodyContent).remove(fieldsToRemove); | ||
if (deleteRecursively) { | ||
jsonNode.forEach(childNode -> removeJsonAttributes(childNode, fieldNames, true)); | ||
Comment on lines
+96
to
+101
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Modified the logic to recursively remove JSON nodes without type distinction, allowing recursive calls regardless of node type. |
||
} | ||
} | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The caller wraps the error with
Mono.error()
to preserve the reactive pipeline and allow proper error handling.The exception type has been explicitly changed to
IllegalStateException
.