This is an automated email from the ASF dual-hosted git repository. reta pushed a commit to branch 3.6.x-fixes in repository https://gitbox.apache.org/repos/asf/cxf.git
commit 8ddd7177c16b5c8b1ef2ffadf1527b0e7a2090fe Author: Andriy Redko <[email protected]> AuthorDate: Tue Dec 27 17:16:20 2022 -0500 CXF-8797: NameBinding ignored when implementing interface (#1045) (cherry picked from commit 9900e83b5970c32d40b797584d5cf6739952a97f) # Conflicts: # rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/model/OperationResourceInfoTest.java --- .../cxf/jaxrs/model/OperationResourceInfo.java | 4 +- .../cxf/jaxrs/model/OperationResourceInfoTest.java | 65 +++++++++++++++++++++- 2 files changed, 67 insertions(+), 2 deletions(-) diff --git a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/model/OperationResourceInfo.java b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/model/OperationResourceInfo.java index 617e3cb014..38f807a194 100644 --- a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/model/OperationResourceInfo.java +++ b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/model/OperationResourceInfo.java @@ -81,9 +81,11 @@ public class OperationResourceInfo { public OperationResourceInfo(Method mInvoke, Method mAnnotated, ClassResourceInfo cri) { methodToInvoke = mInvoke; annotatedMethod = mAnnotated; + // Combine the name bindings from annotated method and method to invoke + nameBindings.addAll(AnnotationUtils.getNameBindings(mInvoke.getAnnotations())); if (mAnnotated != null) { parameters = ResourceUtils.getParameters(mAnnotated); - nameBindings = AnnotationUtils.getNameBindings(mAnnotated.getAnnotations()); + nameBindings.addAll(AnnotationUtils.getNameBindings(mAnnotated.getAnnotations())); } classResourceInfo = cri; checkMediaTypes(null, null); diff --git a/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/model/OperationResourceInfoTest.java b/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/model/OperationResourceInfoTest.java index 8835279b67..59ec4dd406 100644 --- a/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/model/OperationResourceInfoTest.java +++ b/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/model/OperationResourceInfoTest.java @@ -19,14 +19,22 @@ package org.apache.cxf.jaxrs.model; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; +import java.lang.reflect.Method; import java.util.Collections; import java.util.List; +import java.util.Set; import javax.ws.rs.Consumes; +import javax.ws.rs.NameBinding; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; import org.apache.cxf.endpoint.Endpoint; +import org.apache.cxf.jaxrs.utils.AnnotationUtils; import org.apache.cxf.message.Exchange; import org.apache.cxf.message.ExchangeImpl; import org.apache.cxf.message.Message; @@ -52,6 +60,30 @@ public class OperationResourceInfoTest { }; } + + @Produces("text/xml") + @Consumes("application/xml") + interface TestInteface { + @Produces("text/plain") + void doIt(); + + @CustomNameBinding + @Consumes("application/atom+xml") + void doThat(); + } + + static class TestClass2 implements TestInteface { + @CustomNameBinding + @Override + public void doIt() { + } + + @Override + public void doThat() { + } + + } + @Test public void testConsumeTypes() throws Exception { @@ -137,6 +169,31 @@ public class OperationResourceInfoTest { assertEquals(0, result); } + @Test + public void testNameBindingsClass() throws NoSuchMethodException, SecurityException { + final Method method = TestClass2.class.getMethod("doIt", new Class[]{}); + + OperationResourceInfo ori = new OperationResourceInfo( + method, + AnnotationUtils.getAnnotatedMethod(TestClass2.class, method), + new ClassResourceInfo(TestClass2.class)); + + final Set<String> names = ori.getNameBindings(); + assertEquals(Collections.singleton(CustomNameBinding.class.getName()), names); + } + + @Test + public void testNameBindingsInterface() throws NoSuchMethodException, SecurityException { + final Method method = TestClass2.class.getMethod("doThat", new Class[]{}); + + OperationResourceInfo ori = new OperationResourceInfo( + method, + AnnotationUtils.getAnnotatedMethod(TestClass2.class, method), + new ClassResourceInfo(TestClass2.class)); + + Set<String> names = ori.getNameBindings(); + assertEquals(Collections.singleton(CustomNameBinding.class.getName()), names); + } private static Message createMessage() { Message m = new MessageImpl(); @@ -149,4 +206,10 @@ public class OperationResourceInfoTest { e.put(Endpoint.class, endpoint); return m; } -} \ No newline at end of file + + @Target({ ElementType.TYPE, ElementType.METHOD }) + @Retention(value = RetentionPolicy.RUNTIME) + @NameBinding + public @interface CustomNameBinding { + } +}
