Skip to content

Commit

Permalink
Fix to validate interfaces with default methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Sashir Estela committed Apr 27, 2024
1 parent 1c3268b commit 080065f
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 15 deletions.
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
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 080065f

Please sign in to comment.