Skip to content

Commit 719d3ae

Browse files
authored
Handle nullable arrays (#283)
Fixes: #282
1 parent a79eb2c commit 719d3ae

File tree

5 files changed

+18
-22
lines changed

5 files changed

+18
-22
lines changed

.changelog/v0.5.0.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ title = ""
77
description = ""
88

99
[[bugs]]
10-
title = ""
11-
description = ""
10+
title = "Type fields"
11+
description = "All arrays that are nullable in the API no longer have `omitempty` to avoid panics if unset. [#283](https://github.com/oxidecomputer/oxide.go/pull/283)"
1212

1313
[[enhancements]]
1414
title = ""

internal/generate/exceptions.go

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,16 @@
44

55
package main
66

7-
// Returns a list of types that should not be omitted when empty
8-
// for json serialisation
9-
func omitemptyExceptions() []string {
10-
return []string{
11-
"[]VpcFirewallRuleUpdate",
12-
"[]NameOrId",
13-
}
14-
}
15-
167
func emptyTypes() []string {
178
return []string{
189
"BgpMessageHistory",
1910
"SwitchLinkState",
2011
}
2112
}
2213

23-
// TODO: Actually handle nullable fields properly
2414
func nullable() []string {
15+
// TODO: This type has a nested required "Type" field, which hinders
16+
// the usage of this type. Remove when this is fixed in the upstream API
2517
return []string{
2618
"InstanceDiskAttachment",
2719
}

internal/generate/types.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -541,12 +541,12 @@ func createTypeObject(schema *openapi3.Schema, name, typeName, description strin
541541

542542
field.Name = strcase.ToCamel(k)
543543
field.Type = typeName
544+
544545
serInfo := fmt.Sprintf("`json:\"%s,omitempty\" yaml:\"%s,omitempty\"`", k, k)
545-
// There are a few types we do not want to omit if empty
546-
// TODO: Keep an eye out to see if there is a way to identify all of
547-
if sliceContains(omitemptyExceptions(), typeName) {
546+
if isNullableArray(v) {
548547
serInfo = fmt.Sprintf("`json:\"%s\" yaml:\"%s\"`", k, k)
549548
}
549+
550550
field.SerializationInfo = serInfo
551551

552552
fields = append(fields, field)

internal/generate/utils.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ func isObjectArray(v *openapi3.SchemaRef) bool {
5959
return false
6060
}
6161

62+
func isNullableArray(v *openapi3.SchemaRef) bool {
63+
return v.Value.Type.Is("array") && v.Value.Nullable
64+
}
65+
6266
// formatStringType converts a string schema to a valid Go type.
6367
func formatStringType(t *openapi3.Schema) string {
6468
var format string

oxide/types.go

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

0 commit comments

Comments
 (0)