Skip to content

Commit 4c4fb69

Browse files
committed
Merge branch '4.2.x'
2 parents 3e0f0cd + 2fccda7 commit 4c4fb69

File tree

2 files changed

+53
-27
lines changed

2 files changed

+53
-27
lines changed

spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/filter/factory/RemoveJsonAttributesResponseBodyGatewayFilterFactory.java

+11-21
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013-2020 the original author or authors.
2+
* Copyright 2013-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -22,7 +22,6 @@
2222
import com.fasterxml.jackson.core.JsonProcessingException;
2323
import com.fasterxml.jackson.databind.JsonNode;
2424
import com.fasterxml.jackson.databind.ObjectMapper;
25-
import com.fasterxml.jackson.databind.node.ArrayNode;
2625
import com.fasterxml.jackson.databind.node.ObjectNode;
2726
import reactor.core.publisher.Mono;
2827

@@ -34,6 +33,7 @@
3433

3534
/**
3635
* @author Marta Medio
36+
* @author raccoonback
3737
*/
3838
public class RemoveJsonAttributesResponseBodyGatewayFilterFactory extends
3939
AbstractGatewayFilterFactory<RemoveJsonAttributesResponseBodyGatewayFilterFactory.FieldListConfiguration> {
@@ -72,14 +72,14 @@ public GatewayFilter apply(FieldListConfiguration config) {
7272
RewriteFunction<String, String> rewriteFunction = (exchange, body) -> {
7373
if (MediaType.APPLICATION_JSON.isCompatibleWith(exchange.getResponse().getHeaders().getContentType())) {
7474
try {
75-
JsonNode jsonBodyContent = mapper.readValue(body, JsonNode.class);
75+
JsonNode jsonNode = mapper.readValue(body, JsonNode.class);
7676

77-
removeJsonAttribute(jsonBodyContent, config.getFieldList(), config.isDeleteRecursively());
77+
removeJsonAttributes(jsonNode, config.getFieldList(), config.isDeleteRecursively());
7878

79-
body = mapper.writeValueAsString(jsonBodyContent);
79+
body = mapper.writeValueAsString(jsonNode);
8080
}
8181
catch (JsonProcessingException e) {
82-
throw new RuntimeException(e);
82+
return Mono.error(new IllegalStateException("Failed to process JSON of response body.", e));
8383
}
8484
}
8585
return Mono.just(body);
@@ -93,22 +93,12 @@ public GatewayFilter apply(FieldListConfiguration config) {
9393

9494
private ObjectMapper mapper = new ObjectMapper();
9595

96-
private void removeJsonAttribute(JsonNode jsonBodyContent, List<String> fieldsToRemove, boolean deleteRecursively) {
97-
if (deleteRecursively) {
98-
for (JsonNode jsonNode : jsonBodyContent) {
99-
if (jsonNode instanceof ObjectNode) {
100-
((ObjectNode) jsonNode).remove(fieldsToRemove);
101-
removeJsonAttribute(jsonNode, fieldsToRemove, true);
102-
}
103-
if (jsonNode instanceof ArrayNode) {
104-
for (JsonNode node : jsonNode) {
105-
removeJsonAttribute(node, fieldsToRemove, true);
106-
}
107-
}
108-
}
96+
private void removeJsonAttributes(JsonNode jsonNode, List<String> fieldNames, boolean deleteRecursively) {
97+
if (jsonNode instanceof ObjectNode objectNode) {
98+
objectNode.remove(fieldNames);
10999
}
110-
if (jsonBodyContent instanceof ObjectNode) {
111-
((ObjectNode) jsonBodyContent).remove(fieldsToRemove);
100+
if (deleteRecursively) {
101+
jsonNode.forEach(childNode -> removeJsonAttributes(childNode, fieldNames, true));
112102
}
113103
}
114104

spring-cloud-gateway-server/src/test/java/org/springframework/cloud/gateway/filter/factory/RemoveJsonAttributesResponseBodyGatewayFilterFactoryTests.java

+42-6
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.util.Map;
2020

2121
import org.junit.jupiter.api.Test;
22+
import reactor.core.publisher.Mono;
2223

2324
import org.springframework.beans.factory.annotation.Value;
2425
import org.springframework.boot.SpringBootConfiguration;
@@ -30,8 +31,12 @@
3031
import org.springframework.context.annotation.Bean;
3132
import org.springframework.context.annotation.Import;
3233
import org.springframework.http.HttpHeaders;
34+
import org.springframework.http.HttpStatus;
3335
import org.springframework.http.MediaType;
36+
import org.springframework.http.ResponseEntity;
3437
import org.springframework.test.annotation.DirtiesContext;
38+
import org.springframework.web.bind.annotation.ControllerAdvice;
39+
import org.springframework.web.bind.annotation.ExceptionHandler;
3540

3641
import static org.assertj.core.api.Assertions.assertThat;
3742
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
@@ -42,10 +47,10 @@
4247
*/
4348
@SpringBootTest(webEnvironment = RANDOM_PORT)
4449
@DirtiesContext
45-
public class RemoveJsonAttributesResponseBodyGatewayFilterFactoryTests extends BaseWebClientTests {
50+
class RemoveJsonAttributesResponseBodyGatewayFilterFactoryTests extends BaseWebClientTests {
4651

4752
@Test
48-
public void removeJsonAttributeRootWorks() {
53+
void removeJsonAttributeRootWorks() {
4954
testClient.post()
5055
.uri("/post")
5156
.header("Host", "www.removejsonattributes.org")
@@ -70,8 +75,7 @@ public void removeJsonAttributeRootWorks() {
7075
}
7176

7277
@Test
73-
public void removeJsonAttributeRecursivelyWorks() {
74-
78+
void removeJsonAttributeRecursivelyWorks() {
7579
testClient.post()
7680
.uri("/post")
7781
.header("Host", "www.removejsonattributesrecursively.org")
@@ -93,8 +97,7 @@ public void removeJsonAttributeRecursivelyWorks() {
9397
}
9498

9599
@Test
96-
public void removeJsonAttributeNoMatchesWorks() {
97-
100+
void removeJsonAttributeNoMatchesWorks() {
98101
testClient.post()
99102
.uri("/post")
100103
.header("Host", "www.removejsonattributesnomatches.org")
@@ -113,6 +116,21 @@ public void removeJsonAttributeNoMatchesWorks() {
113116
});
114117
}
115118

119+
@Test
120+
void raisedWhenRemoveJsonAttributes() {
121+
testClient.post()
122+
.uri("/post")
123+
.header("Host", "www.raisederrorwhenremovejsonattributes.org")
124+
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
125+
.exchange()
126+
.expectStatus()
127+
.is5xxServerError()
128+
.expectBody(String.class)
129+
.consumeWith(result -> {
130+
assertThat(result.getResponseBody()).isEqualTo("Failed to process JSON of response body.");
131+
});
132+
}
133+
116134
@EnableAutoConfiguration
117135
@SpringBootConfiguration
118136
@Import(DefaultTestConfig.class)
@@ -142,9 +160,27 @@ public RouteLocator testRouteLocator(RouteLocatorBuilder builder) {
142160
.host("{sub}.removejsonattributesnomatches.org")
143161
.filters(f -> f.removeJsonAttributes("test"))
144162
.uri(uri))
163+
.route("raised_error_when_remove_json_attributes",
164+
r -> r.path("/post")
165+
.and()
166+
.host("{sub}.raisederrorwhenremovejsonattributes.org")
167+
.filters(f -> f.removeJsonAttributes("test")
168+
.modifyResponseBody(String.class, String.class,
169+
(exchange, response) -> Mono.just("{\"invalid_json\": test")))
170+
.uri(uri))
145171
.build();
146172
}
147173

174+
@ControllerAdvice
175+
public class GlobalExceptionHandler {
176+
177+
@ExceptionHandler(IllegalStateException.class)
178+
public ResponseEntity<String> handleIllegalException(IllegalStateException ex) {
179+
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(ex.getMessage());
180+
}
181+
182+
}
183+
148184
}
149185

150186
}

0 commit comments

Comments
 (0)