Skip to content

Commit

Permalink
fix (core): Get dynamic enum value when used as an URL parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
JPPortier committed Aug 12, 2024
1 parent 805b343 commit 9c1162a
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
5 changes: 5 additions & 0 deletions core/src/main/com/sinch/sdk/core/http/URLParameterUtils.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -34,6 +35,10 @@ public static Optional<String> 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());
}
Expand Down
32 changes: 32 additions & 0 deletions core/src/test/java/com/sinch/sdk/core/http/AnEnum.java
Original file line number Diff line number Diff line change
@@ -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<String, AnEnum> {

static final AnEnum ENUM1 = new AnEnum("ENUM1 value");

static final AnEnum ENUM2 = new AnEnum("ENUM2 value");

private static final EnumSupportDynamic<String, AnEnum> ENUM_SUPPORT =
new EnumSupportDynamic<>(AnEnum.class, AnEnum::new, Arrays.asList(ENUM1, ENUM2));

private AnEnum(String value) {
super(value);
}

public static Stream<AnEnum> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,22 @@ 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(
Expand Down

0 comments on commit 9c1162a

Please sign in to comment.