diff --git a/core/src/main/com/sinch/sdk/core/http/URLParameterUtils.java b/core/src/main/com/sinch/sdk/core/http/URLParameterUtils.java index 09c6e08ee..7ced9d8b9 100644 --- a/core/src/main/com/sinch/sdk/core/http/URLParameterUtils.java +++ b/core/src/main/com/sinch/sdk/core/http/URLParameterUtils.java @@ -1,6 +1,7 @@ package com.sinch.sdk.core.http; import com.sinch.sdk.core.exceptions.ApiException; +import com.sinch.sdk.core.utils.EnumDynamic; import com.sinch.sdk.core.utils.StringUtil; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; @@ -34,6 +35,10 @@ public static Optional encode(URLParameter parameter) { } Object value = parameter.getValue(); + if (value instanceof EnumDynamic) { + value = ((EnumDynamic) value).value(); + } + if (value instanceof String || value instanceof Number || value instanceof Boolean) { return encodeString(parameter.getStyle(), parameter.getName(), value.toString()); } diff --git a/core/src/test/java/com/sinch/sdk/core/http/AnEnum.java b/core/src/test/java/com/sinch/sdk/core/http/AnEnum.java new file mode 100644 index 000000000..26cf6ac17 --- /dev/null +++ b/core/src/test/java/com/sinch/sdk/core/http/AnEnum.java @@ -0,0 +1,32 @@ +package com.sinch.sdk.core.http; + +import com.sinch.sdk.core.utils.EnumDynamic; +import com.sinch.sdk.core.utils.EnumSupportDynamic; +import java.util.Arrays; +import java.util.stream.Stream; + +class AnEnum extends EnumDynamic { + + static final AnEnum ENUM1 = new AnEnum("ENUM1 value"); + + static final AnEnum ENUM2 = new AnEnum("ENUM2 value"); + + private static final EnumSupportDynamic ENUM_SUPPORT = + new EnumSupportDynamic<>(AnEnum.class, AnEnum::new, Arrays.asList(ENUM1, ENUM2)); + + private AnEnum(String value) { + super(value); + } + + public static Stream values() { + return ENUM_SUPPORT.values(); + } + + public static AnEnum from(String value) { + return ENUM_SUPPORT.from(value); + } + + public static String valueOf(AnEnum e) { + return ENUM_SUPPORT.valueOf(e); + } +} diff --git a/core/src/test/java/com/sinch/sdk/core/http/URLParameterUtilsTest.java b/core/src/test/java/com/sinch/sdk/core/http/URLParameterUtilsTest.java index 25fb94235..35b582384 100644 --- a/core/src/test/java/com/sinch/sdk/core/http/URLParameterUtilsTest.java +++ b/core/src/test/java/com/sinch/sdk/core/http/URLParameterUtilsTest.java @@ -134,6 +134,64 @@ void encodeFormStringExplode() { new URLParameter("foo", "value with space and &,.", URLParameter.STYLE.FORM, true))); } + @Test + void encodeFormStringArrayNoExplode() { + assertEquals( + Optional.of("foo=value1+with+space+and+%26%2C%2E,value2+with+space+and+%26%2C%2E"), + URLParameterUtils.encode( + new URLParameter( + "foo", + Arrays.asList("value1 with space and &,.", "value2 with space and &,."), + URLParameter.STYLE.FORM, + false))); + } + + @Test + void encodeFormStringArrayExplode() { + assertEquals( + Optional.of("foo=value1+with+space+and+%26%2C%2E&foo=value2+with+space+and+%26%2C%2E"), + URLParameterUtils.encode( + new URLParameter( + "foo", + Arrays.asList("value1 with space and &,.", "value2 with space and &,."), + URLParameter.STYLE.FORM, + true))); + } + + @Test + void encodeDynamicEnumNoExplode() { + assertEquals( + Optional.of("foo=ENUM1+value"), + URLParameterUtils.encode( + new URLParameter("foo", AnEnum.ENUM1, URLParameter.STYLE.FORM, false))); + } + + @Test + void encodeDynamicEnumExplode() { + assertEquals( + Optional.of("foo=ENUM1+value"), + URLParameterUtils.encode( + new URLParameter("foo", AnEnum.ENUM1, URLParameter.STYLE.FORM, true))); + } + + @Test + void encodeDynamicEnumArrayNoExplode() { + assertEquals( + Optional.of("foo=ENUM1+value,ENUM2+value"), + URLParameterUtils.encode( + new URLParameter( + "foo", Arrays.asList(AnEnum.ENUM1, AnEnum.ENUM2), URLParameter.STYLE.FORM, false))); + } + + @Test + void encodeDynamicEnumArrayExplode() { + assertEquals( + Optional.of("foo=ENUM1+value&foo=ENUM2+value"), + URLParameterUtils.encode( + new URLParameter( + "foo", Arrays.asList(AnEnum.ENUM1, AnEnum.ENUM2), URLParameter.STYLE.FORM, true))); + } + @Test void encodeSimpleEmptyStringNoExplode() { assertThrows(