steven-aerts commented on code in PR #3373:
URL: https://github.com/apache/avro/pull/3373#discussion_r2460758590


##########
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:
   I copied this style from 
[`test_avro_766.c`](https://github.com/steven-aerts/avro/blob/master/lang/c/tests/test_avro_766.c#L60)
 I can remove it from both if desired.



##########
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:
   `json_decref` 
[supports](https://github.com/akheron/jansson/blob/master/src/jansson.h#L131) 
dereferencing `NULL`.
   So I do not think this is a problem.



##########
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:
   No jansson guarantees to take ownership of `branch_json` and will decref on 
errors.



-- 
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