Skip to content

Commit 6e0b217

Browse files
committed
generate: set omitzero on specific types
Set `omitzero` on the following types to allow clients to set an empty slice and have it serialized to `[]`, which is required by Oxide APIs to unset a value. * `[]VpcFirewallRuleUpdate` * `[]NameOrId` The previous behavior would omit an empty slice entirely during serialization, causing the Oxide APIs to respond with an error.
1 parent 2e4943b commit 6e0b217

File tree

5 files changed

+20
-7
lines changed

5 files changed

+20
-7
lines changed

.changelog/v0.5.0.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[[breaking]]
2-
title = ""
3-
description = ""
2+
title = "Set `omitzero` on specific types"
3+
description = "Set `omitzero` on specific types so that clients can pass an empty slice and have it serialized as `[]`. Requires Go 1.24 or later. [#289](https://github.com/oxidecomputer/oxide.go/pull/289)"
44

55
[[features]]
66
title = ""
@@ -12,4 +12,4 @@ description = "All arrays that are nullable in the API no longer have `omitempty
1212

1313
[[enhancements]]
1414
title = ""
15-
description = ""
15+
description = ""

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module github.com/oxidecomputer/oxide.go
22

3-
go 1.22.5
3+
go 1.24.3
44

55
require (
66
github.com/getkin/kin-openapi v0.132.0

internal/generate/exceptions.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,15 @@
44

55
package main
66

7+
// omitzeroTypes returns a slice of types that should be tagged with omitzero
8+
// for serialization and deserialization.
9+
func omitzeroTypes() []string {
10+
return []string{
11+
"[]VpcFirewallRuleUpdate",
12+
"[]NameOrId",
13+
}
14+
}
15+
716
func emptyTypes() []string {
817
return []string{
918
"BgpMessageHistory",

internal/generate/types.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,9 +542,13 @@ func createTypeObject(schema *openapi3.Schema, name, typeName, description strin
542542
field.Name = strcase.ToCamel(k)
543543
field.Type = typeName
544544

545+
// TODO: Set omitzero on all types.
546+
// https://github.com/oxidecomputer/oxide.go/issues/290
545547
serInfo := fmt.Sprintf("`json:\"%s,omitempty\" yaml:\"%s,omitempty\"`", k, k)
546548
if isNullableArray(v) {
547549
serInfo = fmt.Sprintf("`json:\"%s\" yaml:\"%s\"`", k, k)
550+
} else if sliceContains(omitzeroTypes(), typeName) {
551+
serInfo = fmt.Sprintf("`json:\"%s,omitzero\" yaml:\"%s,omitzero\"`", k, k)
548552
}
549553

550554
field.SerializationInfo = serInfo

oxide/types.go

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)