[
https://issues.apache.org/jira/browse/AVRO-2035?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16374607#comment-16374607
]
ASF GitHub Bot commented on AVRO-2035:
--------------------------------------
cutting closed pull request #288: AVRO-2035. Java: enable default validation
for schemas created by constructors.
URL: https://github.com/apache/avro/pull/288
This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:
As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):
diff --git a/lang/java/avro/src/main/java/org/apache/avro/Schema.java
b/lang/java/avro/src/main/java/org/apache/avro/Schema.java
index 97aed83cc..9333b7984 100644
--- a/lang/java/avro/src/main/java/org/apache/avro/Schema.java
+++ b/lang/java/avro/src/main/java/org/apache/avro/Schema.java
@@ -399,11 +399,17 @@ public Field(String name, Schema schema, String doc,
@Deprecated
public Field(String name, Schema schema, String doc,
JsonNode defaultValue, Order order) {
+ this(name, schema, doc, defaultValue, true, order);
+ }
+ public Field(String name, Schema schema, String doc,
+ JsonNode defaultValue, boolean validateDefault, Order order) {
super(FIELD_RESERVED);
this.name = validateName(name);
this.schema = schema;
this.doc = doc;
- this.defaultValue = validateDefault(name, schema, defaultValue);
+ this.defaultValue = validateDefault
+ ? validateDefault(name, schema, defaultValue)
+ : defaultValue;
this.order = order;
}
/**
@@ -1159,7 +1165,7 @@ private static String validateName(String name) {
private static final ThreadLocal<Boolean> VALIDATE_DEFAULTS
= new ThreadLocal<Boolean>() {
@Override protected Boolean initialValue() {
- return false;
+ return true;
}
};
diff --git a/lang/java/avro/src/test/java/org/apache/avro/TestSchemas.java
b/lang/java/avro/src/test/java/org/apache/avro/TestSchemas.java
index 271c5fd55..39dd1a241 100644
--- a/lang/java/avro/src/test/java/org/apache/avro/TestSchemas.java
+++ b/lang/java/avro/src/test/java/org/apache/avro/TestSchemas.java
@@ -97,10 +97,10 @@
.setFields(list(new Field("a", INT_SCHEMA, null, 0), new Field("b",
INT_SCHEMA, null, 0)));
A_DINT_B_DFIXED_4_BYTES_RECORD1.setFields(list(
new Field("a", INT_SCHEMA, null, 0),
- new Field("b", FIXED_4_BYTES, null, 0)));
+ new Field("b", FIXED_4_BYTES, null, null)));
A_DINT_B_DFIXED_8_BYTES_RECORD1.setFields(list(
new Field("a", INT_SCHEMA, null, 0),
- new Field("b", FIXED_8_BYTES, null, 0)));
+ new Field("b", FIXED_8_BYTES, null, null)));
A_DINT_B_DINT_STRING_UNION_RECORD1.setFields(list(
new Field("a", INT_SCHEMA, null, 0),
new Field("b", INT_STRING_UNION_SCHEMA, null, 0)));
@@ -109,10 +109,10 @@
new Field("b", INT_UNION_SCHEMA, null, 0)));
A_DINT_B_DENUM_1_RECORD1.setFields(list(
new Field("a", INT_SCHEMA, null, 0),
- new Field("b", ENUM1_AB_SCHEMA, null, 0)));
+ new Field("b", ENUM1_AB_SCHEMA, null, null)));
A_DINT_B_DENUM_2_RECORD1.setFields(list(
new Field("a", INT_SCHEMA, null, 0),
- new Field("b", ENUM2_AB_SCHEMA, null, 0)));
+ new Field("b", ENUM2_AB_SCHEMA, null, null)));
}
// Recursive records
diff --git
a/lang/java/compiler/src/main/javacc/org/apache/avro/compiler/idl/idl.jj
b/lang/java/compiler/src/main/javacc/org/apache/avro/compiler/idl/idl.jj
index 7de123e20..290f0190e 100644
--- a/lang/java/compiler/src/main/javacc/org/apache/avro/compiler/idl/idl.jj
+++ b/lang/java/compiler/src/main/javacc/org/apache/avro/compiler/idl/idl.jj
@@ -1324,7 +1324,8 @@ void VariableDeclarator(Schema type, List<Field> fields):
for (String key : props.keySet())
if ("order".equals(key))
order =
Field.Order.valueOf(getTextProp(key,props,token).toUpperCase());
- Field field = new Field(name, type, getDoc(), defaultValue, order);
+ boolean validate = !SchemaResolver.isUnresolvedSchema(type);
+ Field field = new Field(name,type, getDoc(), defaultValue, validate,
order);
for (String key : props.keySet())
if ("order".equals(key)) { // already handled: ignore
} else if ("aliases".equals(key)) { // aliases
diff --git a/lang/java/compiler/src/test/idl/input/simple.avdl
b/lang/java/compiler/src/test/idl/input/simple.avdl
index 1bfcd7b27..0ad7c7e37 100644
--- a/lang/java/compiler/src/test/idl/input/simple.avdl
+++ b/lang/java/compiler/src/test/idl/input/simple.avdl
@@ -50,8 +50,8 @@ protocol Simple {
date d = 0;
time_ms t = 0;
- @foo.bar("bar.foo") long l;
- union {null, @foo.foo.bar(42) @foo.foo.foo("3foo") string}
nested_properties;
+ @foo.bar("bar.foo") long l = 0;
+ union {null, @foo.foo.bar(42) @foo.foo.foo("3foo") string} prop = null;
}
error TestError {
diff --git a/lang/java/compiler/src/test/idl/output/simple.avpr
b/lang/java/compiler/src/test/idl/output/simple.avpr
index f40030790..ae155f53b 100644
--- a/lang/java/compiler/src/test/idl/output/simple.avpr
+++ b/lang/java/compiler/src/test/idl/output/simple.avpr
@@ -56,10 +56,12 @@
"default": 0
} , {
"name": "l",
- "type": {"type": "long", "foo.bar": "bar.foo"}
+ "type": {"type": "long", "foo.bar": "bar.foo"},
+ "default": 0
} , {
- "name": "nested_properties",
- "type": [ "null" , {"type":"string", "foo.foo.bar": 42, "foo.foo.foo":
"3foo"} ]
+ "name": "prop",
+ "type": [ "null" , {"type":"string", "foo.foo.bar": 42, "foo.foo.foo":
"3foo"} ],
+ "default": null
}],
"my-property" : {
"key" : 3
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
> enable validation of default values in schemas by default
> ---------------------------------------------------------
>
> Key: AVRO-2035
> URL: https://issues.apache.org/jira/browse/AVRO-2035
> Project: Avro
> Issue Type: Bug
> Components: java
> Affects Versions: 1.8.1
> Reporter: radai rosenblatt
> Assignee: Doug Cutting
> Priority: Major
> Fix For: 1.9.0
>
> Attachments: AVRO-2035.patch
>
>
> suppose i have the following schema evolution:
> {code}
> {
> "name": "Bob",
> "type": "record",
> "fields": [
> {"name": "f1", "type": "int"}
> ]
> }
> {code}
> and then:
> {code}
> {
> "name": "Bob",
> "type": "record",
> "fields": [
> {"name": "f1", "type": "int"},
> {"name": "f2", "type": "boolean", "default": "true"}
> ]
> }
> {code}
> the default value for "f2" is specified as the _STRING_ "true" (and not the
> literal boolean true).
> if this default value is ever accessed (when reading a gen1-serialized object
> as a gen2) we get this:
> {code}
> org.apache.avro.AvroTypeException: Non-boolean default for boolean: "true"
> at
> org.apache.avro.io.parsing.ResolvingGrammarGenerator.encode(ResolvingGrammarGenerator.java:408)
> at
> org.apache.avro.io.parsing.ResolvingGrammarGenerator.getBinary(ResolvingGrammarGenerator.java:307)
> at
> org.apache.avro.io.parsing.ResolvingGrammarGenerator.resolveRecords(ResolvingGrammarGenerator.java:285)
> at
> org.apache.avro.io.parsing.ResolvingGrammarGenerator.generate(ResolvingGrammarGenerator.java:118)
> at
> org.apache.avro.io.parsing.ResolvingGrammarGenerator.generate(ResolvingGrammarGenerator.java:50)
> at org.apache.avro.io.ResolvingDecoder.resolve(ResolvingDecoder.java:85)
> at org.apache.avro.io.ResolvingDecoder.<init>(ResolvingDecoder.java:49)
> at
> org.apache.avro.io.DecoderFactory.resolvingDecoder(DecoderFactory.java:307)
> at
> org.apache.avro.generic.GenericDatumReader.getResolver(GenericDatumReader.java:127)
> at
> org.apache.avro.generic.GenericDatumReader.read(GenericDatumReader.java:142)
> {code}
> yet Schema.parse() passes for this
--
This message was sent by Atlassian JIRA
(v7.6.3#76005)