From d0f3edd0b1887049adcdf90a00fbd38acb561763 Mon Sep 17 00:00:00 2001 From: Jean-Pierre Portier Date: Mon, 12 Aug 2024 15:43:16 +0200 Subject: [PATCH] fix (core): Get dynamic enum value when used as an URL parameter --- .../sdk/core/http/URLParameterUtils.java | 5 +++ .../java/com/sinch/sdk/core/http/AnEnum.java | 40 +++++++++++++++++++ .../sdk/core/http/URLParameterUtilsTest.java | 23 ++++++++++- 3 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 core/src/test/java/com/sinch/sdk/core/http/AnEnum.java 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..3fc85eb20 --- /dev/null +++ b/core/src/test/java/com/sinch/sdk/core/http/AnEnum.java @@ -0,0 +1,40 @@ +package com.sinch.sdk.core.http; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import com.sinch.sdk.core.exceptions.ApiException; +import com.sinch.sdk.core.utils.EnumDynamic; +import com.sinch.sdk.core.utils.EnumSupportDynamic; +import com.sinch.sdk.domains.conversation.models.v1.ConversationChannel; +import java.util.Arrays; +import java.util.Optional; +import java.util.stream.Stream; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + + 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..1fcd2d805 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 @@ -4,14 +4,18 @@ import static org.junit.jupiter.api.Assertions.assertThrows; import com.sinch.sdk.core.exceptions.ApiException; +import com.sinch.sdk.core.utils.EnumDynamic; +import com.sinch.sdk.core.utils.EnumSupportDynamic; +import com.sinch.sdk.domains.conversation.models.v1.ConversationChannel; import java.util.Arrays; import java.util.Optional; +import java.util.stream.Stream; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; class URLParameterUtilsTest { - @Test + @Test void encodeNull() { Assertions.assertEquals(Optional.empty(), URLParameterUtils.encode(null)); } @@ -134,6 +138,23 @@ void encodeFormStringExplode() { new URLParameter("foo", "value with space and &,.", URLParameter.STYLE.FORM, true))); } + @Test + void encodeDynamicEnumStringNoExplode() { + assertEquals( + Optional.of("foo=ENUM1+value"), + URLParameterUtils.encode( + new URLParameter("foo", AnEnum.ENUM1, URLParameter.STYLE.FORM, false))); + } + + @Test + void encodeDynamicEnumStringExplode() { + assertEquals( + Optional.of("foo=ENUM1+value"), + URLParameterUtils.encode( + new URLParameter("foo", AnEnum.ENUM1, URLParameter.STYLE.FORM, true))); + } + + @Test void encodeSimpleEmptyStringNoExplode() { assertThrows(