diff --git a/src/main/java/com/mercateo/common/rest/schemagen/link/LinkCreator.java b/src/main/java/com/mercateo/common/rest/schemagen/link/LinkCreator.java index ad49ddd..4d4a824 100644 --- a/src/main/java/com/mercateo/common/rest/schemagen/link/LinkCreator.java +++ b/src/main/java/com/mercateo/common/rest/schemagen/link/LinkCreator.java @@ -148,14 +148,18 @@ private void detectMediaType(Collection scopes, Builder builder) { } private Optional detectMediaType(Method method) { - return Optional.ofNullable(method.getAnnotation(Produces.class)).map(produces -> { + Produces annotation = method.getAnnotation(Produces.class); + if (annotation == null) { + annotation = method.getDeclaringClass().getAnnotation(Produces.class); + } + + return Optional.ofNullable(annotation).map(produces -> { final String[] values = produces.value(); if (values.length > 0) { return values[0]; } return null; }); - } private Map collectPathParameters(Scope scope, Object[] parameters) { diff --git a/src/test/java/com/mercateo/common/rest/schemagen/link/LinkCreatorTest.java b/src/test/java/com/mercateo/common/rest/schemagen/link/LinkCreatorTest.java index 1b6ba72..4705925 100644 --- a/src/test/java/com/mercateo/common/rest/schemagen/link/LinkCreatorTest.java +++ b/src/test/java/com/mercateo/common/rest/schemagen/link/LinkCreatorTest.java @@ -3,6 +3,7 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; import static org.mockito.Mockito.when; import java.lang.reflect.Method; @@ -188,6 +189,75 @@ protected Something getReturnType(ImplementedBeanParamType param) { assertEquals("application/json", link.getParams().get("mediaType")); } + @Test + public void testTargetSchemaPresentOnMatchingMediaTypeAtMethodLevel() + throws NoSuchMethodException, SecurityException { + @Path("test") + class TestResource { + @GET + @Produces("application/json") + public String get() { + return "test"; + } + } + + Scope scope = new CallScope(TestResource.class, TestResource.class.getMethod("get"), + new String[] {}, null); + + Link link = createFor(scope, Relation.of(Rel.SELF)); + + assertEquals("http://host/base/test", link.getUri().toString()); + assertEquals("GET", link.getParams().get("method")); + assertEquals("application/json", link.getParams().get("mediaType")); + assertEquals(testSchema, link.getParams().get(LinkCreator.TARGET_SCHEMA_PARAM_KEY)); + } + + @Test + public void testTargetSchemaPresentOnMatchingMediaTypeAtTypeLevel() + throws NoSuchMethodException, SecurityException { + @Path("test") + @Produces("application/json") + class TestResource { + @GET + public String get() { + return "test"; + } + } + + Scope scope = new CallScope(TestResource.class, TestResource.class.getMethod("get"), + new String[] {}, null); + + Link link = createFor(scope, Relation.of(Rel.SELF)); + + assertEquals("http://host/base/test", link.getUri().toString()); + assertEquals("GET", link.getParams().get("method")); + assertEquals("application/json", link.getParams().get("mediaType")); + assertEquals(testSchema, link.getParams().get(LinkCreator.TARGET_SCHEMA_PARAM_KEY)); + } + + @Test + public void testTargetSchemaAbsentOnNonMatchingMediaType() throws NoSuchMethodException, + SecurityException { + @Path("test") + @Produces("application/octet-stream") + class TestResource { + @GET + public String get() { + return "test"; + } + } + + Scope scope = new CallScope(TestResource.class, TestResource.class.getMethod("get"), + new String[] {}, null); + + Link link = createFor(scope, Relation.of(Rel.SELF)); + + assertEquals("http://host/base/test", link.getUri().toString()); + assertEquals("GET", link.getParams().get("method")); + assertEquals("application/octet-stream", link.getParams().get("mediaType")); + assertNull(link.getParams().get(LinkCreator.TARGET_SCHEMA_PARAM_KEY)); + } + private JsonSchemaGenerator createJsonSchemaGenerator() { JsonSchemaGenerator jsonSchemaGenerator = Mockito.mock(JsonSchemaGenerator.class); when(jsonSchemaGenerator.createInputSchema(Matchers.any(), Matchers.any())).thenReturn(