forked from oapi-codegen/oapi-codegen
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
1 parent
887a24f
commit 224d94f
Showing
17 changed files
with
1,147 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package: issue1039 | ||
generate: | ||
client: true | ||
output: client.gen.go |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
54 changes: 54 additions & 0 deletions
54
internal/test/issues/issue-1039/defaultbehaviour/defaultbehaviour_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
49
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.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.