-
Notifications
You must be signed in to change notification settings - Fork 1.7k
AVRO-4135: [C] full type name for json encoded unions #3373
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -337,10 +337,12 @@ avro_value_to_json_t(const avro_value_t *value) | |
| case AVRO_UNION: | ||
| { | ||
| int disc; | ||
| avro_value_t branch; | ||
| avro_value_t branch; | ||
| avro_schema_t union_schema; | ||
| avro_schema_t branch_schema; | ||
| const char *branch_name; | ||
| const char *branch_name; | ||
| const char *namespace; | ||
| json_t *full_name_buf = NULL; | ||
|
|
||
| check_return(NULL, avro_value_get_current_branch(value, &branch)); | ||
|
|
||
|
|
@@ -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); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| 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); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need to do the same for
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No jansson guarantees to take ownership of |
||
| return NULL; | ||
| } | ||
|
|
||
| json_decref(full_name_buf); | ||
| return result; | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| /* | ||
| * 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 | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is this done here ?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I copied this style from |
||
| { | ||
| 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)); | ||
steven-aerts marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| free(json_encoding); | ||
| check_json_encoding(&val, "{\"field\": {\"space.r2\": {}}}"); | ||
| } | ||
| #endif | ||
|
|
||
| avro_value_decref(&val); | ||
| avro_schema_decref(schema); | ||
| return 0; | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://github.com/steven-aerts/avro/blob/46d87d9aa8964684e602f114292825b9fd5c2554/lang/c/src/schema.c#L1546-L1555
It seems avro_schema_namespace does not support schema reference, so it will return NULL even if the referenced schema has a namespace.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not exactly clear to me what you mean here. In the use cases I tested it with it works.
Not clear which use case is missing.