Skip to content

Commit

Permalink
AVRO-2254: [java] fix unresolved schema name (#2365)
Browse files Browse the repository at this point in the history
* AVRO-2254: fix unresolved shema name
  • Loading branch information
clesaec authored Sep 19, 2023
1 parent 315f28d commit 1faa9f1
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
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;
Expand All @@ -44,6 +45,8 @@ private 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
Expand All @@ -53,8 +56,8 @@ private 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;
}
Expand All @@ -66,8 +69,8 @@ static Schema unresolvedSchema(final String name) {
* @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()));
}

/**
Expand Down
16 changes: 16 additions & 0 deletions lang/java/compiler/src/test/idl/input/union.avdl
Original file line number Diff line number Diff line change
@@ -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;
}

}
38 changes: 38 additions & 0 deletions lang/java/compiler/src/test/idl/output/union.avpr
Original file line number Diff line number Diff line change
@@ -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": {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import java.util.Collections;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Function;
import java.util.stream.Collectors;

Expand All @@ -41,6 +42,8 @@ private 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).
Expand All @@ -49,8 +52,8 @@ private 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;
}
Expand All @@ -62,8 +65,8 @@ static Schema unresolvedSchema(final String name) {
* @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()));
}

/**
Expand Down
16 changes: 16 additions & 0 deletions lang/java/idl/src/test/idl/input/union.avdl
Original file line number Diff line number Diff line change
@@ -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;
}

}
38 changes: 38 additions & 0 deletions lang/java/idl/src/test/idl/output/union.avpr
Original file line number Diff line number Diff line change
@@ -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": {}
}

0 comments on commit 1faa9f1

Please sign in to comment.