Skip to content

Commit

Permalink
AVRO-2254: fix unresolved shema name
Browse files Browse the repository at this point in the history
  • Loading branch information
clesaec committed Jul 18, 2023
1 parent 5e6cec1 commit 8a98795
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 4 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": {}
}

0 comments on commit 8a98795

Please sign in to comment.