Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2327,7 +2327,7 @@ However, you lose the ability to understand the three cases, as there's no way t
- is this field `null`? (Can be checked with `S.Field == nil`)
- does this field have a value? (`S.Field != nil && *S.Field == "123"`)

As of `oapi-codegen` [v2.1.0](https://github.com/oapi-codegen/oapi-codegen/releases/tag/v2.1.0) it is now possible to represent this with the `nullable.Nullable` type from [our new library, oapi-codegen/nullable](https://github.com/oapi-codegen/nullable).
As of `oapi-codegen` [v2.1.0](https://github.com/oapi-codegen/oapi-codegen/releases/tag/v2.1.0) it is now possible to represent this with the `nullable.Value` type from [our library, oapi-codegen/nullable](https://github.com/oapi-codegen/nullable). (previously `nullable.Nullable`, now deprecated);

If you configure your generator's Output Options to opt-in to this behaviour, as so:

Expand All @@ -2341,7 +2341,7 @@ You will now receive the following output:
```go
type S struct {
// note that there's no pointer here, just `omitempty`
Field nullable.Nullable[string] `json:"field,omitempty"`
Field nullable.Value[string] `json:"field,omitempty"`
}
```

Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ require (
github.com/josharian/intern v1.0.0 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 // indirect
github.com/oapi-codegen/nullable v1.1.0 // indirect
github.com/oasdiff/yaml v0.0.0-20250309154309-f31be36b4037 // indirect
github.com/oasdiff/yaml3 v0.0.0-20250309153720-d2182401db90 // indirect
github.com/perimeterx/marshmallow v1.1.5 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826/go.mod h1:TaXosZuwd
github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A=
github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE=
github.com/nxadm/tail v1.4.8/go.mod h1:+ncqLTQzXmGhMZNUePPaPqPvBxHAIsmXswZKocGu+AU=
github.com/oapi-codegen/nullable v1.1.0 h1:eAh8JVc5430VtYVnq00Hrbpag9PFRGWLjxR1/3KntMs=
github.com/oapi-codegen/nullable v1.1.0/go.mod h1:KUZ3vUzkmEKY90ksAmit2+5juDIhIZhfDl+0PwOQlFY=
github.com/oasdiff/yaml v0.0.0-20250309154309-f31be36b4037 h1:G7ERwszslrBzRxj//JalHPu/3yz+De2J+4aLtSRlHiY=
github.com/oasdiff/yaml v0.0.0-20250309154309-f31be36b4037/go.mod h1:2bpvgLBZEtENV5scfDFEtB/5+1M4hkQhDQrccEJ/qGw=
github.com/oasdiff/yaml3 v0.0.0-20250309153720-d2182401db90 h1:bQx3WeLcUWy+RletIKwUIt4x3t8n2SxavmoclizMb8c=
Expand Down
18 changes: 9 additions & 9 deletions internal/test/issues/issue-1039/issue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@ func ptr[T any](v T) *T {
func TestNullableTypesMarshal(t *testing.T) {
// include all fields in patch request
patchReq := PatchRequest{
ComplexRequiredNullable: nullable.NewNullableWithValue(ComplexRequiredNullable{
ComplexRequiredNullable: nullable.NewValue(ComplexRequiredNullable{
Name: ptr("test-name"),
}),
SimpleOptionalNonNullable: ptr(SimpleOptionalNonNullable("bar")),
ComplexOptionalNullable: nullable.NewNullableWithValue(ComplexOptionalNullable{
AliasName: nullable.NewNullableWithValue("foo-alias"),
ComplexOptionalNullable: nullable.NewValue(ComplexOptionalNullable{
AliasName: nullable.NewValue("foo-alias"),
Name: ptr("foo"),
}),
SimpleOptionalNullable: nullable.NewNullableWithValue(10),
SimpleRequiredNullable: nullable.NewNullableWithValue(5),
SimpleOptionalNullable: nullable.NewValue(10),
SimpleRequiredNullable: nullable.NewValue(5),
}

expected := []byte(`{"complex_optional_nullable":{"alias_name":"foo-alias","name":"foo"},"complex_required_nullable":{"name":"test-name"},"simple_optional_non_nullable":"bar","simple_optional_nullable":10,"simple_required_nullable":5}`)
Expand All @@ -37,15 +37,15 @@ func TestNullableTypesMarshal(t *testing.T) {

// omit some fields
patchReq = PatchRequest{
ComplexRequiredNullable: nullable.NewNullableWithValue(ComplexRequiredNullable{
ComplexRequiredNullable: nullable.NewValue(ComplexRequiredNullable{
Name: ptr("test-name"),
}),
// SimpleOptionalNonNullable is omitted
ComplexOptionalNullable: nullable.NewNullableWithValue(ComplexOptionalNullable{
AliasName: nullable.NewNullableWithValue("test-alias-name"),
ComplexOptionalNullable: nullable.NewValue(ComplexOptionalNullable{
AliasName: nullable.NewValue("test-alias-name"),
Name: ptr("test-name"),
}),
SimpleOptionalNullable: nullable.NewNullableWithValue(10),
SimpleOptionalNullable: nullable.NewValue(10),
// SimpleRequiredNullable is omitted
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/codegen/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func (p Property) GoFieldName() string {
func (p Property) GoTypeDef() string {
typeDef := p.Schema.TypeDecl()
if globalState.options.OutputOptions.NullableType && p.Nullable {
return "nullable.Nullable[" + typeDef + "]"
return "nullable.Value[" + typeDef + "]"
}
if !p.Schema.SkipOptionalPointer &&
(!p.Required || p.Nullable ||
Expand Down
8 changes: 4 additions & 4 deletions pkg/codegen/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ func TestProperty_GoTypeDef_nullable(t *testing.T) {
Required: true,
Nullable: true,
},
want: "nullable.Nullable[int]",
want: "nullable.Value[int]",
},

{
Expand Down Expand Up @@ -336,7 +336,7 @@ func TestProperty_GoTypeDef_nullable(t *testing.T) {
Required: false,
Nullable: true,
},
want: "nullable.Nullable[int]",
want: "nullable.Value[int]",
},

{
Expand Down Expand Up @@ -422,7 +422,7 @@ func TestProperty_GoTypeDef_nullable(t *testing.T) {
WriteOnly: true,
Nullable: true,
},
want: "nullable.Nullable[int]",
want: "nullable.Value[int]",
},

{
Expand All @@ -436,7 +436,7 @@ func TestProperty_GoTypeDef_nullable(t *testing.T) {
WriteOnly: true,
Nullable: true,
},
want: "nullable.Nullable[int]",
want: "nullable.Value[int]",
},
}
for _, tt := range tests {
Expand Down