optional nullable field does not get omitempty json tag as mention in Readme
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
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
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 🫣
Looking at https://github.com/oapi-codegen/oapi-codegen/blob/3eff0a25e492a714ea0b2d8508c59fd898881fd0/pkg/codegen/schema.go#L730 this is expected to not be the case (I'll try and work out whether the docs or the behaviour are wrong)