Skip to content

Commit

Permalink
Merge pull request square#1882 from square/jw/string-converter-for-st…
Browse files Browse the repository at this point in the history
…ring

Use string converter for String type.
  • Loading branch information
swankjesse authored Jun 26, 2016
2 parents 5febe68 + 6a12880 commit 9f00bd3
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 23 deletions.
23 changes: 3 additions & 20 deletions retrofit/src/main/java/retrofit2/BuiltInConverters.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,9 @@ final class BuiltInConverters extends Converter.Factory {
public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations,
Retrofit retrofit) {
if (type == ResponseBody.class) {
if (Utils.isAnnotationPresent(annotations, Streaming.class)) {
return StreamingResponseBodyConverter.INSTANCE;
}
return BufferingResponseBodyConverter.INSTANCE;
return Utils.isAnnotationPresent(annotations, Streaming.class)
? StreamingResponseBodyConverter.INSTANCE
: BufferingResponseBodyConverter.INSTANCE;
}
if (type == Void.class) {
return VoidResponseBodyConverter.INSTANCE;
Expand All @@ -47,22 +46,6 @@ public Converter<?, RequestBody> requestBodyConverter(Type type,
return null;
}

@Override public Converter<?, String> stringConverter(Type type, Annotation[] annotations,
Retrofit retrofit) {
if (type == String.class) {
return StringConverter.INSTANCE;
}
return null;
}

static final class StringConverter implements Converter<String, String> {
static final StringConverter INSTANCE = new StringConverter();

@Override public String convert(String value) throws IOException {
return value;
}
}

static final class VoidResponseBodyConverter implements Converter<ResponseBody, Void> {
static final VoidResponseBodyConverter INSTANCE = new VoidResponseBodyConverter();

Expand Down
8 changes: 5 additions & 3 deletions retrofit/src/test/java/retrofit2/RetrofitTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -399,11 +399,13 @@ class MyConverterFactory extends Converter.Factory {
assertThat(annotations).hasAtLeastOneElementOfType(Annotated.Foo.class);
}

@Test public void stringConverterNotCalledForString() {
@Test public void stringConverterCalledForString() {
final AtomicBoolean factoryCalled = new AtomicBoolean();
class MyConverterFactory extends Converter.Factory {
@Override public Converter<?, String> stringConverter(Type type, Annotation[] annotations,
Retrofit retrofit) {
throw new AssertionError();
factoryCalled.set(true);
return null;
}
}
Retrofit retrofit = new Retrofit.Builder()
Expand All @@ -413,7 +415,7 @@ class MyConverterFactory extends Converter.Factory {
CallMethod service = retrofit.create(CallMethod.class);
Call<ResponseBody> call = service.queryString(null);
assertThat(call).isNotNull();
// We also implicitly assert the above factory was not called as it would have thrown.
assertThat(factoryCalled.get()).isTrue();
}

@Test public void stringConverterReturningNullResultsInDefault() {
Expand Down

0 comments on commit 9f00bd3

Please sign in to comment.