martin-g commented on code in PR #3373:
URL: https://github.com/apache/avro/pull/3373#discussion_r2454346878


##########
lang/c/src/value-json.c:
##########
@@ -352,26 +354,40 @@ avro_value_to_json_t(const avro_value_t *value)
                        union_schema = avro_value_get_schema(value);
                        branch_schema =
                            avro_schema_union_branch(union_schema, disc);
+
+                       namespace = avro_schema_namespace(branch_schema);
                        branch_name = avro_schema_type_name(branch_schema);
+                       if (namespace != NULL) {
+                               full_name_buf = json_sprintf("%s.%s", 
namespace, branch_name);
+                               if (full_name_buf == NULL) {
+                                       avro_set_error("Cannot allocate full 
name union");
+                                       return NULL;
+                               }
+                               branch_name = json_string_value(full_name_buf);
+                       }
 
                        json_t  *result = json_object();
                        if (result == NULL) {
                                avro_set_error("Cannot allocate JSON union");
+                               json_decref(full_name_buf);

Review Comment:
   `full_name_buf` could be NULL if there is no namespace. Maybe we need to 
check before calling json_decref() ?!
   Same issues below.



##########
lang/c/tests/test_avro_4135.c:
##########
@@ -0,0 +1,110 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+ * implied.  See the License for the specific language governing
+ * permissions and limitations under the License.
+ */
+
+#include <stdio.h>
+#include <string.h>
+#include <avro.h>
+
+#define check_exit(call) \
+       do { \
+               int  __rc = call; \
+               if (__rc != 0) { \
+                       fprintf(stderr, "Unexpected error:\n  %s\n  %s\n", \
+                               avro_strerror(), #call); \
+                       exit(EXIT_FAILURE); \
+               } \
+       } while (0)
+
+void check_json_encoding(const avro_value_t * val, const char * expected) {
+       char * actual;
+
+       check_exit(avro_value_to_json(val, 1, &actual));
+       if (strcmp(expected, actual) != 0) {
+               fprintf(stderr, "json encoding mismatch\n expected: %s\n 
actual:  %s\n", expected, actual);
+               exit(EXIT_FAILURE);
+       }
+       free(actual);
+}
+
+int main(int argc, char **argv)
+{
+       const char  *json =
+               "{"
+               "  \"type\": \"record\","
+               "  \"name\": \"r\","
+               "  \"fields\": ["
+               "    { \"name\": \"field\", \"type\": ["
+               "       \"null\","
+               "       \"int\","
+               "       {"
+               "         \"type\": \"record\","
+               "         \"name\": \"r1\","
+               "         \"fields\": []"
+           "       },"
+               "       {"
+               "         \"type\": \"record\","
+               "         \"name\": \"r2\","
+               "         \"namespace\": \"space\","
+               "         \"fields\": []"
+           "       }"
+               "     ]}"
+               "  ]"
+               "}";
+
+       avro_schema_t schema = NULL;
+       avro_schema_error_t error;
+
+       (void) argc;
+       (void) argv;
+
+       check_exit(avro_schema_from_json(json, strlen(json), &schema, &error));
+
+       avro_value_iface_t  *iface = avro_generic_class_from_schema(schema);
+       avro_value_t  val;
+       check_exit(avro_generic_value_new(iface, &val));
+
+#define TEST_AVRO_4135 (1)
+       #if TEST_AVRO_4135

Review Comment:
   Why is this done here ?
   Do you need to disable the rest sometimes ?!



##########
lang/c/tests/test_avro_4135.c:
##########
@@ -0,0 +1,110 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+ * implied.  See the License for the specific language governing
+ * permissions and limitations under the License.
+ */
+
+#include <stdio.h>
+#include <string.h>
+#include <avro.h>
+
+#define check_exit(call) \
+       do { \
+               int  __rc = call; \
+               if (__rc != 0) { \
+                       fprintf(stderr, "Unexpected error:\n  %s\n  %s\n", \
+                               avro_strerror(), #call); \
+                       exit(EXIT_FAILURE); \
+               } \
+       } while (0)
+
+void check_json_encoding(const avro_value_t * val, const char * expected) {
+       char * actual;
+
+       check_exit(avro_value_to_json(val, 1, &actual));
+       if (strcmp(expected, actual) != 0) {
+               fprintf(stderr, "json encoding mismatch\n expected: %s\n 
actual:  %s\n", expected, actual);
+               exit(EXIT_FAILURE);
+       }
+       free(actual);
+}
+
+int main(int argc, char **argv)
+{
+       const char  *json =
+               "{"
+               "  \"type\": \"record\","
+               "  \"name\": \"r\","
+               "  \"fields\": ["
+               "    { \"name\": \"field\", \"type\": ["
+               "       \"null\","
+               "       \"int\","
+               "       {"
+               "         \"type\": \"record\","
+               "         \"name\": \"r1\","
+               "         \"fields\": []"
+           "       },"
+               "       {"
+               "         \"type\": \"record\","
+               "         \"name\": \"r2\","
+               "         \"namespace\": \"space\","
+               "         \"fields\": []"
+           "       }"
+               "     ]}"
+               "  ]"
+               "}";
+
+       avro_schema_t schema = NULL;
+       avro_schema_error_t error;
+
+       (void) argc;
+       (void) argv;
+
+       check_exit(avro_schema_from_json(json, strlen(json), &schema, &error));
+
+       avro_value_iface_t  *iface = avro_generic_class_from_schema(schema);
+       avro_value_t  val;
+       check_exit(avro_generic_value_new(iface, &val));
+
+#define TEST_AVRO_4135 (1)
+       #if TEST_AVRO_4135
+       {
+               avro_value_t  field;
+               avro_value_t  branch;
+               char  *json_encoding;
+
+               avro_value_get_by_index(&val, 0, &field, NULL);
+               check_exit(avro_value_set_branch(&field, 0, &branch));
+               avro_value_set_null(&branch);
+               check_json_encoding(&val, "{\"field\": null}");
+
+               check_exit(avro_value_set_branch(&field, 1, &branch));
+               avro_value_set_int(&branch, 42);
+               check_exit(avro_value_to_json(&val, 1, &json_encoding));
+               check_json_encoding(&val, "{\"field\": {\"int\": 42}}");
+
+               check_exit(avro_value_set_branch(&field, 2, &branch));
+               check_exit(avro_value_to_json(&val, 1, &json_encoding));
+               check_json_encoding(&val, "{\"field\": {\"r1\": {}}}");
+
+               check_exit(avro_value_set_branch(&field, 3, &branch));
+               check_exit(avro_value_to_json(&val, 3, &json_encoding));

Review Comment:
   Do we need to free `json_encoding` at the end ?



##########
lang/c/src/value-json.c:
##########
@@ -352,26 +354,40 @@ avro_value_to_json_t(const avro_value_t *value)
                        union_schema = avro_value_get_schema(value);
                        branch_schema =
                            avro_schema_union_branch(union_schema, disc);
+
+                       namespace = avro_schema_namespace(branch_schema);
                        branch_name = avro_schema_type_name(branch_schema);
+                       if (namespace != NULL) {
+                               full_name_buf = json_sprintf("%s.%s", 
namespace, branch_name);
+                               if (full_name_buf == NULL) {
+                                       avro_set_error("Cannot allocate full 
name union");
+                                       return NULL;
+                               }
+                               branch_name = json_string_value(full_name_buf);
+                       }
 
                        json_t  *result = json_object();
                        if (result == NULL) {
                                avro_set_error("Cannot allocate JSON union");
+                               json_decref(full_name_buf);
                                return NULL;
                        }
 
                        json_t  *branch_json = avro_value_to_json_t(&branch);
                        if (branch_json == NULL) {
                                json_decref(result);
+                               json_decref(full_name_buf);
                                return NULL;
                        }
 
                        if (json_object_set_new(result, branch_name, 
branch_json)) {
                                avro_set_error("Cannot append branch to union");
                                json_decref(result);
+                               json_decref(full_name_buf);

Review Comment:
   Do we need to do the same for `branch_json` too ?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to