oapi-codegen
oapi-codegen copied to clipboard
Enable forcing non-pointer types using Nullable
Proposed update to GoTypeDef to enable forcing a non pointer type. This is useful in go to avoid using *string
instead of string
in most cases where the value is not required, but there's no need to differentiate between nil
and empty string values.
There may need to be some more semantics around this in order to force the pointer in some cases where p.Nullable
is true. We probably need some discussion around this prior to merging.
See also #479
Thanks @veleek for this! This may no longer be necessary, especially as with #1404, we'll be introducing a separate type that can be opted-in for better Nullable support :crossed_fingers:
May we consider adding this as a default behaviour? At least have an option in generator output configuration to enable this as a default behaviour.
We have many YAML spec files, and annotating 1000s of properties with x-go-type-skip-optional-pointer
is a no go.
Technically, the generator should respect a built-in nullable
spec that it ignores at the moment, even when it is set explicitly.
In other word for the sollowing schema:
schema:
type: object
properties:
nonNullable:
type: string
nonNullableExplicit:
type: string
nullable: false
nullable:
type: string
nullable: true
The expected struct:
type JSONBody struct {
nonNullable string `json:"nonNullable,omitempty"`
nonNullableExplicit string `json:"nonNullableExplicit,omitempty"`
nullable *string `json:"nullable,omitempty"`
}
Instead it produces pointers everywhere:
type JSONBody struct {
nonNullable *string `json:"nonNullable,omitempty"`
nonNullableExplicit *string `json:"nonNullableExplicit,omitempty"`
nullable *string `json:"nullable,omitempty"`
}