Skip to content

Commit bc97778

Browse files
sonasingh46jamietanna
authored andcommitted
Allow generating nullable.Nullable for nullable properties
As part of #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 #1039. Co-authored-by: Ashutosh Kumar <ashutosh.kumar@elastic.co>
1 parent 887a24f commit bc97778

17 files changed

+1147
-3
lines changed

internal/test/go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ require (
1313
github.com/gorilla/mux v1.8.0
1414
github.com/kataras/iris/v12 v12.2.6-0.20230908161203-24ba4e8933b9
1515
github.com/labstack/echo/v4 v4.11.3
16+
github.com/oapi-codegen/nullable v1.0.1
1617
github.com/oapi-codegen/runtime v1.1.0
1718
github.com/oapi-codegen/testutil v1.0.0
1819
github.com/stretchr/testify v1.8.4

internal/test/go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,8 @@ github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjY
145145
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 h1:RWengNIwukTxcDr9M+97sNutRR1RKhG96O6jWumTTnw=
146146
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826/go.mod h1:TaXosZuwdSHYgviHp1DAtfrULt5eUgsSMsZf+YrPgl8=
147147
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
148+
github.com/oapi-codegen/nullable v1.0.1 h1:/g+R1Kl1qVYhXlVTg+YT4UnHeYqW+cDh9rfzr+pAV/0=
149+
github.com/oapi-codegen/nullable v1.0.1/go.mod h1:KUZ3vUzkmEKY90ksAmit2+5juDIhIZhfDl+0PwOQlFY=
148150
github.com/oapi-codegen/runtime v1.1.0 h1:rJpoNUawn5XTvekgfkvSZr0RqEnoYpFkyvrzfWeFKWM=
149151
github.com/oapi-codegen/runtime v1.1.0/go.mod h1:BeSfBkWWWnAnGdyS+S/GnlbmHKzf8/hwkvelJZDeKA8=
150152
github.com/oapi-codegen/testutil v1.0.0 h1:1GI2IiMMLh2vDHr1OkNacaYU/VaApKdcmfgl4aeXAa8=
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package: issue1039
2+
generate:
3+
client: true
4+
output: client.gen.go

internal/test/issues/issue-1039/client.gen.go

Lines changed: 261 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package defaultbehaviour
2+
3+
import (
4+
"encoding/json"
5+
"testing"
6+
7+
"github.com/stretchr/testify/require"
8+
)
9+
10+
func ptr[T any](v T) *T {
11+
return &v
12+
}
13+
14+
func TestNullableDisabled(t *testing.T) {
15+
// include all fields in patch request
16+
patchReq := PatchRequest{
17+
ComplexRequiredNullable: &ComplexRequiredNullable{
18+
Name: ptr("test-name"),
19+
},
20+
SimpleOptionalNonNullable: ptr(SimpleOptionalNonNullable("bar")),
21+
ComplexOptionalNullable: &ComplexOptionalNullable{
22+
AliasName: ptr("foo-alias"),
23+
Name: ptr("foo"),
24+
},
25+
SimpleOptionalNullable: ptr(SimpleOptionalNullable(10)),
26+
SimpleRequiredNullable: ptr(SimpleRequiredNullable(5)),
27+
}
28+
29+
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}`)
30+
31+
actual, err := json.Marshal(patchReq)
32+
require.NoError(t, err)
33+
require.Equal(t, string(expected), string(actual))
34+
35+
// omit some fields
36+
patchReq = PatchRequest{
37+
ComplexRequiredNullable: &ComplexRequiredNullable{
38+
Name: ptr("test-name"),
39+
},
40+
// SimpleOptionalNonNullable is omitted
41+
ComplexOptionalNullable: &ComplexOptionalNullable{
42+
AliasName: ptr("test-alias-name"),
43+
Name: ptr("test-name"),
44+
},
45+
SimpleOptionalNullable: ptr(SimpleOptionalNullable(10)),
46+
// SimpleRequiredNullable is omitted
47+
}
48+
49+
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}`)
50+
51+
actual, err = json.Marshal(patchReq)
52+
require.NoError(t, err)
53+
require.Equal(t, string(expected), string(actual))
54+
}

internal/test/issues/issue-1039/defaultbehaviour/types.gen.go

Lines changed: 49 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package issue1039
2+
3+
//go:generate go run github.com/deepmap/oapi-codegen/v2/cmd/oapi-codegen --config=types-config.yaml spec.yaml
4+
//go:generate go run github.com/deepmap/oapi-codegen/v2/cmd/oapi-codegen --config=type-config-defaultbehaviour.yaml spec.yaml
5+
//go:generate go run github.com/deepmap/oapi-codegen/v2/cmd/oapi-codegen --config=client-config.yaml spec.yaml
6+
//go:generate go run github.com/deepmap/oapi-codegen/v2/cmd/oapi-codegen --config=server-config.yaml spec.yaml

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy