Replies: 1 comment
-
Hello, great question! The idea is that having an API where the semantic differs between a field being present but with a That being said that is not always possible (e.g. for backwards compatibility or other external dependencies) so func NullableAttribute(name string, args ...any) {
Attribute(name, args...)
var parent *expr.AttributeExpr
switch def := eval.Current().(type) {
case *expr.AttributeExpr:
parent = def
case expr.CompositeExpr:
parent = def.Attribute()
}
if parent == nil {
return
}
obj, ok := parent.Type.(*expr.Object)
if !ok {
return
}
att := obj.Attribute(name)
if att == nil {
return
}
if att.Meta == nil {
att.Meta = make(map[string][]string)
}
att.Meta["struct:tag:json"] = []string{name}
} |
Beta Was this translation helpful? Give feedback.
-
At the moment, if an attribute is not marked as required and the value is
nil
, it will be omitted from the response JSON.It is semantically valid to return a
null
value in an API and it would be helpful if this could be nominated as a behaviour of the type generator.At the moment a workaround is to specify the
struct:tag:XXXX
meta on the attribute to achieve this, but it's very clunky to do so for every non-required attribute in the DSL. Has this been considered, and why is the default behaviour to addomitempty
to the generated struct tags?Beta Was this translation helpful? Give feedback.
All reactions