Skip to content

Commit 47daf9c

Browse files
committed
Fix #148
1 parent 0ed8c0d commit 47daf9c

File tree

3 files changed

+30
-6
lines changed

3 files changed

+30
-6
lines changed

protobuf/src/main/java/com/fasterxml/jackson/dataformat/protobuf/ProtobufMapper.java

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import java.net.URL;
77

88
import com.fasterxml.jackson.core.Version;
9-
9+
import com.fasterxml.jackson.core.type.TypeReference;
1010
import com.fasterxml.jackson.databind.JavaType;
1111
import com.fasterxml.jackson.databind.JsonMappingException;
1212
import com.fasterxml.jackson.databind.ObjectMapper;
@@ -65,7 +65,6 @@ protected ProtobufMapper(ProtobufMapper src) {
6565
/**
6666
* @since 2.10
6767
*/
68-
@SuppressWarnings("unchecked")
6968
public static ProtobufMapper.Builder builder() {
7069
return new Builder(new ProtobufMapper());
7170
}
@@ -118,7 +117,9 @@ public void setSchemaLoader(ProtobufSchemaLoader l) {
118117
* given Java type. Uses {@link ProtobufSchemaGenerator} for
119118
* generation.
120119
*
121-
* @since 2.8
120+
* @param type Resolved type to generate {@link ProtobufSchema} for
121+
*
122+
* @return Generated {@link ProtobufSchema}
122123
*/
123124
public ProtobufSchema generateSchemaFor(JavaType type) throws JsonMappingException
124125
{
@@ -129,8 +130,11 @@ public ProtobufSchema generateSchemaFor(JavaType type) throws JsonMappingExcepti
129130

130131
/**
131132
* Convenience method for constructing protoc definition that matches
132-
* given Java type. Uses {@link ProtobufSchemaGenerator} for
133-
* generation.
133+
* given Java type. Uses {@link ProtobufSchemaGenerator} for generation.
134+
*
135+
* @param type Type-erased type to generate {@link ProtobufSchema} for
136+
*
137+
* @return Generated {@link ProtobufSchema}
134138
*
135139
* @since 2.8
136140
*/
@@ -141,6 +145,20 @@ public ProtobufSchema generateSchemaFor(Class<?> type) throws JsonMappingExcepti
141145
return gen.getGeneratedSchema();
142146
}
143147

148+
/**
149+
* Convenience method for constructing protoc definition that matches
150+
* given Java type. Uses {@link ProtobufSchemaGenerator} for generation.
151+
*
152+
* @param type Type to generate {@link ProtobufSchema} for
153+
*
154+
* @return Generated {@link ProtobufSchema}
155+
*
156+
* @since 2.10
157+
*/
158+
public ProtobufSchema generateSchemaFor(TypeReference<?> type) throws JsonMappingException {
159+
return generateSchemaFor(_typeFactory.constructType(type));
160+
}
161+
144162
/*
145163
/**********************************************************
146164
/* Schema access, FileDescriptorSets (since 2.9)

protobuf/src/test/java/com/fasterxml/jackson/dataformat/protobuf/EnumHandlingTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.fasterxml.jackson.dataformat.protobuf;
22

3+
import com.fasterxml.jackson.core.type.TypeReference;
34
import com.fasterxml.jackson.databind.ObjectReader;
45
import com.fasterxml.jackson.databind.ObjectWriter;
56
import com.fasterxml.jackson.dataformat.protobuf.schema.ProtobufSchema;
@@ -49,7 +50,7 @@ public void testBigEnum() throws Exception
4950
// type + short id == 2 bytes
5051
assertEquals(2, bytes.length);
5152

52-
ObjectReader r = MAPPER.readerFor(BigEnumWrapper.class).with(schema);
53+
ObjectReader r = MAPPER.readerFor(new TypeReference<BigEnumWrapper> () {}).with(schema);
5354
BigEnumWrapper result = r.readValue(bytes);
5455
assertEquals(input.value, result.value);
5556
}

release-notes/VERSION-2.x

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ Project: jackson-datatypes-binaryModules:
88
=== Releases ===
99
------------------------------------------------------------------------
1010

11+
2.10.0 (not yet released)
12+
13+
#148: (protobuf) Add `ProtobufMapper.generateSchemaFor(TypeReference<?>)` overload
14+
(suggested by MrThreepwood@github)
15+
1116
2.10.0.pr2 (31-Aug-2019)
1217

1318
#168: (avro) `JsonMappingException` for union types with multiple Record types

0 commit comments

Comments
 (0)