This is an automated email from the ASF dual-hosted git repository.
clesaec pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/avro.git
The following commit(s) were added to refs/heads/master by this push:
new 1faa9f199 AVRO-2254: [java] fix unresolved schema name (#2365)
1faa9f199 is described below
commit 1faa9f1994c954e935fd8f5f12e5906a3b278ece
Author: Christophe Le Saec <[email protected]>
AuthorDate: Tue Sep 19 11:37:40 2023 +0200
AVRO-2254: [java] fix unresolved schema name (#2365)
* AVRO-2254: fix unresolved shema name
---
.../apache/avro/compiler/idl/SchemaResolver.java | 11 ++++---
lang/java/compiler/src/test/idl/input/union.avdl | 16 +++++++++
lang/java/compiler/src/test/idl/output/union.avpr | 38 ++++++++++++++++++++++
.../java/org/apache/avro/idl/SchemaResolver.java | 11 ++++---
lang/java/idl/src/test/idl/input/union.avdl | 16 +++++++++
lang/java/idl/src/test/idl/output/union.avpr | 38 ++++++++++++++++++++++
6 files changed, 122 insertions(+), 8 deletions(-)
diff --git
a/lang/java/compiler/src/main/java/org/apache/avro/compiler/idl/SchemaResolver.java
b/lang/java/compiler/src/main/java/org/apache/avro/compiler/idl/SchemaResolver.java
index 193f87117..6a1a13789 100644
---
a/lang/java/compiler/src/main/java/org/apache/avro/compiler/idl/SchemaResolver.java
+++
b/lang/java/compiler/src/main/java/org/apache/avro/compiler/idl/SchemaResolver.java
@@ -23,6 +23,7 @@ import java.util.Collections;
import java.util.IdentityHashMap;
import java.util.List;
import java.util.Map;
+import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Function;
import org.apache.avro.Protocol;
@@ -44,6 +45,8 @@ final class SchemaResolver {
private static final String UR_SCHEMA_NS = "org.apache.avro.compiler";
+ private static final AtomicInteger COUNTER = new AtomicInteger();
+
/**
* Create a schema to represent a "unresolved" schema. (used to represent a
* schema where the definition is not known at the time) This concept might
be
@@ -53,8 +56,8 @@ final class SchemaResolver {
* @return
*/
static Schema unresolvedSchema(final String name) {
- Schema schema = Schema.createRecord(UR_SCHEMA_NAME, "unresolved schema",
UR_SCHEMA_NS, false,
- Collections.EMPTY_LIST);
+ Schema schema = Schema.createRecord(UR_SCHEMA_NAME + '_' +
COUNTER.getAndIncrement(), "unresolved schema",
+ UR_SCHEMA_NS, false, Collections.EMPTY_LIST);
schema.addProp(UR_SCHEMA_ATTR, name);
return schema;
}
@@ -66,8 +69,8 @@ final class SchemaResolver {
* @return
*/
static boolean isUnresolvedSchema(final Schema schema) {
- return (schema.getType() == Schema.Type.RECORD &&
schema.getProp(UR_SCHEMA_ATTR) != null
- && UR_SCHEMA_NAME.equals(schema.getName()) &&
UR_SCHEMA_NS.equals(schema.getNamespace()));
+ return (schema.getType() == Schema.Type.RECORD &&
schema.getProp(UR_SCHEMA_ATTR) != null && schema.getName() != null
+ && schema.getName().startsWith(UR_SCHEMA_NAME) &&
UR_SCHEMA_NS.equals(schema.getNamespace()));
}
/**
diff --git a/lang/java/compiler/src/test/idl/input/union.avdl
b/lang/java/compiler/src/test/idl/input/union.avdl
new file mode 100644
index 000000000..19f37f2f7
--- /dev/null
+++ b/lang/java/compiler/src/test/idl/input/union.avdl
@@ -0,0 +1,16 @@
+@namespace("org.apache.avro.gen")
+protocol UnionFwd {
+
+ record TestRecord {
+ union {SR1, SR2} unionField;
+ }
+
+ record SR1 {
+ string field;
+ }
+
+ record SR2 {
+ string field;
+ }
+
+}
diff --git a/lang/java/compiler/src/test/idl/output/union.avpr
b/lang/java/compiler/src/test/idl/output/union.avpr
new file mode 100644
index 000000000..61748d179
--- /dev/null
+++ b/lang/java/compiler/src/test/idl/output/union.avpr
@@ -0,0 +1,38 @@
+{
+ "protocol": "UnionFwd",
+ "namespace": "org.apache.avro.gen",
+ "types": [
+ {
+ "type": "record",
+ "name": "TestRecord",
+ "fields": [
+ {
+ "name": "unionField",
+ "type": [
+ {
+ "type": "record",
+ "name": "SR1",
+ "fields": [
+ {
+ "name": "field",
+ "type": "string"
+ }
+ ]
+ },
+ {
+ "type": "record",
+ "name": "SR2",
+ "fields": [
+ {
+ "name": "field",
+ "type": "string"
+ }
+ ]
+ }
+ ]
+ }
+ ]
+ }
+ ],
+ "messages": {}
+}
diff --git
a/lang/java/idl/src/main/java/org/apache/avro/idl/SchemaResolver.java
b/lang/java/idl/src/main/java/org/apache/avro/idl/SchemaResolver.java
index 3130f5a26..315f32221 100644
--- a/lang/java/idl/src/main/java/org/apache/avro/idl/SchemaResolver.java
+++ b/lang/java/idl/src/main/java/org/apache/avro/idl/SchemaResolver.java
@@ -23,6 +23,7 @@ import org.apache.avro.Schema;
import java.util.Collections;
import java.util.Map;
+import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Function;
import java.util.stream.Collectors;
@@ -41,6 +42,8 @@ final class SchemaResolver {
private static final String UR_SCHEMA_NS = "org.apache.avro.compiler";
+ private static final AtomicInteger COUNTER = new AtomicInteger();
+
/**
* Create a schema to represent an "unresolved" schema. (used to represent a
* schema whose definition does not exist, yet).
@@ -49,8 +52,8 @@ final class SchemaResolver {
* @return an unresolved schema for the given name
*/
static Schema unresolvedSchema(final String name) {
- Schema schema = Schema.createRecord(UR_SCHEMA_NAME, "unresolved schema",
UR_SCHEMA_NS, false,
- Collections.emptyList());
+ Schema schema = Schema.createRecord(UR_SCHEMA_NAME + '_' +
COUNTER.getAndIncrement(), "unresolved schema",
+ UR_SCHEMA_NS, false, Collections.emptyList());
schema.addProp(UR_SCHEMA_ATTR, name);
return schema;
}
@@ -62,8 +65,8 @@ final class SchemaResolver {
* @return whether the schema is an unresolved schema
*/
static boolean isUnresolvedSchema(final Schema schema) {
- return (schema.getType() == Schema.Type.RECORD &&
schema.getProp(UR_SCHEMA_ATTR) != null
- && UR_SCHEMA_NAME.equals(schema.getName()) &&
UR_SCHEMA_NS.equals(schema.getNamespace()));
+ return (schema.getType() == Schema.Type.RECORD &&
schema.getProp(UR_SCHEMA_ATTR) != null && schema.getName() != null
+ && schema.getName().startsWith(UR_SCHEMA_NAME) &&
UR_SCHEMA_NS.equals(schema.getNamespace()));
}
/**
diff --git a/lang/java/idl/src/test/idl/input/union.avdl
b/lang/java/idl/src/test/idl/input/union.avdl
new file mode 100644
index 000000000..19f37f2f7
--- /dev/null
+++ b/lang/java/idl/src/test/idl/input/union.avdl
@@ -0,0 +1,16 @@
+@namespace("org.apache.avro.gen")
+protocol UnionFwd {
+
+ record TestRecord {
+ union {SR1, SR2} unionField;
+ }
+
+ record SR1 {
+ string field;
+ }
+
+ record SR2 {
+ string field;
+ }
+
+}
diff --git a/lang/java/idl/src/test/idl/output/union.avpr
b/lang/java/idl/src/test/idl/output/union.avpr
new file mode 100644
index 000000000..61748d179
--- /dev/null
+++ b/lang/java/idl/src/test/idl/output/union.avpr
@@ -0,0 +1,38 @@
+{
+ "protocol": "UnionFwd",
+ "namespace": "org.apache.avro.gen",
+ "types": [
+ {
+ "type": "record",
+ "name": "TestRecord",
+ "fields": [
+ {
+ "name": "unionField",
+ "type": [
+ {
+ "type": "record",
+ "name": "SR1",
+ "fields": [
+ {
+ "name": "field",
+ "type": "string"
+ }
+ ]
+ },
+ {
+ "type": "record",
+ "name": "SR2",
+ "fields": [
+ {
+ "name": "field",
+ "type": "string"
+ }
+ ]
+ }
+ ]
+ }
+ ]
+ }
+ ],
+ "messages": {}
+}