Repository navigation
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}
} |
Uh oh!
There was an error while loading. Please reload this page.
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
nullvalue 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:XXXXmeta 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 addomitemptyto the generated struct tags?All reactions