snowdrop-zen / quarkus

Quarkus: Supersonic Subatomic Java.
https://quarkus.io
Apache License 2.0
1 stars 0 forks source link

Spring MVC @RequestParam encoding problem on UTF-8 #115

Closed snowdrop-bot closed 4 years ago

snowdrop-bot commented 4 years ago

I develop a rest API that serves city information for users. But I'm having problems with @RequestParam charset. I sent some params to the controller which includes Turkish characters. And it didn't work as expected.

Some of the characters that I have Turkish character problem:

If you want to check that source code of this project: https://github.com/enesusta/tzone/tree/develop

Here is my request

http://localhost:8080/district?name=diyarbakı

Here is our controller

import com.github.enesusta.tzone.district.service.DistrictsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.Executor;

@RestController
public class DistrictsController {

    @Autowired
    DistrictsService districtsService;

    @Autowired
    Executor executor;

    @GetMapping("/districts")
    public CompletionStage<List<Districts>> getDistricts() {
        return CompletableFuture.supplyAsync(districtsService.getDistricts(), executor);
    }

    @GetMapping(value = "/district", produces = "application/json;charset=utf-8")
    public CompletionStage<List<Districts>> getDistrictsByName(@RequestParam(name = "name") String districtName) throws UnsupportedEncodingException {
        return CompletableFuture.supplyAsync(districtsService.findDistrictsByCityName(districtName), executor);
    }
}

Here is our service:

import com.github.enesusta.tzone.district.Districts;
import com.github.enesusta.tzone.district.repository.DistrictsRepository;
import org.jboss.logging.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Locale;
import java.util.function.Supplier;

@Service
public class DefaultDistrictsService implements DistrictsService {

    private final static Logger logger = Logger.getLogger(DefaultDistrictsService.class);

    @Autowired
    DistrictsRepository districtsRepository;

    @Autowired
    Locale locale;

    @Override
    public Supplier<List<Districts>> getDistricts() {
        return () -> districtsRepository.getDistricts();
    }

    @Override
    public Supplier<List<Districts>> findDistrictsByCityName(String cityName) throws UnsupportedEncodingException {
        final String encodedParam = URLEncoder.encode(cityName, StandardCharsets.UTF_8);
        logger.info("encodedParam =" + encodedParam);
        final String upperCasedFirstCharacter = encodedParam.substring(0, 1).toUpperCase(locale);
        final String query = String.format("%s%s", upperCasedFirstCharacter, encodedParam.substring(1));
        logger.info("query = " + query);
        return () -> districtsRepository.findDistrictsByCityName(query);
    }
}

Expected behavior

name=diyarbakı
encodedParam = diyarbakı
query = Diyarbakı

Actual behavior

name = diyarbak%25C4%25B1
encodedParam  = diyarbak%25C4%25B1
query = Diyarbak%25C4%25B1

Environment

Linux DESKTOP-B3CNSC5 4.4.0-18362-Microsoft #836-Microsoft Mon May 05 16:04:00 PST 2020 x86_64 x86_64 x86_64 GNU/Linux
openjdk 11.0.6 2020-01-14
OpenJDK Runtime Environment GraalVM CE 20.0.0 (build 11.0.6+9-jvmci-20.0-b02)
OpenJDK 64-Bit Server VM GraalVM CE 20.0.0 (build 11.0.6+9-jvmci-20.0-b02, mixed mode, sharing)
Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f)
Maven home: /mnt/c/Java/apache-maven-3.6.3
Java version: 11.0.6, vendor: Oracle Corporation, runtime: /home/phield/java/graalvm-ce-java11-20.0.0
Default locale: en, platform encoding: UTF-8
OS name: "linux", version: "4.4.0-18362-microsoft", arch: "amd64", family: "unix"
<properties>
    <compiler-plugin.version>3.8.1</compiler-plugin.version>
    <maven.compiler.parameters>true</maven.compiler.parameters>
    <maven.compiler.source>11</maven.compiler.source>
    <maven.compiler.target>11</maven.compiler.target>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <quarkus-plugin.version>1.5.2.Final</quarkus-plugin.version>
    <quarkus.platform.artifact-id>quarkus-universe-bom</quarkus.platform.artifact-id>
    <quarkus.platform.group-id>io.quarkus</quarkus.platform.group-id>
    <quarkus.platform.version>1.5.2.Final</quarkus.platform.version>
    <surefire-plugin.version>2.22.1</surefire-plugin.version>
    <jacoco.version>0.8.4</jacoco.version>
  </properties>

https://github.com/quarkusio/quarkus/issues/10133


$upstream:10133$