Skip to content

Commit

Permalink
Merge pull request #45 from mweirauch/master
Browse files Browse the repository at this point in the history
fix for targetSchema creation when @produces is declared at type level
  • Loading branch information
wuan committed Aug 3, 2017
2 parents 4a7c60a + e1c774a commit 61a7459
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -148,14 +148,18 @@ private void detectMediaType(Collection<Scope> scopes, Builder builder) {
}

private Optional<String> 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<String, Object> collectPathParameters(Scope scope, Object[] parameters) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit 61a7459

Please sign in to comment.