Skip to content

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

Conversation

raccoonback
Copy link
Contributor

@raccoonback raccoonback commented Apr 19, 2025

Summary

This pull request refactors the filter responsible for removing fields from JSON-formatted response bodies.
Simplified the filtering logic for better readability, and improved internal method structure and naming.

Changes

  • The functionality to remove JSON attributes remains unchanged.
  • Exceptions during serialization and deserialization are now wrapped using Mono.error().
    This allows the calling side to handle errors explicitly within the Reactive pipeline.
    The change does not affect backward compatibility.
  • Exceptions during serialization and deserialization have been changed under IllegalStateException.
    I think using a more specific exception type would improve clarity.

Let me know if further adjustments are needed.

}
catch (JsonProcessingException e) {
throw new RuntimeException(e);
return Mono.error(new IllegalStateException("Failed to process JSON of response body.", e));
Copy link
Contributor Author

Choose a reason for hiding this comment

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

  1. The caller wraps the error with Mono.error() to preserve the reactive pipeline and allow proper error handling.

  2. The exception type has been explicitly changed to IllegalStateException.

Comment on lines +96 to +101
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));
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.

@ryanjbaxter
Copy link
Contributor

Can you submit this against the 4.1.x branch?

@raccoonback raccoonback changed the base branch from main to 4.1.x May 12, 2025 23:50
@raccoonback raccoonback force-pushed the refactor-json-field-removal-filter branch from c991ecd to 2458503 Compare May 12, 2025 23:56
@raccoonback
Copy link
Contributor Author

@ryanjbaxter I've changed the target to the 4.1.x branch.

@ryanjbaxter ryanjbaxter added this to the 4.1.8 milestone May 13, 2025
@ryanjbaxter ryanjbaxter merged commit 9413619 into spring-cloud:4.1.x May 13, 2025
2 checks passed
@github-project-automation github-project-automation bot moved this to Done in 2025.0.0 May 13, 2025
@github-project-automation github-project-automation bot moved this to Done in 2023.0.6 May 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Done
Status: No status
Status: Done
Development

Successfully merging this pull request may close these issues.

3 participants