Skip to content

Commit

Permalink
Support for Enum Query parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
sashirestela committed Aug 31, 2024
1 parent d6f8127 commit 2000a0a
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 15 deletions.
28 changes: 14 additions & 14 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>io.github.sashirestela</groupId>
<artifactId>cleverclient</artifactId>
<version>1.4.3</version>
<version>1.4.4</version>
<packaging>jar</packaging>

<name>cleverclient</name>
Expand Down Expand Up @@ -51,23 +51,23 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.release>11</maven.compiler.release>
<!-- Dependencies Versions -->
<slf4j.version>2.0.12</slf4j.version>
<lombok.version>1.18.32</lombok.version>
<jackson.version>2.17.0</jackson.version>
<junit.version>5.10.2</junit.version>
<mockito.version>5.11.0</mockito.version>
<slf4j.version>2.0.16</slf4j.version>
<lombok.version>1.18.34</lombok.version>
<jackson.version>2.17.2</jackson.version>
<junit.version>5.11.0</junit.version>
<mockito.version>5.13.0</mockito.version>
<!-- Plugins Versions -->
<compiler.version>3.13.0</compiler.version>
<enforcer.version>3.4.1</enforcer.version>
<surefire.version>3.2.5</surefire.version>
<exec.version>3.2.0</exec.version>
<enforcer.version>3.5.0</enforcer.version>
<surefire.version>3.5.0</surefire.version>
<exec.version>3.4.1</exec.version>
<jacoco.version>0.8.12</jacoco.version>
<helper.version>3.5.0</helper.version>
<versions.version>2.16.2</versions.version>
<helper.version>3.6.0</helper.version>
<versions.version>2.17.1</versions.version>
<source.version>3.3.1</source.version>
<javadoc.version>3.6.3</javadoc.version>
<gpg.version>3.1.0</gpg.version>
<sonatype.version>1.6.13</sonatype.version>
<javadoc.version>3.10.0</javadoc.version>
<gpg.version>3.2.5</gpg.version>
<sonatype.version>1.7.0</sonatype.version>
<spotless.version>2.43.0</spotless.version>
</properties>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@

import io.github.sashirestela.cleverclient.metadata.InterfaceMetadata.MethodMetadata;
import io.github.sashirestela.cleverclient.metadata.InterfaceMetadata.ParameterMetadata;
import io.github.sashirestela.cleverclient.support.CleverClientException;
import io.github.sashirestela.cleverclient.util.CommonUtil;
import io.github.sashirestela.cleverclient.util.JsonUtil;

import java.lang.reflect.Field;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;

import com.fasterxml.jackson.annotation.JsonProperty;

public class URLBuilder {

private static URLBuilder urlBuilder = null;
Expand Down Expand Up @@ -52,15 +57,35 @@ private String includeQueryParams(String url, List<ParameterMetadata> queryParam
if (value == null) {
continue;
}
if (value instanceof Enum) {
value = getEnumValue(value);
}
var queryParam = parameter.getAnnotation().getValue();
if (queryParam == null || queryParam.isEmpty())
if (CommonUtil.isNullOrEmpty(queryParam))
appendQueryParams(JsonUtil.objectToMap(value), queryParamBuilder);
else
appendQueryParam(queryParam, value, queryParamBuilder);
}
return url + queryParamBuilder;
}

private String getEnumValue(Object value) {
var clazz = value.getClass();
var enumValue = ((Enum<?>) value).name();
Field enumField;
try {
enumField = clazz.getField(enumValue);
var hasJsonPropertyAnnotation = enumField.isAnnotationPresent(JsonProperty.class);
if (hasJsonPropertyAnnotation) {
return enumField.getAnnotation(JsonProperty.class).value();
} else {
return enumValue;
}
} catch (NoSuchFieldException | SecurityException e) {
throw new CleverClientException("Cannot get the enum field for the enum value {0}", enumValue, e);
}
}

private void appendQueryParams(Map<String, ?> queryParams, StringBuilder queryParamBuilder) {
queryParams.forEach((k, v) -> appendQueryParam(k, v, queryParamBuilder));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import lombok.Data;
import org.junit.jupiter.api.Test;

import com.fasterxml.jackson.annotation.JsonProperty;

import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -126,6 +128,35 @@ void shouldReturnReplacedUrlWithQueryParamsWhenMethodContainsQueryParamsForPojos
assertEquals(expectedUrl, actualUrl);
}

@Test
void shouldReturnReplacedUrlWithQueryParamsWhenMethodContainsEnumQueryParams() {
var url = "/api/domain/entities";
var paramsList = List.of(
ParameterMetadata.builder()
.index(0)
.annotation(AnnotationMetadata.builder()
.name("Query")
.isHttpMethod(false)
.valueByField(Map.of("value", "statusOpen"))
.build())
.build(),
ParameterMetadata.builder()
.index(1)
.annotation(AnnotationMetadata.builder()
.name("Query")
.isHttpMethod(false)
.valueByField(Map.of("value", "statusClosed"))
.build())
.build());

when(methodMetadata.getPathParameters()).thenReturn(List.of());
when(methodMetadata.getQueryParameters()).thenReturn(paramsList);

var actualUrl = urlBuilder.build(url, methodMetadata, new Object[] { Status.OPEN, Status.CLOSED });
var expectedUrl = "/api/domain/entities?statusOpen=open&statusClosed=CLOSED";
assertEquals(expectedUrl, actualUrl);
}

@Data
@AllArgsConstructor
static class Pagination {
Expand All @@ -135,4 +166,12 @@ static class Pagination {

}

enum Status {

@JsonProperty("open")
OPEN,

CLOSED;
}

}

0 comments on commit 2000a0a

Please sign in to comment.