You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@avro.apache.org by rs...@apache.org on 2022/06/24 15:12:28 UTC

[avro] branch branch-1.11 updated: AVRO-3374: special cases for qualified name (#1688)

This is an automated email from the ASF dual-hosted git repository.

rskraba pushed a commit to branch branch-1.11
in repository https://gitbox.apache.org/repos/asf/avro.git


The following commit(s) were added to refs/heads/branch-1.11 by this push:
     new ea3eb118e AVRO-3374: special cases for qualified name (#1688)
ea3eb118e is described below

commit ea3eb118e80e0cc66964963bea721c0e487990be
Author: clesaec <51...@users.noreply.github.com>
AuthorDate: Fri Jun 24 17:08:25 2022 +0200

    AVRO-3374: special cases for qualified name (#1688)
---
 .../avro/src/main/java/org/apache/avro/Schema.java | 26 ++++++++++++++++++-
 .../src/test/java/org/apache/avro/TestSchema.java  | 29 ++++++++++++++++++++++
 2 files changed, 54 insertions(+), 1 deletion(-)

diff --git a/lang/java/avro/src/main/java/org/apache/avro/Schema.java b/lang/java/avro/src/main/java/org/apache/avro/Schema.java
index ae0015d9d..f6c3de768 100644
--- a/lang/java/avro/src/main/java/org/apache/avro/Schema.java
+++ b/lang/java/avro/src/main/java/org/apache/avro/Schema.java
@@ -751,8 +751,32 @@ public abstract class Schema extends JsonProperties implements Serializable {
     }
 
     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 {
diff --git a/lang/java/avro/src/test/java/org/apache/avro/TestSchema.java b/lang/java/avro/src/test/java/org/apache/avro/TestSchema.java
index acfbcb568..95cb36746 100644
--- a/lang/java/avro/src/test/java/org/apache/avro/TestSchema.java
+++ b/lang/java/avro/src/test/java/org/apache/avro/TestSchema.java
@@ -26,6 +26,7 @@ import java.io.InputStream;
 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 class TestSchema {
   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"));
+  }
+
 }