oapi-codegen icon indicating copy to clipboard operation
oapi-codegen copied to clipboard

Enable forcing non-pointer types using Nullable

Open veleek opened this issue 2 years ago • 2 comments

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

veleek avatar Oct 26 '22 15:10 veleek

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:

jamietanna avatar Jan 10 '24 11:01 jamietanna

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"`
}

ghen avatar Jun 19 '24 03:06 ghen