From 7673e8da822a9e27d7ff3e6258f91e2369b9f620 Mon Sep 17 00:00:00 2001 From: Oscar Westra van Holthe - Kind Date: Wed, 13 Apr 2022 21:07:44 +0200 Subject: [PATCH] AVRO-265: Protocol.toJson writes null namespace (#1638) This change fixes this cosmetic bug. --- .../main/java/org/apache/avro/Protocol.java | 25 ++++++++++--------- .../src/test/idl/output/reservedwords.avpr | 1 - .../compiler/src/test/idl/output/unicode.avpr | 1 - 3 files changed, 13 insertions(+), 14 deletions(-) diff --git a/lang/java/avro/src/main/java/org/apache/avro/Protocol.java b/lang/java/avro/src/main/java/org/apache/avro/Protocol.java index 914beb8e829..ff996889517 100644 --- a/lang/java/avro/src/main/java/org/apache/avro/Protocol.java +++ b/lang/java/avro/src/main/java/org/apache/avro/Protocol.java @@ -17,30 +17,29 @@ */ package org.apache.avro; +import com.fasterxml.jackson.core.JsonGenerator; +import com.fasterxml.jackson.core.JsonParser; +import com.fasterxml.jackson.databind.JsonNode; +import org.apache.avro.Schema.Field; +import org.apache.avro.Schema.Field.Order; + import java.io.ByteArrayInputStream; import java.io.File; +import java.io.IOException; import java.io.InputStream; import java.io.StringWriter; import java.nio.charset.StandardCharsets; -import java.io.IOException; import java.security.MessageDigest; import java.util.ArrayList; import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.HashSet; import java.util.Iterator; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; -import java.util.Collection; -import java.util.Collections; import java.util.Set; -import java.util.HashSet; - -import org.apache.avro.Schema.Field; -import org.apache.avro.Schema.Field.Order; - -import com.fasterxml.jackson.core.JsonGenerator; -import com.fasterxml.jackson.core.JsonParser; -import com.fasterxml.jackson.databind.JsonNode; /** * A set of messages forming an application protocol. @@ -417,7 +416,9 @@ void toJson(JsonGenerator gen) throws IOException { gen.writeStartObject(); gen.writeStringField("protocol", name); - gen.writeStringField("namespace", namespace); + if (namespace != null) { + gen.writeStringField("namespace", namespace); + } if (doc != null) gen.writeStringField("doc", doc); diff --git a/lang/java/compiler/src/test/idl/output/reservedwords.avpr b/lang/java/compiler/src/test/idl/output/reservedwords.avpr index 96f5b0febe7..f28a90cc8b3 100644 --- a/lang/java/compiler/src/test/idl/output/reservedwords.avpr +++ b/lang/java/compiler/src/test/idl/output/reservedwords.avpr @@ -1,6 +1,5 @@ { "protocol" : "Foo", - "namespace" : null, "doc" : "Licensed to the Apache Software Foundation (ASF) under one\nor more contributor license agreements. See the NOTICE file\ndistributed with this work for additional information\nregarding copyright ownership. The ASF licenses this file\nto you under the Apache License, Version 2.0 (the\n\"License\"); you may not use this file except in compliance\nwith the License. You may obtain a copy of the License at\n\n https://www.apache.org/licenses/LICENSE-2.0\n\nUnless required by applicable law or agreed to in writing, software\ndistributed under the License is distributed on an \"AS IS\" BASIS,\nWITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\nSee the License for the specific language governing permissions and\nlimitations under the License.", "types" : [ ], "messages" : { diff --git a/lang/java/compiler/src/test/idl/output/unicode.avpr b/lang/java/compiler/src/test/idl/output/unicode.avpr index 67642e2189a..c58a043003b 100644 --- a/lang/java/compiler/src/test/idl/output/unicode.avpr +++ b/lang/java/compiler/src/test/idl/output/unicode.avpr @@ -1,6 +1,5 @@ { "protocol" : "Протоколы", - "namespace" : null, "doc" : "This is a test that UTF8 functions correctly.\nこのテストでは、UTF - 8で正しく機能している。\n这是一个测试,UTF - 8的正常运行。", "types" : [ { "type" : "record",