Skip to content

Commit

Permalink
fix: LoadBalancerUpdateServiceOpts not converted correctly
Browse files Browse the repository at this point in the history
In the conversion from LoadBalancerUpdateServiceOpts to schema.LoadBalancerActionUpdateServiceRequest, there are conversions from slices to slice pointers. Slice pointers exist in schemas to differentiate between absent and empty.

Goverter converts nil slices to &nil. This leads to the field being marshaled to `null` in JSON (even with the `omitempty` flag) instead of being not present (See hetznercloud/cli#702)

This PR fixes this issue by adding manual conversion methods
  • Loading branch information
phm07 committed Mar 5, 2024
1 parent 7511685 commit ac8c740
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 29 deletions.
21 changes: 21 additions & 0 deletions hcloud/schema_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ You can find a documentation of goverter here: https://goverter.jmattheis.de/
// goverter:extend schemaFromLoadBalancerCreateOptsTargetServer
// goverter:extend schemaFromLoadBalancerCreateOptsTargetIP
// goverter:extend stringMapToStringMapPtr
// goverter:extend int64SlicePtrFromCertificatePtrSlice
// goverter:extend stringSlicePtrFromStringSlice
type converter interface {

// goverter:map Error.Code ErrorCode
Expand Down Expand Up @@ -926,3 +928,22 @@ func mapZeroFloat32ToNil(f float32) *float32 {
func isDeprecationNotNil(d *DeprecationInfo) bool {
return d != nil
}

// this is needed so that a nil slice is mapped to nil instead of &nil

Check failure on line 932 in hcloud/schema_gen.go

View workflow job for this annotation

GitHub Actions / lint

Comment should end in a period (godot)
func int64SlicePtrFromCertificatePtrSlice(s []*Certificate) *[]int64 {
if s == nil {
return nil
}
var ids = make([]int64, len(s))
for i, cert := range s {
ids[i] = cert.ID
}
return &ids
}

func stringSlicePtrFromStringSlice(s []string) *[]string {
if s == nil {
return nil
}
return &s
}
14 changes: 12 additions & 2 deletions hcloud/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1797,8 +1797,18 @@ func TestLoadBalancerUpdateServiceOptsToSchema(t *testing.T) {
Request schema.LoadBalancerActionUpdateServiceRequest
}{
"empty": {
Opts: LoadBalancerUpdateServiceOpts{},
Request: schema.LoadBalancerActionUpdateServiceRequest{},
Opts: LoadBalancerUpdateServiceOpts{
HTTP: &LoadBalancerUpdateServiceOptsHTTP{},
HealthCheck: &LoadBalancerUpdateServiceOptsHealthCheck{
HTTP: &LoadBalancerUpdateServiceOptsHealthCheckHTTP{},
},
},
Request: schema.LoadBalancerActionUpdateServiceRequest{
HTTP: &schema.LoadBalancerActionUpdateServiceRequestHTTP{},
HealthCheck: &schema.LoadBalancerActionUpdateServiceRequestHealthCheck{
HTTP: &schema.LoadBalancerActionUpdateServiceRequestHealthCheckHTTP{},
},
},
},
"all set": {
Opts: LoadBalancerUpdateServiceOpts{
Expand Down
33 changes: 6 additions & 27 deletions hcloud/zz_schema.go

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

0 comments on commit ac8c740

Please sign in to comment.