Skip to content

Enable forcing non-pointer types using Nullable#823

Closed
veleek wants to merge 1 commit intooapi-codegen:mainfrom
veleek:patch-2
Closed

Enable forcing non-pointer types using Nullable#823
veleek wants to merge 1 commit intooapi-codegen:mainfrom
veleek:patch-2

Conversation

@veleek
Copy link
Copy Markdown
Contributor

@veleek veleek commented Oct 26, 2022

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

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.

See also oapi-codegen#479
@jamietanna
Copy link
Copy Markdown
Member

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 🤞

@ghen
Copy link
Copy Markdown

ghen commented Jun 19, 2024

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

@jamietanna jamietanna added this to the v2.5.0 milestone May 7, 2025
@jamietanna jamietanna modified the milestones: v2.5.0, v2.6.0 Jul 15, 2025
@mromaszewicz
Copy link
Copy Markdown
Member

Thank you for contributing, and I'm very sorry for taking so long to get to this PR. At this point, the code has changed so much that it's no longer relevant because the nullable-type configuration option now exists. When enabled, nullable fields use nullable.Nullable[type] wrapper instead of pointer types. This PR's intent is fully implemented.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants