Skip to content

Commit

Permalink
Support supplying a protobuf extension registry for deserialization.
Browse files Browse the repository at this point in the history
  • Loading branch information
JakeWharton committed Apr 2, 2016
1 parent 011a89e commit c8c71ed
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 79 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package retrofit2.converter.protobuf;

import com.google.protobuf.ExtensionRegistryLite;
import com.google.protobuf.MessageLite;
import com.google.protobuf.Parser;
import java.lang.annotation.Annotation;
Expand All @@ -33,7 +34,18 @@
*/
public final class ProtoConverterFactory extends Converter.Factory {
public static ProtoConverterFactory create() {
return new ProtoConverterFactory();
return new ProtoConverterFactory(null);
}

/** Create an instance which uses {@code registry} when deserializing. */
public static ProtoConverterFactory createWithRegistry(ExtensionRegistryLite registry) {
return new ProtoConverterFactory(registry);
}

private final ExtensionRegistryLite registry;

private ProtoConverterFactory(ExtensionRegistryLite registry) {
this.registry = registry;
}

@Override
Expand All @@ -56,7 +68,7 @@ public static ProtoConverterFactory create() {
throw new IllegalArgumentException(
"Found a protobuf message but " + c.getName() + " had no PARSER field.");
}
return new ProtoResponseBodyConverter<>(parser);
return new ProtoResponseBodyConverter<>(parser, registry);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package retrofit2.converter.protobuf;

import com.google.protobuf.ExtensionRegistryLite;
import com.google.protobuf.InvalidProtocolBufferException;
import com.google.protobuf.MessageLite;
import com.google.protobuf.Parser;
Expand All @@ -25,14 +26,16 @@
final class ProtoResponseBodyConverter<T extends MessageLite>
implements Converter<ResponseBody, T> {
private final Parser<T> parser;
private final ExtensionRegistryLite registry;

ProtoResponseBodyConverter(Parser<T> parser) {
ProtoResponseBodyConverter(Parser<T> parser, ExtensionRegistryLite registry) {
this.parser = parser;
this.registry = registry;
}

@Override public T convert(ResponseBody value) throws IOException {
try {
return parser.parseFrom(value.byteStream());
return parser.parseFrom(value.byteStream(), registry);
} catch (InvalidProtocolBufferException e) {
throw new RuntimeException(e); // Despite extending IOException, this is data mismatch.
} finally {
Expand Down
Loading

0 comments on commit c8c71ed

Please sign in to comment.