mirror of
https://gitlab.com/restcountries/restcountries.git
synced 2026-03-31 15:07:46 +01:00
Closes #280: error message when sending more than 10 fields should be more specific
This commit is contained in:
@@ -81,12 +81,12 @@ public class ControllerHelper {
|
||||
return HttpResponse.badRequest().body(
|
||||
new ResponseEntity(
|
||||
Response.Status.BAD_REQUEST.getStatusCode(),
|
||||
"'fields' query not specified"
|
||||
"'fields' query not specified or you're requesting more than 10 fields"
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
protected static boolean hasValidFields(Optional<String> fields) {
|
||||
protected static boolean hasInvalidFields(Optional<String> fields) {
|
||||
if (fields.isEmpty()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -20,9 +20,8 @@ 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;
|
||||
import static com.restcountries.controller.ControllerHelper.hasInvalidFields;
|
||||
|
||||
@Hidden
|
||||
@Controller("/v2/")
|
||||
@@ -30,7 +29,7 @@ public class CountryControllerV2 {
|
||||
|
||||
@Get(uri = "all", produces = MediaType.APPLICATION_JSON)
|
||||
public Object getAllCountries(@QueryValue("fields") Optional<String> fields) {
|
||||
if (hasValidFields(fields)) {
|
||||
if (hasInvalidFields(fields)) {
|
||||
return ControllerHelper.badAllRequest();
|
||||
}
|
||||
List<Country> countries = CountryServiceV2.getInstance().getAll();
|
||||
|
||||
@@ -10,12 +10,9 @@ 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;
|
||||
import static com.restcountries.controller.ControllerHelper.hasInvalidFields;
|
||||
|
||||
@Hidden
|
||||
@Controller("/v3/")
|
||||
@@ -23,7 +20,7 @@ public class CountryControllerV3 extends ControllerV3Helper {
|
||||
|
||||
@Get(uri = "all", produces = MediaType.APPLICATION_JSON)
|
||||
public HttpResponse<Object> getAllCountries(@QueryValue("fields") Optional<String> fields) {
|
||||
if (hasValidFields(fields)) {
|
||||
if (hasInvalidFields(fields)) {
|
||||
return ControllerHelper.badAllRequest();
|
||||
}
|
||||
var countries = CountryServiceV3.getInstance().getAll();
|
||||
|
||||
@@ -11,12 +11,9 @@ 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;
|
||||
import static com.restcountries.controller.ControllerHelper.hasInvalidFields;
|
||||
|
||||
@Controller("/v3.1/")
|
||||
public class CountryControllerV31 extends ControllerV3Helper {
|
||||
@@ -24,7 +21,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 (hasValidFields(fields)) {
|
||||
if (hasInvalidFields(fields)) {
|
||||
return ControllerHelper.badAllRequest();
|
||||
}
|
||||
var countries = CountryServiceV31.getInstance().getAll();
|
||||
|
||||
Reference in New Issue
Block a user