From 57aae3fd20e98f3efe67102fdf375bfcf0835f0d Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Sun, 6 Jul 2025 02:22:07 -0300 Subject: [PATCH] cgen: fix json decode option alias (fix #24843) (#24853) --- .../tests/json_decode_option_alias_field_test.v | 16 ++++++++++++++++ vlib/v/gen/c/cgen.v | 5 +++-- 2 files changed, 19 insertions(+), 2 deletions(-) create mode 100644 vlib/json/tests/json_decode_option_alias_field_test.v diff --git a/vlib/json/tests/json_decode_option_alias_field_test.v b/vlib/json/tests/json_decode_option_alias_field_test.v new file mode 100644 index 0000000000..9d0a60a4f7 --- /dev/null +++ b/vlib/json/tests/json_decode_option_alias_field_test.v @@ -0,0 +1,16 @@ +module main + +import json + +struct Req { + height ?int + width ?i32 +} + +const payload = '{}' + +fn test_main() { + r := json.decode(Req, payload) or { panic(err) } + assert r.height == none + assert r.width == none +} diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index d6a28aa994..40eff451e2 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -7364,9 +7364,10 @@ fn (mut g Gen) type_default_impl(typ_ ast.Type, decode_sumtype bool) string { if sym.language in [.c, .v] { for field in info.fields { field_sym := g.table.sym(field.typ) - if field.has_default_expr + is_option := field.typ.has_flag(.option) + if is_option || field.has_default_expr || field_sym.kind in [.enum, .array_fixed, .array, .map, .string, .bool, .alias, .i8, .i16, .int, .i64, .u8, .u16, .u32, .u64, .f32, .f64, .char, .voidptr, .byteptr, .charptr, .struct, .chan, .sum_type] { - if sym.language == .c && !field.has_default_expr { + if sym.language == .c && !field.has_default_expr && !is_option { continue } field_name := c_name(field.name)