Skip to content

Commit a2b4df9

Browse files
Merge branch 'main' into add-missing-fields-to-network-types
2 parents 809b1c9 + 65b1d0f commit a2b4df9

File tree

5 files changed

+97
-31
lines changed

5 files changed

+97
-31
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 & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,31 +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-
// Networking Types
15-
"[]AddressConfig",
16-
"[]BgpPeerConfig",
17-
"[]SwitchInterfaceConfigCreate",
18-
"[]LinkConfigCreate",
19-
"[]RouteConfig",
20-
}
21-
}
22-
237
func emptyTypes() []string {
248
return []string{
259
"BgpMessageHistory",
2610
"SwitchLinkState",
2711
}
2812
}
2913

30-
// TODO: Actually handle nullable fields properly
3114
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
3217
return []string{
3318
"InstanceDiskAttachment",
3419
"TxEqConfig",

internal/generate/types.go

Lines changed: 13 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)
@@ -724,6 +724,7 @@ func createOneOf(s *openapi3.Schema, name, typeName string) ([]TypeTemplate, []E
724724
}
725725

726726
propertyName := strcase.ToCamel(prop)
727+
727728
// Avoids duplication for every enum
728729
if !containsMatchFirstWord(parsedProperties, propertyName) {
729730
field := TypeFields{
@@ -753,6 +754,15 @@ func createOneOf(s *openapi3.Schema, name, typeName string) ([]TypeTemplate, []E
753754

754755
enumFieldName = strcase.ToCamel(p.Value.Enum[0].(string))
755756
}
757+
758+
// Enums can appear in a valid OpenAPI spec as a OneOf without necessarily
759+
// being identified as such. If we find an object with a single property
760+
// nested inside a OneOf we will assume this is an enum and modify the name of
761+
// the struct that will be created out of this object.
762+
// e.g. https://github.com/oxidecomputer/omicron/blob/158c0b205f23772dc6c4c97633fd1769cc0e00d4/openapi/nexus.json#L18637-L18682
763+
if len(keys) == 1 && p.Value.Enum == nil {
764+
enumFieldName = propertyName
765+
}
756766
}
757767

758768
// TODO: This is the only place that has an "additional name" at the end

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: 76 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)