Skip to content

Commit

Permalink
Merge pull request #284 from vitorquintanilha/fix-channel-detail-as-e…
Browse files Browse the repository at this point in the history
…mpty-array

Fixes issue when serializing ChannelDetails as an empty array
  • Loading branch information
michaelklishin authored Sep 25, 2023
2 parents faf9bdf + 5feab4e commit d10d27c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
11 changes: 11 additions & 0 deletions queues.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,17 @@ type ChannelDetails struct {
User string `json:"user"`
}

// Handles special case where `ChannelDetails` is an empty array
// See https://github.com/rabbitmq/rabbitmq-server/issues/2684
func (c *ChannelDetails) UnmarshalJSON(data []byte) error {
if string(data) == "[]" {
*c = ChannelDetails{}
return nil
}
type Alias ChannelDetails
return json.Unmarshal(data, (*Alias)(c))
}

// QueueDetail describe queue information with a consumer
type QueueDetail struct {
Name string `json:"name"`
Expand Down
18 changes: 18 additions & 0 deletions unit_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package rabbithole

import (
"encoding/json"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
Expand Down Expand Up @@ -74,4 +76,20 @@ var _ = Describe("Unit tests", func() {
Ω(d).Should(Equal(Port(0)))
})
})
Context("ConsumerDetails marshalling", func() {
It("unmarshal ConsumerDetails when channel_details is an empty array", func() {
var d ConsumerDetail
s := []byte("{\"channel_details\":[]}")
err := json.Unmarshal(s, &d)
Ω(err).ShouldNot(HaveOccurred())
Ω(d.ChannelDetails).Should(Equal(ChannelDetails{}))
})
It("unmarshal ConsumerDetails when channel_details is an object", func() {
var d ConsumerDetail
s := []byte("{\"channel_details\":{\"name\":\"foo\"}}")
err := json.Unmarshal(s, &d)
Ω(err).ShouldNot(HaveOccurred())
Ω(d.ChannelDetails).Should(Equal(ChannelDetails{Name: "foo"}))
})
})
})

0 comments on commit d10d27c

Please sign in to comment.