Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .changelog/v0.5.0.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ title = ""
description = ""

[[bugs]]
title = ""
description = ""
title = "Type fields"
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)"

[[enhancements]]
title = ""
Expand Down
12 changes: 2 additions & 10 deletions internal/generate/exceptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,16 @@

package main

// Returns a list of types that should not be omitted when empty
// for json serialisation
func omitemptyExceptions() []string {
return []string{
"[]VpcFirewallRuleUpdate",
"[]NameOrId",
}
}

func emptyTypes() []string {
return []string{
"BgpMessageHistory",
"SwitchLinkState",
}
}

// TODO: Actually handle nullable fields properly
func nullable() []string {
// TODO: This type has a nested required "Type" field, which hinders
// the usage of this type. Remove when this is fixed in the upstream API
return []string{
"InstanceDiskAttachment",
}
Expand Down
6 changes: 3 additions & 3 deletions internal/generate/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -541,12 +541,12 @@ func createTypeObject(schema *openapi3.Schema, name, typeName, description strin

field.Name = strcase.ToCamel(k)
field.Type = typeName

serInfo := fmt.Sprintf("`json:\"%s,omitempty\" yaml:\"%s,omitempty\"`", k, k)
// There are a few types we do not want to omit if empty
// TODO: Keep an eye out to see if there is a way to identify all of
if sliceContains(omitemptyExceptions(), typeName) {
if isNullableArray(v) {
serInfo = fmt.Sprintf("`json:\"%s\" yaml:\"%s\"`", k, k)
}

field.SerializationInfo = serInfo

fields = append(fields, field)
Expand Down
4 changes: 4 additions & 0 deletions internal/generate/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ func isObjectArray(v *openapi3.SchemaRef) bool {
return false
}

func isNullableArray(v *openapi3.SchemaRef) bool {
return v.Value.Type.Is("array") && v.Value.Nullable
}

// formatStringType converts a string schema to a valid Go type.
func formatStringType(t *openapi3.Schema) string {
var format string
Expand Down
14 changes: 7 additions & 7 deletions oxide/types.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.