From 725f34151c5f86b0b50f6e5601223e6da194af1b Mon Sep 17 00:00:00 2001 From: Antonio Murdaca Date: Wed, 3 Jun 2015 18:23:14 +0200 Subject: [PATCH] Do not omit empty json field in /containers/json api response Signed-off-by: Antonio Murdaca --- api/types/types.go | 20 +++++------ integration-cli/docker_api_containers_test.go | 35 +++++++++++++++++++ 2 files changed, 45 insertions(+), 10 deletions(-) diff --git a/api/types/types.go b/api/types/types.go index dd550050c8..d33e38d584 100644 --- a/api/types/types.go +++ b/api/types/types.go @@ -101,16 +101,16 @@ type Port struct { } type Container struct { - ID string `json:"Id"` - Names []string `json:",omitempty"` - Image string `json:",omitempty"` - Command string `json:",omitempty"` - Created int `json:",omitempty"` - Ports []Port `json:",omitempty"` - SizeRw int `json:",omitempty"` - SizeRootFs int `json:",omitempty"` - Labels map[string]string `json:",omitempty"` - Status string `json:",omitempty"` + ID string `json:"Id"` + Names []string + Image string + Command string + Created int + Ports []Port + SizeRw int + SizeRootFs int + Labels map[string]string + Status string } // POST "/containers/"+containerID+"/copy" diff --git a/integration-cli/docker_api_containers_test.go b/integration-cli/docker_api_containers_test.go index d7bf9e97f9..d8fa1d71da 100644 --- a/integration-cli/docker_api_containers_test.go +++ b/integration-cli/docker_api_containers_test.go @@ -51,6 +51,41 @@ func (s *DockerSuite) TestContainerApiGetAll(c *check.C) { } } +// regression test for empty json field being omitted #13691 +func (s *DockerSuite) TestContainerApiGetJSONNoFieldsOmitted(c *check.C) { + runCmd := exec.Command(dockerBinary, "run", "busybox", "true") + _, err := runCommand(runCmd) + c.Assert(err, check.IsNil) + + status, body, err := sockRequest("GET", "/containers/json?all=1", nil) + c.Assert(status, check.Equals, http.StatusOK) + c.Assert(err, check.IsNil) + + // empty Labels field triggered this bug, make sense to check for everything + // cause even Ports for instance can trigger this bug + // better safe than sorry.. + fields := []string{ + "Id", + "Names", + "Image", + "Command", + "Created", + "Ports", + "SizeRw", + "SizeRootFs", + "Labels", + "Status", + } + + // decoding into types.Container do not work since it eventually unmarshal + // and empty field to an empty go map, so we just check for a string + for _, f := range fields { + if !strings.Contains(string(body), f) { + c.Fatalf("Field %s is missing and it shouldn't", f) + } + } +} + func (s *DockerSuite) TestContainerApiGetExport(c *check.C) { name := "exportcontainer" runCmd := exec.Command(dockerBinary, "run", "--name", name, "busybox", "touch", "/test")