From 7378efe122fece613d986b3f03a97540b21d00d8 Mon Sep 17 00:00:00 2001 From: Alejandro Date: Wed, 4 Jun 2025 17:12:47 -0500 Subject: [PATCH] #265 endpoint 'all' requires fields to be specified --- .../com/restcountries/controller/CountryControllerV2.java | 7 +++++++ .../com/restcountries/controller/CountryControllerV3.java | 7 +++++++ .../com/restcountries/controller/CountryControllerV31.java | 7 +++++++ 3 files changed, 21 insertions(+) diff --git a/src/main/java/com/restcountries/controller/CountryControllerV2.java b/src/main/java/com/restcountries/controller/CountryControllerV2.java index 619afd3..e25db3d 100644 --- a/src/main/java/com/restcountries/controller/CountryControllerV2.java +++ b/src/main/java/com/restcountries/controller/CountryControllerV2.java @@ -27,6 +27,13 @@ public class CountryControllerV2 { @Get(uri = "all", produces = MediaType.APPLICATION_JSON) public Object getAllCountries(@QueryValue("fields") Optional fields) { + if (fields.isEmpty()) { + return ControllerHelper.badAllRequest(); + } + var totalFields = fields.get().split(",").length; + if (totalFields > 10) { + return ControllerHelper.badAllRequest(); + } List countries = CountryServiceV2.getInstance().getAll(); return checkFieldsAndParseCountries(fields, countries); } diff --git a/src/main/java/com/restcountries/controller/CountryControllerV3.java b/src/main/java/com/restcountries/controller/CountryControllerV3.java index 2b0f2ab..e924b52 100644 --- a/src/main/java/com/restcountries/controller/CountryControllerV3.java +++ b/src/main/java/com/restcountries/controller/CountryControllerV3.java @@ -18,6 +18,13 @@ public class CountryControllerV3 extends ControllerV3Helper { @Get(uri = "all", produces = MediaType.APPLICATION_JSON) public HttpResponse getAllCountries(@QueryValue("fields") Optional fields) { + if (fields.isEmpty()) { + return ControllerHelper.badAllRequest(); + } + var totalFields = fields.get().split(",").length; + if (totalFields > 10) { + return ControllerHelper.badAllRequest(); + } var countries = CountryServiceV3.getInstance().getAll(); return ControllerHelper.ok(checkFieldsAndParseCountries(fields, countries)); } diff --git a/src/main/java/com/restcountries/controller/CountryControllerV31.java b/src/main/java/com/restcountries/controller/CountryControllerV31.java index 28a6d99..382effe 100644 --- a/src/main/java/com/restcountries/controller/CountryControllerV31.java +++ b/src/main/java/com/restcountries/controller/CountryControllerV31.java @@ -19,6 +19,13 @@ public class CountryControllerV31 extends ControllerV3Helper { @Get(uri = "all", produces = MediaType.APPLICATION_JSON) @Schema(name = "RestCountries") public Object getAllCountries(@QueryValue("fields") Optional fields) { + if (fields.isEmpty()) { + return ControllerHelper.badAllRequest(); + } + var totalFields = fields.get().split(",").length; + if (totalFields > 10) { + return ControllerHelper.badAllRequest(); + } var countries = CountryServiceV31.getInstance().getAll(); return ControllerHelper.ok(checkFieldsAndParseCountries(fields, countries)); }