From 47e75c68a9aa4b0af61759f4de5470fa8e7b3cdd Mon Sep 17 00:00:00 2001 From: yuyi Date: Mon, 22 Aug 2022 16:04:58 +0800 Subject: [PATCH] cgen: fix json.encode of a struct containing a field of an alias type of another struct (#15490) --- vlib/json/json_test.v | 20 ++++++++++++++++++++ vlib/v/gen/c/json.v | 2 +- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/vlib/json/json_test.v b/vlib/json/json_test.v index 5ebb39a5b1..21ffd84234 100644 --- a/vlib/json/json_test.v +++ b/vlib/json/json_test.v @@ -488,3 +488,23 @@ struct StByteArray { fn test_byte_array() { assert json.encode(StByteArray{ ba: [byte(1), 2, 3, 4, 5] }) == '{"ba":[1,2,3,4,5]}' } + +struct Aa { + sub SumType +} + +struct Bb { + a int +} + +type SumType = Bb + +fn test_encode_alias_field() { + s := json.encode(Aa{ + sub: SumType(Bb{ + a: 1 + }) + }) + println(s) + assert s == '{"sub":{"a":1}}' +} diff --git a/vlib/v/gen/c/json.v b/vlib/v/gen/c/json.v index 0d3178098e..485633a0b7 100644 --- a/vlib/v/gen/c/json.v +++ b/vlib/v/gen/c/json.v @@ -436,7 +436,7 @@ fn (mut g Gen) gen_struct_enc_dec(type_info ast.TypeInfo, styp string, mut enc s } dec.writeln('\t}') } else { - g.gen_json_for_type(field.typ) + g.gen_json_for_type(alias.parent_type) tmp := g.new_tmp_var() gen_js_get_opt(dec_name, field_type, styp, tmp, name, mut dec, is_required) dec.writeln('\tif (jsonroot_$tmp) {')