Skip to content

Commit

Permalink
Allow generating nullable.Nullable for nullable properties
Browse files Browse the repository at this point in the history
As part of oapi-codegen#1039, we've created a new library `oapi-codegen/nullable`,
which allows tracking whether:

- a field is not sent
- a field is sent with an explicit `null`
- a field is sent with an explicit value

This introduces an opt-in `output-options` flag, `nullable-type`, which
can generate the `nullable.Nullable` types.

This is opt-in, as existing code will break due to the signature change,
as well as a behaviour change.

Closes oapi-codegen#1039.

Co-authored-by: Ashutosh Kumar <ashutosh.kumar@elastic.co>
  • Loading branch information
2 people authored and jamietanna committed Jan 11, 2024
1 parent 887a24f commit bc97778
Show file tree
Hide file tree
Showing 17 changed files with 1,147 additions and 3 deletions.
1 change: 1 addition & 0 deletions internal/test/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ require (
github.com/gorilla/mux v1.8.0
github.com/kataras/iris/v12 v12.2.6-0.20230908161203-24ba4e8933b9
github.com/labstack/echo/v4 v4.11.3
github.com/oapi-codegen/nullable v1.0.1
github.com/oapi-codegen/runtime v1.1.0
github.com/oapi-codegen/testutil v1.0.0
github.com/stretchr/testify v1.8.4
Expand Down
2 changes: 2 additions & 0 deletions internal/test/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@ github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjY
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 h1:RWengNIwukTxcDr9M+97sNutRR1RKhG96O6jWumTTnw=
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826/go.mod h1:TaXosZuwdSHYgviHp1DAtfrULt5eUgsSMsZf+YrPgl8=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
github.com/oapi-codegen/nullable v1.0.1 h1:/g+R1Kl1qVYhXlVTg+YT4UnHeYqW+cDh9rfzr+pAV/0=
github.com/oapi-codegen/nullable v1.0.1/go.mod h1:KUZ3vUzkmEKY90ksAmit2+5juDIhIZhfDl+0PwOQlFY=
github.com/oapi-codegen/runtime v1.1.0 h1:rJpoNUawn5XTvekgfkvSZr0RqEnoYpFkyvrzfWeFKWM=
github.com/oapi-codegen/runtime v1.1.0/go.mod h1:BeSfBkWWWnAnGdyS+S/GnlbmHKzf8/hwkvelJZDeKA8=
github.com/oapi-codegen/testutil v1.0.0 h1:1GI2IiMMLh2vDHr1OkNacaYU/VaApKdcmfgl4aeXAa8=
Expand Down
4 changes: 4 additions & 0 deletions internal/test/issues/issue-1039/client-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package: issue1039
generate:
client: true
output: client.gen.go
261 changes: 261 additions & 0 deletions internal/test/issues/issue-1039/client.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package defaultbehaviour

import (
"encoding/json"
"testing"

"github.com/stretchr/testify/require"
)

func ptr[T any](v T) *T {
return &v
}

func TestNullableDisabled(t *testing.T) {
// include all fields in patch request
patchReq := PatchRequest{
ComplexRequiredNullable: &ComplexRequiredNullable{
Name: ptr("test-name"),
},
SimpleOptionalNonNullable: ptr(SimpleOptionalNonNullable("bar")),
ComplexOptionalNullable: &ComplexOptionalNullable{
AliasName: ptr("foo-alias"),
Name: ptr("foo"),
},
SimpleOptionalNullable: ptr(SimpleOptionalNullable(10)),
SimpleRequiredNullable: ptr(SimpleRequiredNullable(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}`)

actual, err := json.Marshal(patchReq)
require.NoError(t, err)
require.Equal(t, string(expected), string(actual))

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

expected = []byte(`{"complex_optional_nullable":{"alias_name":"test-alias-name","name":"test-name"},"complex_required_nullable":{"name":"test-name"},"simple_optional_nullable":10,"simple_required_nullable":null}`)

actual, err = json.Marshal(patchReq)
require.NoError(t, err)
require.Equal(t, string(expected), string(actual))
}
49 changes: 49 additions & 0 deletions internal/test/issues/issue-1039/defaultbehaviour/types.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions internal/test/issues/issue-1039/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package issue1039

//go:generate go run github.com/deepmap/oapi-codegen/v2/cmd/oapi-codegen --config=types-config.yaml spec.yaml
//go:generate go run github.com/deepmap/oapi-codegen/v2/cmd/oapi-codegen --config=type-config-defaultbehaviour.yaml spec.yaml
//go:generate go run github.com/deepmap/oapi-codegen/v2/cmd/oapi-codegen --config=client-config.yaml spec.yaml
//go:generate go run github.com/deepmap/oapi-codegen/v2/cmd/oapi-codegen --config=server-config.yaml spec.yaml
Loading

0 comments on commit bc97778

Please sign in to comment.