From 080065f8a5f9afdc55aae9538d89b11331d56590 Mon Sep 17 00:00:00 2001 From: Sashir Estela Date: Sat, 27 Apr 2024 22:31:40 +0000 Subject: [PATCH] Fix to validate interfaces with default methods --- .../metadata/InterfaceMetadataStore.java | 32 ++++++++++--------- .../sashirestela/cleverclient/http/ITest.java | 12 +++++++ .../metadata/InterfaceMetadataStoreTest.java | 6 ++++ 3 files changed, 35 insertions(+), 15 deletions(-) diff --git a/src/main/java/io/github/sashirestela/cleverclient/metadata/InterfaceMetadataStore.java b/src/main/java/io/github/sashirestela/cleverclient/metadata/InterfaceMetadataStore.java index 61e8eaa..157f725 100644 --- a/src/main/java/io/github/sashirestela/cleverclient/metadata/InterfaceMetadataStore.java +++ b/src/main/java/io/github/sashirestela/cleverclient/metadata/InterfaceMetadataStore.java @@ -119,21 +119,23 @@ private List 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))); + } } }); } diff --git a/src/test/java/io/github/sashirestela/cleverclient/http/ITest.java b/src/test/java/io/github/sashirestela/cleverclient/http/ITest.java index d234f49..730c168 100644 --- a/src/test/java/io/github/sashirestela/cleverclient/http/ITest.java +++ b/src/test/java/io/github/sashirestela/cleverclient/http/ITest.java @@ -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 diff --git a/src/test/java/io/github/sashirestela/cleverclient/metadata/InterfaceMetadataStoreTest.java b/src/test/java/io/github/sashirestela/cleverclient/metadata/InterfaceMetadataStoreTest.java index d2f1dd9..264dabb 100644 --- a/src/test/java/io/github/sashirestela/cleverclient/metadata/InterfaceMetadataStoreTest.java +++ b/src/test/java/io/github/sashirestela/cleverclient/metadata/InterfaceMetadataStoreTest.java @@ -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; @@ -151,4 +152,9 @@ void shouldThrownExceptionWhenUrlPathParamAtMethodUnmatchesAnnotatedArguments() exception.getMessage()); } + @Test + void shouldSaveSuccesfullyWhenInterfaceHasResourcePathParamAndDefaultMethod() { + assertDoesNotThrow(() -> store.save(ITest.WithResourcePathParamAndDefaultMethods.class)); + } + }