Skip to content

Commit ecfa72d

Browse files
authored
generate: set omitzero on specific types (#289)
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 855352f commit ecfa72d

File tree

4 files changed

+20
-4
lines changed

4 files changed

+20
-4
lines changed

.changelog/v0.5.0.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
[[breaking]]
22
title = "Update to Go 1.24"
33
description = "Updated the SDK's Go version to Go 1.24. Consumers of this SDK will need to update to Go 1.24 as well. [#291](https://github.com/oxidecomputer/oxide.go/pull/291)"
4+
title = "Set `omitzero` on specific types"
5+
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)"
46

57
[[features]]
68
title = ""

internal/generate/exceptions.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,16 @@
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+
"DerEncodedKeyPair",
14+
}
15+
}
16+
717
func emptyTypes() []string {
818
return []string{
919
"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: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)