mirror of
https://gitlab.com/restcountries/restcountries.git
synced 2026-03-31 15:07:46 +01:00
Updated #256: avoids 200 when fields are empty
This commit is contained in:
@@ -4,6 +4,11 @@ import com.restcountries.domain.ResponseEntity;
|
||||
import io.micronaut.http.HttpResponse;
|
||||
import jakarta.ws.rs.core.Response;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static com.restcountries.utils.Constants.CACHE_CONTROL_VALUE;
|
||||
import static io.micronaut.http.HttpHeaders.CACHE_CONTROL;
|
||||
|
||||
@@ -80,4 +85,21 @@ public class ControllerHelper {
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
protected static boolean hasValidFields(Optional<String> fields) {
|
||||
if (fields.isEmpty()) {
|
||||
return true;
|
||||
}
|
||||
var totalFields = Arrays
|
||||
.stream(fields.get().split(","))
|
||||
.toList()
|
||||
.stream()
|
||||
.map(String::trim)
|
||||
.collect(Collectors.toCollection(ArrayList::new));
|
||||
boolean isEmptyOrHasBlank = totalFields.isEmpty() || totalFields.stream().anyMatch(String::isEmpty);
|
||||
if (isEmptyOrHasBlank || totalFields.size() > 10) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,6 +20,9 @@ import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static com.restcountries.controller.ControllerHelper.hasValidFields;
|
||||
|
||||
@Hidden
|
||||
@Controller("/v2/")
|
||||
@@ -27,11 +30,7 @@ public class CountryControllerV2 {
|
||||
|
||||
@Get(uri = "all", produces = MediaType.APPLICATION_JSON)
|
||||
public Object getAllCountries(@QueryValue("fields") Optional<String> fields) {
|
||||
if (fields.isEmpty()) {
|
||||
return ControllerHelper.badAllRequest();
|
||||
}
|
||||
var totalFields = fields.get().split(",").length;
|
||||
if (totalFields > 10) {
|
||||
if (hasValidFields(fields)) {
|
||||
return ControllerHelper.badAllRequest();
|
||||
}
|
||||
List<Country> countries = CountryServiceV2.getInstance().getAll();
|
||||
|
||||
@@ -10,7 +10,12 @@ import io.micronaut.http.annotation.QueryValue;
|
||||
import io.swagger.v3.oas.annotations.Hidden;
|
||||
|
||||
import javax.ws.rs.QueryParam;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static com.restcountries.controller.ControllerHelper.hasValidFields;
|
||||
|
||||
@Hidden
|
||||
@Controller("/v3/")
|
||||
@@ -18,11 +23,7 @@ public class CountryControllerV3 extends ControllerV3Helper {
|
||||
|
||||
@Get(uri = "all", produces = MediaType.APPLICATION_JSON)
|
||||
public HttpResponse<Object> getAllCountries(@QueryValue("fields") Optional<String> fields) {
|
||||
if (fields.isEmpty()) {
|
||||
return ControllerHelper.badAllRequest();
|
||||
}
|
||||
var totalFields = fields.get().split(",").length;
|
||||
if (totalFields > 10) {
|
||||
if (hasValidFields(fields)) {
|
||||
return ControllerHelper.badAllRequest();
|
||||
}
|
||||
var countries = CountryServiceV3.getInstance().getAll();
|
||||
|
||||
@@ -11,7 +11,12 @@ import io.swagger.v3.oas.annotations.media.Schema;
|
||||
|
||||
import javax.ws.rs.QueryParam;
|
||||
import javax.ws.rs.core.Response;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static com.restcountries.controller.ControllerHelper.hasValidFields;
|
||||
|
||||
@Controller("/v3.1/")
|
||||
public class CountryControllerV31 extends ControllerV3Helper {
|
||||
@@ -19,11 +24,7 @@ public class CountryControllerV31 extends ControllerV3Helper {
|
||||
@Get(uri = "all", produces = MediaType.APPLICATION_JSON)
|
||||
@Schema(name = "RestCountries")
|
||||
public Object getAllCountries(@QueryValue("fields") Optional<String> fields) {
|
||||
if (fields.isEmpty()) {
|
||||
return ControllerHelper.badAllRequest();
|
||||
}
|
||||
var totalFields = fields.get().split(",").length;
|
||||
if (totalFields > 10) {
|
||||
if (hasValidFields(fields)) {
|
||||
return ControllerHelper.badAllRequest();
|
||||
}
|
||||
var countries = CountryServiceV31.getInstance().getAll();
|
||||
|
||||
Reference in New Issue
Block a user