Skip to content

optional nullable field does not get omitempty json tag as mention in Readme #2091

Closed

Description

@damnOblivious

Readme states that by default nullable fields get omitempty tag. See https://github.com/oapi-codegen/oapi-codegen?tab=readme-ov-file#generating-nullable-types. But that doesn't seem to be the case.

#[0]$ cat config.yaml2
package: preferskipoptionalpointerwithomitzero
output: gen.go
generate:
  models: true
output-options:
  skip-prune: true
#[0]$ cat spec.yaml2
openapi: "3.0.0"
info:
  version: 1.0.0
  title: testing
components:
  schemas:
    ClientWithExtension:
      type: object
      properties:
        newfield:
          type: string
          nullable: true
#[0]$ oapi-codegen --config config.yaml2 spec.yaml2

#[0]$ cat gen.go
// Package preferskipoptionalpointerwithomitzero provides primitives to interact with the openapi HTTP API.
//
// Code generated by github.com/oapi-codegen/oapi-codegen/v2 version v2.5.0 DO NOT EDIT.
package preferskipoptionalpointerwithomitzero

// ClientWithExtension defines model for ClientWithExtension.
type ClientWithExtension struct {
        Newfield *string `json:"newfield"`
}
#[0]$ oapi-codegen --version
github.com/oapi-codegen/oapi-codegen/v2/cmd/oapi-codegen
v2.5.0

#[0]$ go version
go version go1.24.4 linux/amd64

Activity

  1. timharek commented on Sep 22, 2025

    @timharek

    We have the same issue. Our temporary solution is to use overlay for the time being:

      - target: "$.components.schemas.*.properties..[?(@.nullable)]"
        description: Ensure that nullable fields, results in omitempty
        update:
          x-omitempty: true
  2. jamietanna commented on Sep 23, 2025

    @jamietanna
    Member

    Huh, thanks! Looks like this behaviour hasn't been true at least as far as back as v2.2.0, so could be we documented this incorrectly 🫣

  3. added
    bug
    on Sep 23, 2025
  4. jamietanna commented on Sep 23, 2025

    @jamietanna
    Member

    Looking at

    omitEmpty := !p.Nullable && shouldOmitEmpty
    this is expected to not be the case (I'll try and work out whether the docs or the behaviour are wrong)

  5. mromaszewicz commented on Feb 14, 2026

    @mromaszewicz
    Member

    I looked around - it's a bug. We have several generated files in our tests which fail to add the ,omitempty. The condition on whether a field should be omitted is much more complex than that.

  6. added a commit that references this issue on Feb 14, 2026
    33101e8
  7. mromaszewicz commented on Feb 14, 2026

    @mromaszewicz
    Member

    That code was a bit strange. I think it resulted from some conflict being resolved incorrectly or something, because the condition right above the code I removed was correct.

  8. added a commit that references this issue on Feb 26, 2026
    d567e49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions