Skip to content

Commit 0f187ce

Browse files
authored
fix: LoadBalancerUpdateServiceOpts not converted correctly (#394)
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 and adds tests to account for this behavior. --------- Co-authored-by: pauhull <[email protected]>
1 parent 7511685 commit 0f187ce

File tree

3 files changed

+39
-29
lines changed

3 files changed

+39
-29
lines changed

hcloud/schema_gen.go

+21
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ You can find a documentation of goverter here: https://goverter.jmattheis.de/
7878
// goverter:extend schemaFromLoadBalancerCreateOptsTargetServer
7979
// goverter:extend schemaFromLoadBalancerCreateOptsTargetIP
8080
// goverter:extend stringMapToStringMapPtr
81+
// goverter:extend int64SlicePtrFromCertificatePtrSlice
82+
// goverter:extend stringSlicePtrFromStringSlice
8183
type converter interface {
8284

8385
// goverter:map Error.Code ErrorCode
@@ -926,3 +928,22 @@ func mapZeroFloat32ToNil(f float32) *float32 {
926928
func isDeprecationNotNil(d *DeprecationInfo) bool {
927929
return d != nil
928930
}
931+
932+
// int64SlicePtrFromCertificatePtrSlice is needed so that a nil slice is mapped to nil instead of &nil.
933+
func int64SlicePtrFromCertificatePtrSlice(s []*Certificate) *[]int64 {
934+
if s == nil {
935+
return nil
936+
}
937+
var ids = make([]int64, len(s))
938+
for i, cert := range s {
939+
ids[i] = cert.ID
940+
}
941+
return &ids
942+
}
943+
944+
func stringSlicePtrFromStringSlice(s []string) *[]string {
945+
if s == nil {
946+
return nil
947+
}
948+
return &s
949+
}

hcloud/schema_test.go

+12-2
Original file line numberDiff line numberDiff line change
@@ -1797,8 +1797,18 @@ func TestLoadBalancerUpdateServiceOptsToSchema(t *testing.T) {
17971797
Request schema.LoadBalancerActionUpdateServiceRequest
17981798
}{
17991799
"empty": {
1800-
Opts: LoadBalancerUpdateServiceOpts{},
1801-
Request: schema.LoadBalancerActionUpdateServiceRequest{},
1800+
Opts: LoadBalancerUpdateServiceOpts{
1801+
HTTP: &LoadBalancerUpdateServiceOptsHTTP{},
1802+
HealthCheck: &LoadBalancerUpdateServiceOptsHealthCheck{
1803+
HTTP: &LoadBalancerUpdateServiceOptsHealthCheckHTTP{},
1804+
},
1805+
},
1806+
Request: schema.LoadBalancerActionUpdateServiceRequest{
1807+
HTTP: &schema.LoadBalancerActionUpdateServiceRequestHTTP{},
1808+
HealthCheck: &schema.LoadBalancerActionUpdateServiceRequestHealthCheck{
1809+
HTTP: &schema.LoadBalancerActionUpdateServiceRequestHealthCheckHTTP{},
1810+
},
1811+
},
18021812
},
18031813
"all set": {
18041814
Opts: LoadBalancerUpdateServiceOpts{

hcloud/zz_schema.go

+6-27
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)