cgen: fix json decode option alias (fix #24843) (#24853)

This commit is contained in:
Felipe Pena 2025-07-06 02:22:07 -03:00 committed by GitHub
parent 8f15d65168
commit 57aae3fd20
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 19 additions and 2 deletions

View File

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

View File

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