Skip to content

Commit 1faa9f1

Browse files
authored
AVRO-2254: [java] fix unresolved schema name (#2365)
* AVRO-2254: fix unresolved shema name
1 parent 315f28d commit 1faa9f1

File tree

6 files changed

+122
-8
lines changed

6 files changed

+122
-8
lines changed

lang/java/compiler/src/main/java/org/apache/avro/compiler/idl/SchemaResolver.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.util.IdentityHashMap;
2424
import java.util.List;
2525
import java.util.Map;
26+
import java.util.concurrent.atomic.AtomicInteger;
2627
import java.util.function.Function;
2728

2829
import org.apache.avro.Protocol;
@@ -44,6 +45,8 @@ private SchemaResolver() {
4445

4546
private static final String UR_SCHEMA_NS = "org.apache.avro.compiler";
4647

48+
private static final AtomicInteger COUNTER = new AtomicInteger();
49+
4750
/**
4851
* Create a schema to represent a "unresolved" schema. (used to represent a
4952
* schema where the definition is not known at the time) This concept might be
@@ -53,8 +56,8 @@ private SchemaResolver() {
5356
* @return
5457
*/
5558
static Schema unresolvedSchema(final String name) {
56-
Schema schema = Schema.createRecord(UR_SCHEMA_NAME, "unresolved schema", UR_SCHEMA_NS, false,
57-
Collections.EMPTY_LIST);
59+
Schema schema = Schema.createRecord(UR_SCHEMA_NAME + '_' + COUNTER.getAndIncrement(), "unresolved schema",
60+
UR_SCHEMA_NS, false, Collections.EMPTY_LIST);
5861
schema.addProp(UR_SCHEMA_ATTR, name);
5962
return schema;
6063
}
@@ -66,8 +69,8 @@ static Schema unresolvedSchema(final String name) {
6669
* @return
6770
*/
6871
static boolean isUnresolvedSchema(final Schema schema) {
69-
return (schema.getType() == Schema.Type.RECORD && schema.getProp(UR_SCHEMA_ATTR) != null
70-
&& UR_SCHEMA_NAME.equals(schema.getName()) && UR_SCHEMA_NS.equals(schema.getNamespace()));
72+
return (schema.getType() == Schema.Type.RECORD && schema.getProp(UR_SCHEMA_ATTR) != null && schema.getName() != null
73+
&& schema.getName().startsWith(UR_SCHEMA_NAME) && UR_SCHEMA_NS.equals(schema.getNamespace()));
7174
}
7275

7376
/**
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
@namespace("org.apache.avro.gen")
2+
protocol UnionFwd {
3+
4+
record TestRecord {
5+
union {SR1, SR2} unionField;
6+
}
7+
8+
record SR1 {
9+
string field;
10+
}
11+
12+
record SR2 {
13+
string field;
14+
}
15+
16+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"protocol": "UnionFwd",
3+
"namespace": "org.apache.avro.gen",
4+
"types": [
5+
{
6+
"type": "record",
7+
"name": "TestRecord",
8+
"fields": [
9+
{
10+
"name": "unionField",
11+
"type": [
12+
{
13+
"type": "record",
14+
"name": "SR1",
15+
"fields": [
16+
{
17+
"name": "field",
18+
"type": "string"
19+
}
20+
]
21+
},
22+
{
23+
"type": "record",
24+
"name": "SR2",
25+
"fields": [
26+
{
27+
"name": "field",
28+
"type": "string"
29+
}
30+
]
31+
}
32+
]
33+
}
34+
]
35+
}
36+
],
37+
"messages": {}
38+
}

lang/java/idl/src/main/java/org/apache/avro/idl/SchemaResolver.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
import java.util.Collections;
2525
import java.util.Map;
26+
import java.util.concurrent.atomic.AtomicInteger;
2627
import java.util.function.Function;
2728
import java.util.stream.Collectors;
2829

@@ -41,6 +42,8 @@ private SchemaResolver() {
4142

4243
private static final String UR_SCHEMA_NS = "org.apache.avro.compiler";
4344

45+
private static final AtomicInteger COUNTER = new AtomicInteger();
46+
4447
/**
4548
* Create a schema to represent an "unresolved" schema. (used to represent a
4649
* schema whose definition does not exist, yet).
@@ -49,8 +52,8 @@ private SchemaResolver() {
4952
* @return an unresolved schema for the given name
5053
*/
5154
static Schema unresolvedSchema(final String name) {
52-
Schema schema = Schema.createRecord(UR_SCHEMA_NAME, "unresolved schema", UR_SCHEMA_NS, false,
53-
Collections.emptyList());
55+
Schema schema = Schema.createRecord(UR_SCHEMA_NAME + '_' + COUNTER.getAndIncrement(), "unresolved schema",
56+
UR_SCHEMA_NS, false, Collections.emptyList());
5457
schema.addProp(UR_SCHEMA_ATTR, name);
5558
return schema;
5659
}
@@ -62,8 +65,8 @@ static Schema unresolvedSchema(final String name) {
6265
* @return whether the schema is an unresolved schema
6366
*/
6467
static boolean isUnresolvedSchema(final Schema schema) {
65-
return (schema.getType() == Schema.Type.RECORD && schema.getProp(UR_SCHEMA_ATTR) != null
66-
&& UR_SCHEMA_NAME.equals(schema.getName()) && UR_SCHEMA_NS.equals(schema.getNamespace()));
68+
return (schema.getType() == Schema.Type.RECORD && schema.getProp(UR_SCHEMA_ATTR) != null && schema.getName() != null
69+
&& schema.getName().startsWith(UR_SCHEMA_NAME) && UR_SCHEMA_NS.equals(schema.getNamespace()));
6770
}
6871

6972
/**
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
@namespace("org.apache.avro.gen")
2+
protocol UnionFwd {
3+
4+
record TestRecord {
5+
union {SR1, SR2} unionField;
6+
}
7+
8+
record SR1 {
9+
string field;
10+
}
11+
12+
record SR2 {
13+
string field;
14+
}
15+
16+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"protocol": "UnionFwd",
3+
"namespace": "org.apache.avro.gen",
4+
"types": [
5+
{
6+
"type": "record",
7+
"name": "TestRecord",
8+
"fields": [
9+
{
10+
"name": "unionField",
11+
"type": [
12+
{
13+
"type": "record",
14+
"name": "SR1",
15+
"fields": [
16+
{
17+
"name": "field",
18+
"type": "string"
19+
}
20+
]
21+
},
22+
{
23+
"type": "record",
24+
"name": "SR2",
25+
"fields": [
26+
{
27+
"name": "field",
28+
"type": "string"
29+
}
30+
]
31+
}
32+
]
33+
}
34+
]
35+
}
36+
],
37+
"messages": {}
38+
}

0 commit comments

Comments
 (0)