You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@avro.apache.org by cu...@apache.org on 2010/08/23 22:39:17 UTC
svn commit: r988304 - in /avro/trunk/lang/java/src:
java/org/apache/avro/io/parsing/ResolvingGrammarGenerator.java
test/java/org/apache/avro/TestSchema.java
Author: cutting
Date: Mon Aug 23 20:39:16 2010
New Revision: 988304
URL: http://svn.apache.org/viewvc?rev=988304&view=rev
Log:
AVRO-617. Java: Detect erroneous default field values.
Modified:
avro/trunk/lang/java/src/java/org/apache/avro/io/parsing/ResolvingGrammarGenerator.java
avro/trunk/lang/java/src/test/java/org/apache/avro/TestSchema.java
Modified: avro/trunk/lang/java/src/java/org/apache/avro/io/parsing/ResolvingGrammarGenerator.java
URL: http://svn.apache.org/viewvc/avro/trunk/lang/java/src/java/org/apache/avro/io/parsing/ResolvingGrammarGenerator.java?rev=988304&r1=988303&r2=988304&view=diff
==============================================================================
--- avro/trunk/lang/java/src/java/org/apache/avro/io/parsing/ResolvingGrammarGenerator.java (original)
+++ avro/trunk/lang/java/src/java/org/apache/avro/io/parsing/ResolvingGrammarGenerator.java Mon Aug 23 20:39:16 2010
@@ -341,6 +341,8 @@ public class ResolvingGrammarGenerator e
encode(e, s.getTypes().get(0), n);
break;
case FIXED:
+ if (!n.isTextual())
+ throw new AvroTypeException("Non-string default value for fixed: "+n);
byte[] bb = n.getTextValue().getBytes("ISO-8859-1");
if (bb.length != s.getFixedSize()) {
bb = Arrays.copyOf(bb, s.getFixedSize());
@@ -348,27 +350,43 @@ public class ResolvingGrammarGenerator e
e.writeFixed(bb);
break;
case STRING:
+ if (!n.isTextual())
+ throw new AvroTypeException("Non-string default value for string: "+n);
e.writeString(n.getTextValue());
break;
case BYTES:
+ if (!n.isTextual())
+ throw new AvroTypeException("Non-string default value for bytes: "+n);
e.writeBytes(n.getTextValue().getBytes("ISO-8859-1"));
break;
case INT:
+ if (!n.isNumber())
+ throw new AvroTypeException("Non-numeric default value for int: "+n);
e.writeInt(n.getIntValue());
break;
case LONG:
+ if (!n.isNumber())
+ throw new AvroTypeException("Non-numeric default value for long: "+n);
e.writeLong(n.getLongValue());
break;
case FLOAT:
+ if (!n.isNumber())
+ throw new AvroTypeException("Non-numeric default value for float: "+n);
e.writeFloat((float) n.getDoubleValue());
break;
case DOUBLE:
+ if (!n.isNumber())
+ throw new AvroTypeException("Non-numeric default value for double: "+n);
e.writeDouble(n.getDoubleValue());
break;
case BOOLEAN:
+ if (!n.isBoolean())
+ throw new AvroTypeException("Non-boolean default for boolean: "+n);
e.writeBoolean(n.getBooleanValue());
break;
case NULL:
+ if (!n.isNull())
+ throw new AvroTypeException("Non-null default value for null type: "+n);
e.writeNull();
break;
}
Modified: avro/trunk/lang/java/src/test/java/org/apache/avro/TestSchema.java
URL: http://svn.apache.org/viewvc/avro/trunk/lang/java/src/test/java/org/apache/avro/TestSchema.java?rev=988304&r1=988303&r2=988304&view=diff
==============================================================================
--- avro/trunk/lang/java/src/test/java/org/apache/avro/TestSchema.java (original)
+++ avro/trunk/lang/java/src/test/java/org/apache/avro/TestSchema.java Mon Aug 23 20:39:16 2010
@@ -228,6 +228,25 @@ public class TestSchema {
check("[\"string\", \"long\"]", false);
checkDefault("[\"double\", \"long\"]", "1.1", new Double(1.1));
+ // test that erroneous default values cause errors
+ for (String type : new String[]
+ {"int", "long", "float", "double", "string", "bytes", "boolean"}) {
+ boolean error = false;
+ try {
+ checkDefault("[\""+type+"\", \"null\"]", "null", 0);
+ } catch (AvroTypeException e) {
+ error = true;
+ }
+ assertTrue(error);
+ error = false;
+ try {
+ checkDefault("[\"null\", \""+type+"\"]", "0", null);
+ } catch (AvroTypeException e) {
+ error = true;
+ }
+ assertTrue(error);
+ }
+
// check union json
String record = "{\"type\":\"record\",\"name\":\"Foo\",\"fields\":[]}";
String fixed = "{\"type\":\"fixed\",\"name\":\"Bar\",\"size\": 1}";