spring-cloud-gateway icon indicating copy to clipboard operation
spring-cloud-gateway copied to clipboard

[Spring Cloud Gateway MVC] Internal forwarded body rewrites umlauts

Open sapo-di opened this issue 1 year ago • 0 comments

Describe the bug

I am using spring-cloud-gateway-mvc in an existing application to forward any request starting with a specific route. Some of the requests should be passed to a downstream service and some should be forwarded to a different route in the same service.
If a POST request contains umlauts in the JSON body, forwarding it to the downstream service is fine. If it is forwarded internally the umlauts in the request body are rewritten.

Sample

@Controller
public class MyController {
    private static final Logger logger = LoggerFactory.getLogger(MyController.class);


    private final ObjectMapper objectMapper = new ObjectMapper();


    @RequestMapping(path = "/legacy-route")
    @ResponseBody
    public ResponseEntity<JsonNode> answer(@RequestBody Map<String,String> content) throws JsonProcessingException {
        logger.info(objectMapper.writeValueAsString(content));
        return ResponseEntity.ok(objectMapper.readTree(objectMapper.writeValueAsString(content)));
    }
}
@Controller
public class ProxyController {

    @RequestMapping(path = "/api/{*tail}")
    @ResponseBody
    public void useExchange(@PathVariable("tail") String tail, ProxyExchange<byte[]> exchange) {
        exchange.forward(tail);
    }

}
POST http://localhost:8080/api/legacy-route
Content-Type: application/json
Content-Length: 39
User-Agent: IntelliJ HTTP Client/IntelliJ IDEA 2024.2.1
Accept-Encoding: br, deflate, gzip, x-gzip
Accept: */*

{
  "id" : "unique",
  "umlaut": "ä"
}

###

HTTP/1.1 200 
Content-Type: application/json
Transfer-Encoding: chunked
Date: Tue, 24 Sep 2024 06:54:55 GMT

{
  "id": "unique",
  "umlaut": "ᅢᄂ"
}
Response file saved.
> 2024-09-24T085455.200.json

Response code: 200; Time: 521ms (521 ms); Content length: 29 bytes (29 B)

I provided an example here: https://github.com/sapo-di/gateway-mvc-test/

Failing test here: https://github.com/sapo-di/gateway-mvc-test/actions/runs/11009568757/job/30569503977

sapo-di avatar Sep 24 '24 12:09 sapo-di