Skip to content

Commit

Permalink
Merge pull request #71 from sashirestela/70-issue-at-validating-inter…
Browse files Browse the repository at this point in the history
…faces-with-default-methods

Fix to validate interfaces with default methods
  • Loading branch information
sashirestela committed Apr 27, 2024
2 parents 1c3268b + d9a8125 commit ac67bd9
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 23 deletions.
2 changes: 1 addition & 1 deletion 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.1</version>
<version>1.4.2</version>
<packaging>jar</packaging>

<name>cleverclient</name>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package io.github.sashirestela.cleverclient.annotation;

import io.github.sashirestela.cleverclient.annotation.StreamType.List;
import io.github.sashirestela.cleverclient.annotation.StreamType.StreamTypeArray;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
Expand All @@ -12,7 +12,7 @@
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.METHOD, ElementType.ANNOTATION_TYPE })
@Repeatable(List.class)
@Repeatable(StreamTypeArray.class)
public @interface StreamType {

Class<?> type();
Expand All @@ -22,7 +22,7 @@
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.METHOD, ElementType.ANNOTATION_TYPE })
@interface List {
@interface StreamTypeArray {

StreamType[] value();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,21 +119,23 @@ private List<ParameterMetadata> getParameters(Parameter[] javaParameters) {

private void validate(InterfaceMetadata interfaceMetadata) {
interfaceMetadata.getMethodBySignature().forEach((methodSignature, methodMetadata) -> {
if (!methodMetadata.isDefault() && !methodMetadata.hasHttpAnnotation()) {
throw new CleverClientException("Missing HTTP annotation for the method {0}.",
methodMetadata.getName(), null);
}
var url = interfaceMetadata.getFullUrlByMethod(methodMetadata);
var listPathParams = CommonUtil.findFullMatches(url, Constant.REGEX_PATH_PARAM_URL);
if (!CommonUtil.isNullOrEmpty(listPathParams)) {
listPathParams.forEach(pathParam -> methodMetadata.getPathParameters()
.stream()
.map(parameter -> parameter.getAnnotation().getValue())
.filter(paramAnnotValue -> pathParam.equals(paramAnnotValue))
.findFirst()
.orElseThrow(() -> new CleverClientException(
"Path param {0} in the url cannot find an annotated argument in the method {1}.",
pathParam, methodMetadata.getName(), null)));
if (!methodMetadata.isDefault()) {
if (!methodMetadata.hasHttpAnnotation()) {
throw new CleverClientException("Missing HTTP annotation for the method {0}.",
methodMetadata.getName(), null);
}
var url = interfaceMetadata.getFullUrlByMethod(methodMetadata);
var listPathParams = CommonUtil.findFullMatches(url, Constant.REGEX_PATH_PARAM_URL);
if (!CommonUtil.isNullOrEmpty(listPathParams)) {
listPathParams.forEach(pathParam -> methodMetadata.getPathParameters()
.stream()
.map(parameter -> parameter.getAnnotation().getValue())
.filter(paramAnnotValue -> pathParam.equals(paramAnnotValue))
.findFirst()
.orElseThrow(() -> new CleverClientException(
"Path param {0} in the url cannot find an annotated argument in the method {1}.",
pathParam, methodMetadata.getName(), null)));
}
}
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.github.sashirestela.cleverclient.support;

import io.github.sashirestela.cleverclient.annotation.StreamType;
import io.github.sashirestela.cleverclient.annotation.StreamType.StreamTypeArray;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
Expand Down Expand Up @@ -44,19 +45,19 @@ public ReturnType(Method method) {
}

private void setClassByEventIfExists(Method method) {
if (method.isAnnotationPresent(StreamType.List.class)) {
if (method.isAnnotationPresent(StreamTypeArray.class)) {
this.classByEvent = calculateClassByEvent(
method.getDeclaredAnnotation(StreamType.List.class).value());
method.getDeclaredAnnotation(StreamTypeArray.class).value());
} else if (method.isAnnotationPresent(StreamType.class)) {
this.classByEvent = calculateClassByEvent(
new StreamType[] { method.getDeclaredAnnotation(StreamType.class) });
} else {
var innerStreamTypeList = getInnerAnnotationIfExists(method, StreamType.List.class);
var innerStreamTypeList = getInnerAnnotationIfExists(method, StreamTypeArray.class);
if (innerStreamTypeList.isPresent()) {
this.classByEvent = calculateClassByEvent(
innerStreamTypeList.get()
.annotationType()
.getDeclaredAnnotation(StreamType.List.class)
.getDeclaredAnnotation(StreamTypeArray.class)
.value());
} else {
var innerStreamType = getInnerAnnotationIfExists(method, StreamType.class);
Expand Down
12 changes: 12 additions & 0 deletions src/test/java/io/github/sashirestela/cleverclient/http/ITest.java
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,18 @@ interface NotSavedService {

}

@Resource("/api/demos/{demoId}")
interface WithResourcePathParamAndDefaultMethods {

@GET
Demo getDemoPrimitive(@Path("demoId") Integer demoId);

default Demo getDemo(Integer demoId) {
return getDemoPrimitive(demoId);
}

}

@NoArgsConstructor
@AllArgsConstructor
@Getter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.util.HashMap;

import static io.github.sashirestela.cleverclient.util.CommonUtil.createMapString;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

Expand Down Expand Up @@ -151,4 +152,9 @@ void shouldThrownExceptionWhenUrlPathParamAtMethodUnmatchesAnnotatedArguments()
exception.getMessage());
}

@Test
void shouldSaveSuccesfullyWhenInterfaceHasResourcePathParamAndDefaultMethod() {
assertDoesNotThrow(() -> store.save(ITest.WithResourcePathParamAndDefaultMethods.class));
}

}

0 comments on commit ac67bd9

Please sign in to comment.