You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@avro.apache.org by fo...@apache.org on 2020/05/09 07:15:48 UTC
[avro] branch master updated: AVRO-2822: Add toString that doesn't
inline referenced schemas (#869)
This is an automated email from the ASF dual-hosted git repository.
fokko pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/avro.git
The following commit(s) were added to refs/heads/master by this push:
new b5a3c44 AVRO-2822: Add toString that doesn't inline referenced schemas (#869)
b5a3c44 is described below
commit b5a3c44d223d781fed2572cb4ed6f11ca431f25f
Author: Robert Yokota <ra...@gmail.com>
AuthorDate: Sat May 9 00:15:40 2020 -0700
AVRO-2822: Add toString that doesn't inline referenced schemas (#869)
---
.../avro/src/main/java/org/apache/avro/Schema.java | 23 +++++++++++++++++++++-
.../src/test/java/org/apache/avro/TestSchema.java | 15 ++++++++++++++
2 files changed, 37 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 ca2bf60..8e152ae 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
@@ -388,12 +388,33 @@ public abstract class Schema extends JsonProperties implements Serializable {
* @param pretty if true, pretty-print JSON.
*/
public String toString(boolean pretty) {
+ return toString(new Names(), pretty);
+ }
+
+ /**
+ * Render this as <a href="https://json.org/">JSON</a>, but without inlining the
+ * referenced schemas.
+ *
+ * @param referencedSchemas referenced schemas
+ * @param pretty if true, pretty-print JSON.
+ */
+ public String toString(Collection<Schema> referencedSchemas, boolean pretty) {
+ Schema.Names names = new Schema.Names();
+ if (referencedSchemas != null) {
+ for (Schema s : referencedSchemas) {
+ names.add(s);
+ }
+ }
+ return toString(names, pretty);
+ }
+
+ String toString(Names names, boolean pretty) {
try {
StringWriter writer = new StringWriter();
JsonGenerator gen = FACTORY.createGenerator(writer);
if (pretty)
gen.useDefaultPrettyPrinter();
- toJson(new Names(), gen);
+ toJson(names, gen);
gen.flush();
return writer.toString();
} catch (IOException e) {
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 039f982..4899b0e 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
@@ -200,4 +200,19 @@ public class TestSchema {
}
}
+ @Test
+ public void testReconstructSchemaStringWithoutInlinedChildReference() {
+ String child = "{\"type\":\"record\"," + "\"name\":\"Child\"," + "\"namespace\":\"org.apache.avro.nested\","
+ + "\"fields\":" + "[{\"name\":\"childField\",\"type\":\"string\"}]}";
+ String parent = "{\"type\":\"record\"," + "\"name\":\"Parent\"," + "\"namespace\":\"org.apache.avro.nested\","
+ + "\"fields\":" + "[{\"name\":\"child\",\"type\":\"Child\"}]}";
+ Schema.Parser parser = new Schema.Parser();
+ Schema childSchema = parser.parse(child);
+ Schema parentSchema = parser.parse(parent);
+ String parentWithoutInlinedChildReference = parentSchema.toString(Collections.singleton(childSchema), false);
+ // The generated string should be the same as the original parent
+ // schema string that did not have the child schema inlined.
+ assertEquals(parent, parentWithoutInlinedChildReference);
+ }
+
}