Skip to content

Commit 63f9a0f

Browse files
authored
Fix #662: remove throws IOException from AvroMapper.schemaFrom() method signatures (#671)
1 parent 8a93fb3 commit 63f9a0f

17 files changed

Lines changed: 73 additions & 57 deletions

avro/src/main/java/tools/jackson/dataformat/avro/AvroMapper.java

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import tools.jackson.core.JacksonException;
1010
import tools.jackson.core.JsonGenerator;
1111
import tools.jackson.core.Version;
12-
12+
import tools.jackson.core.exc.JacksonIOException;
1313
import tools.jackson.databind.*;
1414
import tools.jackson.databind.cfg.MapperBuilder;
1515
import tools.jackson.databind.cfg.MapperBuilderState;
@@ -277,13 +277,16 @@ protected JacksonException _invalidSchemaDefinition(JavaType type,
277277
* and once done (successfully or not), closing the stream.
278278
*<p>
279279
* Resulting schema object does not use separate reader/writer schemas.
280+
*<p>
281+
* NOTE: up until 3.1, was declared to throw {@link IOException}; removed
282+
* in 3.2
280283
*/
281-
public AvroSchema schemaFrom(InputStream in) throws IOException
284+
public AvroSchema schemaFrom(InputStream in0)
282285
{
283-
try {
286+
try (InputStream in = in0) {
284287
return new AvroSchema(new Schema.Parser().parse(in));
285-
} finally {
286-
in.close();
288+
} catch (IOException e) {
289+
throw JacksonIOException.construct(e);
287290
}
288291
}
289292

@@ -292,8 +295,11 @@ public AvroSchema schemaFrom(InputStream in) throws IOException
292295
* encoded JSON representation.
293296
*<p>
294297
* Resulting schema object does not use separate reader/writer schemas.
298+
*<p>
299+
* NOTE: up until 3.1, was declared to throw {@link IOException}; removed
300+
* in 3.2
295301
*/
296-
public AvroSchema schemaFrom(String schemaAsString) throws IOException
302+
public AvroSchema schemaFrom(String schemaAsString)
297303
{
298304
return new AvroSchema(new Schema.Parser().parse(schemaAsString));
299305
}
@@ -303,10 +309,17 @@ public AvroSchema schemaFrom(String schemaAsString) throws IOException
303309
* encoded JSON representation.
304310
*<p>
305311
* Resulting schema object does not use separate reader/writer schemas.
312+
*<p>
313+
* NOTE: up until 3.1, was declared to throw {@link IOException}; removed
314+
* in 3.2
306315
*/
307-
public AvroSchema schemaFrom(File schemaFile) throws IOException
316+
public AvroSchema schemaFrom(File schemaFile)
308317
{
309-
return new AvroSchema(new Schema.Parser().parse(schemaFile));
318+
try {
319+
return new AvroSchema(new Schema.Parser().parse(schemaFile));
320+
} catch (IOException e) {
321+
throw JacksonIOException.construct(e);
322+
}
310323
}
311324

312325
/*

avro/src/test/java/tools/jackson/dataformat/avro/AmbiguousUnionWriteTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
// it should not be.
1010
public class AmbiguousUnionWriteTest extends AvroTestBase
1111
{
12-
protected final String SCHEMA_WITH_AMBIGUITY = aposToQuotes("{\n"
12+
protected final String SCHEMA_WITH_AMBIGUITY = a2q("{\n"
1313
+"'type': 'record',\n"
1414
+"'name': 'WithUnion',\n"
1515
+"'fields': [\n"

avro/src/test/java/tools/jackson/dataformat/avro/AnyProperties75Test.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public Object get(String name) {
3838
@Test
3939
public void testReadWriteIntSequence() throws Exception
4040
{
41-
final String SCHEMA_JSON = aposToQuotes("{\n"
41+
final String SCHEMA_JSON = a2q("{\n"
4242
+ " 'type': 'record',\n"
4343
+ " 'name': 'Pojo75',\n"
4444
+ " 'fields': [\n"

avro/src/test/java/tools/jackson/dataformat/avro/AvroTestBase.java

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public abstract class AvroTestBase
4040
+" {\"name\": \"boss\", \"type\": [\"Employee\",\"null\"]}\n"
4141
+"]}";
4242

43-
protected final String EMPLOYEE_ARRAY_SCHEMA_JSON = aposToQuotes(
43+
protected final String EMPLOYEE_ARRAY_SCHEMA_JSON = a2q(
4444
"{"
4545
+"'name': 'EmployeeArray',\n"
4646
+"'type': 'array',\n"
@@ -288,8 +288,8 @@ protected AvroSchema parseSchema(String schemaAsJson) {
288288

289289
protected AvroSchema parseSchema(AvroMapper mapper, String schemaAsJson) {
290290
try {
291-
return getMapper().schemaFrom(aposToQuotes(schemaAsJson));
292-
} catch (IOException e) {
291+
return getMapper().schemaFrom(a2q(schemaAsJson));
292+
} catch (Exception e) {
293293
fail("Could not parse Avro Schema from: "+schemaAsJson+", problem: "+e);
294294
return null;
295295
}
@@ -346,12 +346,22 @@ public static void verifyException(Throwable e, String... matches)
346346
fail("Expected an exception with one of substrings ("+Arrays.asList(matches)+"): got one with message \""+msg+"\"");
347347
}
348348

349-
protected static String quote(String str) {
349+
protected static String a2q(String json) {
350+
return json.replace("'", "\"");
351+
}
352+
353+
protected static String q(String str) {
350354
return "\""+str+"\"";
351355
}
356+
357+
@Deprecated // @since 3.2
358+
protected static String quote(String str) {
359+
return q(str);
360+
}
352361

362+
@Deprecated // @since 3.2
353363
protected static String aposToQuotes(String json) {
354-
return json.replace("'", "\"");
364+
return a2q(json);
355365
}
356366

357367
protected static String asJSON(AvroSchema sch) {

avro/src/test/java/tools/jackson/dataformat/avro/MapWithUnionTest.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package tools.jackson.dataformat.avro;
22

33
import java.io.ByteArrayOutputStream;
4-
import java.io.IOException;
54
import java.util.LinkedHashMap;
65
import java.util.Map;
76

@@ -14,7 +13,7 @@
1413

1514
public class MapWithUnionTest extends AvroTestBase
1615
{
17-
protected final String MAP_WITH_UNION_SCHEMA_JSON = aposToQuotes(
16+
protected final String MAP_WITH_UNION_SCHEMA_JSON = a2q(
1817
"{"
1918
+"'name': 'Map',\n"
2019
+"'type': 'map',\n"
@@ -25,7 +24,7 @@ public class MapWithUnionTest extends AvroTestBase
2524
+"}\n");
2625

2726
// for [dataformats-binary#39]
28-
final static String MAP_CONTAINER_SCHEMA_JSON = aposToQuotes("{\n"
27+
final static String MAP_CONTAINER_SCHEMA_JSON = a2q("{\n"
2928
+" 'namespace': 'com.salesforce.conduit.avro',\n"
3029
+" 'type': 'record',\n"
3130
+" 'name': 'MapContainer',\n"
@@ -105,7 +104,7 @@ public void testRootMapWithUnionSequence() throws Exception
105104
}
106105

107106
@Test
108-
public void testMapContainerWithNested() throws IOException
107+
public void testMapContainerWithNested() throws Exception
109108
{
110109
Map<String,Object> map = new LinkedHashMap<>();
111110
map.put("hello", "world");

avro/src/test/java/tools/jackson/dataformat/avro/RootSequenceTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public class RootSequenceTest extends AvroTestBase
2222
@Test
2323
public void testReadWriteIntSequence() throws Exception
2424
{
25-
AvroSchema schema = MAPPER.schemaFrom(quote("int"));
25+
AvroSchema schema = MAPPER.schemaFrom(q("int"));
2626
ByteArrayOutputStream b = new ByteArrayOutputStream(1000);
2727

2828
// First: write a sequence of 3 root-level ints
@@ -50,7 +50,7 @@ public void testReadWriteIntSequence() throws Exception
5050
@Test
5151
public void testReadWriteStringSequence() throws Exception
5252
{
53-
AvroSchema schema = MAPPER.schemaFrom(quote("string"));
53+
AvroSchema schema = MAPPER.schemaFrom(q("string"));
5454
ByteArrayOutputStream b = new ByteArrayOutputStream(1000);
5555

5656
// First: write a sequence of 3 root-level Strings

avro/src/test/java/tools/jackson/dataformat/avro/RoundtripTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
public class RoundtripTest extends MapTest
1414
{
15-
static String SCHEMA_ISSUE_16 = aposToQuotes("{\n"+
15+
static String SCHEMA_ISSUE_16 = a2q("{\n"+
1616
" 'namespace':'org.example.testsnippets',\n"+
1717
" 'type':'record',\n"+
1818
" 'name':'TestDto',\n"+

avro/src/test/java/tools/jackson/dataformat/avro/ScalarTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public class ScalarTest extends AvroTestBase
1717
public void testRootString() throws Exception
1818
{
1919
final String INPUT = "Something or other";
20-
AvroSchema schema = MAPPER.schemaFrom(quote("string"));
20+
AvroSchema schema = MAPPER.schemaFrom(q("string"));
2121
byte[] avro = MAPPER.writerFor(String.class)
2222
.with(schema)
2323
.writeValueAsBytes(INPUT);
@@ -41,7 +41,7 @@ public void _testRootString(int chunkSize, ObjectReader r, byte[] encoded,
4141
public void testRootInt() throws Exception
4242
{
4343
Integer inputValue = Integer.valueOf(0xE134567);
44-
AvroSchema schema = MAPPER.schemaFrom(quote("int"));
44+
AvroSchema schema = MAPPER.schemaFrom(q("int"));
4545
byte[] avro = MAPPER.writer(schema)
4646
.writeValueAsBytes(inputValue);
4747
ObjectReader r = MAPPER.readerFor(Integer.class)
@@ -164,7 +164,7 @@ public void _testRootFloat(int chunkSize, ObjectReader r, byte[] encoded,
164164
@Test
165165
public void testRootBoolean() throws Exception
166166
{
167-
AvroSchema schema = MAPPER.schemaFrom(quote("boolean"));
167+
AvroSchema schema = MAPPER.schemaFrom(q("boolean"));
168168
byte[] avro = MAPPER.writer(schema)
169169
.writeValueAsBytes(Boolean.TRUE);
170170
Boolean result = MAPPER.readerFor(Boolean.class)

avro/src/test/java/tools/jackson/dataformat/avro/constraints/DeeplyNestedAvroReadTest.java

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,7 @@ public Node(int id, Node next) {
5656
MAPPER_400 = new AvroMapper(f);
5757
}
5858

59-
private final AvroSchema NODE_SCHEMA;
60-
{
61-
try {
62-
NODE_SCHEMA = DEFAULT_MAPPER.schemaFrom(NODE_SCHEMA_JSON);
63-
} catch (IOException e) {
64-
throw new RuntimeException(e);
65-
}
66-
}
59+
private final AvroSchema NODE_SCHEMA = DEFAULT_MAPPER.schemaFrom(NODE_SCHEMA_JSON);
6760

6861
@Test
6962
public void testDeeplyNestedObjectsHighLimits() throws Exception

avro/src/test/java/tools/jackson/dataformat/avro/schemaev/ArrayEvolutionTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public class ArrayEvolutionTest extends AvroTestBase
1313
// NOTE! Avro requires names of record types to match; this is why type names
1414
// are identical despite differences in field composition...
1515

16-
static String SCHEMA_X_ARRAY_JSON = aposToQuotes("{\n"+
16+
static String SCHEMA_X_ARRAY_JSON = a2q("{\n"+
1717
"'name': 'XArray',\n"+
1818
"'type': 'array',\n"+
1919
"'items': {\n"+
@@ -23,7 +23,7 @@ public class ArrayEvolutionTest extends AvroTestBase
2323
" { 'name':'x', 'type':'int' }\n"+
2424
" ]}}");
2525

26-
static String SCHEMA_XY_ARRAY_JSON = aposToQuotes("{\n"+
26+
static String SCHEMA_XY_ARRAY_JSON = a2q("{\n"+
2727
"'name': 'XYArray',\n"+
2828
"'type': 'array',\n"+
2929
"'items': {\n"+
@@ -34,7 +34,7 @@ public class ArrayEvolutionTest extends AvroTestBase
3434
" { 'name':'y', 'type':'int' }\n"+
3535
" ]}}");
3636

37-
static String SCHEMA_XYZ_ARRAY_JSON = aposToQuotes("{\n"+
37+
static String SCHEMA_XYZ_ARRAY_JSON = a2q("{\n"+
3838
"'name': 'XYZArray',\n"+
3939
"'type': 'array',\n"+
4040
"'items': {\n"+

0 commit comments

Comments
 (0)