Skip to content
Permalink
Browse files
AVRO-3374: special cases for qualified name (#1688)
  • Loading branch information
clesaec committed Jun 24, 2022
1 parent a8821d3 commit f190f1e7d46f557881c3dc600df26d9d78a0233f
Showing 2 changed files with 54 additions and 1 deletion.
@@ -751,8 +751,32 @@ public void writeName(Names names, JsonGenerator gen) throws IOException {
}

public String getQualified(String defaultSpace) {
return (space == null || space.equals(defaultSpace)) ? name : full;
return this.shouldWriteFull(defaultSpace) ? full : name;
}

/**
* Determine if full name must be written. There are 2 cases for true :
* defaultSpace != from this.space or name is already a Schema.Type (int, array
* ...)
*
* @param defaultSpace : default name space.
* @return true if full name must be written.
*/
private boolean shouldWriteFull(String defaultSpace) {
if (space != null && space.equals(defaultSpace)) {
for (Type schemaType : Type.values()) {
if (schemaType.name.equals(name)) {
// name is a 'Type', so namespace must be written
return true;
}
}
// this.space == defaultSpace
return false;
}
// this.space != defaultSpace, so namespace must be written.
return true;
}

}

private static abstract class NamedSchema extends Schema {
@@ -26,6 +26,7 @@
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

@@ -366,4 +367,32 @@ public void testEnumSymbolAsNull() {
public void testSchemaFieldWithoutSchema() {
new Schema.Field("f", null);
}

@Test
public void testParseRecordWithNameAsType() {
final String schemaString = "{\n \"type\" : \"record\",\n \"name\" : \"ns.int\",\n"
+ " \"fields\" : [ \n {\"name\" : \"value\", \"type\" : \"int\"}, \n"
+ " {\"name\" : \"next\", \"type\" : [ \"null\", \"ns.int\" ]}\n ]\n}";
final Schema schema = new Schema.Parser().parse(schemaString);
String toString = schema.toString(true);

final Schema schema2 = new Schema.Parser().parse(toString);
assertEquals(schema, schema2);
}

@Test
public void testQualifiedName() {
Arrays.stream(Type.values()).forEach((Type t) -> {
final Schema.Name name = new Schema.Name(t.getName(), "space");
assertEquals("space." + t.getName(), name.getQualified("space"));
assertEquals("space." + t.getName(), name.getQualified("otherdefault"));
});
final Schema.Name name = new Schema.Name("name", "space");
assertEquals("name", name.getQualified("space"));
assertEquals("space.name", name.getQualified("otherdefault"));

final Schema.Name nameInt = new Schema.Name("Int", "space");
assertEquals("Int", nameInt.getQualified("space"));
}

}

0 comments on commit f190f1e

Please sign in to comment.