Browse code

api/types/container: introduce CommitResponse type

Move api/types.IDResponse to a "common" package (to prevent cyclic import
issues), and introduce a container.CommitResponse type as alias. This allows
consumers to use ContainerCommit without having to import the "types" package,
and allows us to differentiate the response for container commit separate from
other endpoints currently using IDResponse.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>

Sebastiaan van Stijn authored on 2025/02/11 03:34:25
Showing 10 changed files
... ...
@@ -71,7 +71,7 @@ func (c *containerRouter) postCommit(ctx context.Context, w http.ResponseWriter,
71 71
 		return err
72 72
 	}
73 73
 
74
-	return httputils.WriteJSON(w, http.StatusCreated, &types.IDResponse{ID: imgID})
74
+	return httputils.WriteJSON(w, http.StatusCreated, &container.CommitResponse{ID: imgID})
75 75
 }
76 76
 
77 77
 func (c *containerRouter) getContainersJSON(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
... ...
@@ -2908,9 +2908,10 @@ definitions:
2908 2908
     example:
2909 2909
       message: "Something went wrong."
2910 2910
 
2911
-  IdResponse:
2911
+  IDResponse:
2912 2912
     description: "Response to an API call that returns just an Id"
2913 2913
     type: "object"
2914
+    x-go-name: "IDResponse"
2914 2915
     required: ["Id"]
2915 2916
     properties:
2916 2917
       Id:
... ...
@@ -10220,7 +10221,7 @@ paths:
10220 10220
         201:
10221 10221
           description: "no error"
10222 10222
           schema:
10223
-            $ref: "#/definitions/IdResponse"
10223
+            $ref: "#/definitions/IDResponse"
10224 10224
         404:
10225 10225
           description: "no such container"
10226 10226
           schema:
... ...
@@ -10614,7 +10615,7 @@ paths:
10614 10614
         201:
10615 10615
           description: "no error"
10616 10616
           schema:
10617
-            $ref: "#/definitions/IdResponse"
10617
+            $ref: "#/definitions/IDResponse"
10618 10618
         404:
10619 10619
           description: "no such container"
10620 10620
           schema:
... ...
@@ -13094,7 +13095,7 @@ paths:
13094 13094
         201:
13095 13095
           description: "no error"
13096 13096
           schema:
13097
-            $ref: "#/definitions/IdResponse"
13097
+            $ref: "#/definitions/IDResponse"
13098 13098
         409:
13099 13099
           description: "name conflicts with an existing object"
13100 13100
           schema:
... ...
@@ -13301,7 +13302,7 @@ paths:
13301 13301
         201:
13302 13302
           description: "no error"
13303 13303
           schema:
13304
-            $ref: "#/definitions/IdResponse"
13304
+            $ref: "#/definitions/IDResponse"
13305 13305
         409:
13306 13306
           description: "name conflicts with an existing object"
13307 13307
           schema:
13308 13308
new file mode 100644
... ...
@@ -0,0 +1,13 @@
0
+package common
1
+
2
+// This file was generated by the swagger tool.
3
+// Editing this file might prove futile when you re-run the swagger generate command
4
+
5
+// IDResponse Response to an API call that returns just an Id
6
+// swagger:model IDResponse
7
+type IDResponse struct {
8
+
9
+	// The id of the newly created object.
10
+	// Required: true
11
+	ID string `json:"Id"`
12
+}
0 13
new file mode 100644
... ...
@@ -0,0 +1,7 @@
0
+package container
1
+
2
+import "github.com/docker/docker/api/types/common"
3
+
4
+// CommitResponse response for the commit API call, containing the ID of the
5
+// image that was produced.
6
+type CommitResponse = common.IDResponse
0 7
deleted file mode 100644
... ...
@@ -1,13 +0,0 @@
1
-package types
2
-
3
-// This file was generated by the swagger tool.
4
-// Editing this file might prove futile when you re-run the swagger generate command
5
-
6
-// IDResponse Response to an API call that returns just an Id
7
-// swagger:model IdResponse
8
-type IDResponse struct {
9
-
10
-	// The id of the newly created object.
11
-	// Required: true
12
-	ID string `json:"Id"`
13
-}
... ...
@@ -3,11 +3,15 @@ package types
3 3
 import (
4 4
 	"context"
5 5
 
6
+	"github.com/docker/docker/api/types/common"
6 7
 	"github.com/docker/docker/api/types/container"
7 8
 	"github.com/docker/docker/api/types/image"
8 9
 	"github.com/docker/docker/api/types/storage"
9 10
 )
10 11
 
12
+// IDResponse Response to an API call that returns just an Id
13
+type IDResponse = common.IDResponse
14
+
11 15
 // ContainerJSONBase contains response of Engine API GET "/containers/{name:.*}/json"
12 16
 // for API version 1.18 and older.
13 17
 //
... ...
@@ -69,7 +69,7 @@ type HijackDialer interface {
69 69
 // ContainerAPIClient defines API client methods for the containers
70 70
 type ContainerAPIClient interface {
71 71
 	ContainerAttach(ctx context.Context, container string, options container.AttachOptions) (types.HijackedResponse, error)
72
-	ContainerCommit(ctx context.Context, container string, options container.CommitOptions) (types.IDResponse, error)
72
+	ContainerCommit(ctx context.Context, container string, options container.CommitOptions) (container.CommitResponse, error)
73 73
 	ContainerCreate(ctx context.Context, config *container.Config, hostConfig *container.HostConfig, networkingConfig *network.NetworkingConfig, platform *ocispec.Platform, containerName string) (container.CreateResponse, error)
74 74
 	ContainerDiff(ctx context.Context, container string) ([]container.FilesystemChange, error)
75 75
 	ContainerExecAttach(ctx context.Context, execID string, options container.ExecAttachOptions) (types.HijackedResponse, error)
... ...
@@ -7,26 +7,25 @@ import (
7 7
 	"net/url"
8 8
 
9 9
 	"github.com/distribution/reference"
10
-	"github.com/docker/docker/api/types"
11 10
 	"github.com/docker/docker/api/types/container"
12 11
 )
13 12
 
14 13
 // ContainerCommit applies changes to a container and creates a new tagged image.
15
-func (cli *Client) ContainerCommit(ctx context.Context, containerID string, options container.CommitOptions) (types.IDResponse, error) {
14
+func (cli *Client) ContainerCommit(ctx context.Context, containerID string, options container.CommitOptions) (container.CommitResponse, error) {
16 15
 	containerID, err := trimID("container", containerID)
17 16
 	if err != nil {
18
-		return types.IDResponse{}, err
17
+		return container.CommitResponse{}, err
19 18
 	}
20 19
 
21 20
 	var repository, tag string
22 21
 	if options.Reference != "" {
23 22
 		ref, err := reference.ParseNormalizedNamed(options.Reference)
24 23
 		if err != nil {
25
-			return types.IDResponse{}, err
24
+			return container.CommitResponse{}, err
26 25
 		}
27 26
 
28 27
 		if _, isCanonical := ref.(reference.Canonical); isCanonical {
29
-			return types.IDResponse{}, errors.New("refusing to create a tag with a digest reference")
28
+			return container.CommitResponse{}, errors.New("refusing to create a tag with a digest reference")
30 29
 		}
31 30
 		ref = reference.TagNameOnly(ref)
32 31
 
... ...
@@ -49,7 +48,7 @@ func (cli *Client) ContainerCommit(ctx context.Context, containerID string, opti
49 49
 		query.Set("pause", "0")
50 50
 	}
51 51
 
52
-	var response types.IDResponse
52
+	var response container.CommitResponse
53 53
 	resp, err := cli.post(ctx, "/commit", query, options.Config, nil)
54 54
 	defer ensureReaderClosed(resp)
55 55
 	if err != nil {
... ...
@@ -10,7 +10,6 @@ import (
10 10
 	"strings"
11 11
 	"testing"
12 12
 
13
-	"github.com/docker/docker/api/types"
14 13
 	"github.com/docker/docker/api/types/container"
15 14
 	"github.com/docker/docker/errdefs"
16 15
 	"gotest.tools/v3/assert"
... ...
@@ -77,7 +76,7 @@ func TestContainerCommit(t *testing.T) {
77 77
 			if len(changes) != len(expectedChanges) {
78 78
 				return nil, fmt.Errorf("expected container changes size to be '%d', got %d", len(expectedChanges), len(changes))
79 79
 			}
80
-			b, err := json.Marshal(types.IDResponse{
80
+			b, err := json.Marshal(container.CommitResponse{
81 81
 				ID: "new_container_id",
82 82
 			})
83 83
 			if err != nil {
... ...
@@ -4,7 +4,6 @@ set -eu
4 4
 swagger generate model -f api/swagger.yaml \
5 5
 	-t api -m types --skip-validator -C api/swagger-gen.yaml \
6 6
 	-n ErrorResponse \
7
-	-n IdResponse \
8 7
 	-n Plugin \
9 8
 	-n PluginDevice \
10 9
 	-n PluginMount \
... ...
@@ -12,6 +11,10 @@ swagger generate model -f api/swagger.yaml \
12 12
 	-n PluginInterfaceType
13 13
 
14 14
 swagger generate model -f api/swagger.yaml \
15
+	-t api -m types/common --skip-validator -C api/swagger-gen.yaml \
16
+	-n IDResponse
17
+
18
+swagger generate model -f api/swagger.yaml \
15 19
 	-t api -m types/storage --skip-validator -C api/swagger-gen.yaml \
16 20
 	-n DriverData
17 21