Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions cli/compose/loader/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -717,6 +717,9 @@ var transformServicePort TransformerFunc = func(data any) (any, error) {
}
ports = append(ports, v...)
case map[string]any:
if err := normalizeLongFormPort(value); err != nil {
return data, err
}
ports = append(ports, value)
default:
return data, fmt.Errorf("invalid type %T for port", value)
Expand All @@ -728,6 +731,27 @@ var transformServicePort TransformerFunc = func(data any) (any, error) {
}
}

// normalizeLongFormPort accepts quoted integers for target and published.
// compose convert emits those as strings, and the schema used to reject them.
func normalizeLongFormPort(port map[string]any) error {
for _, field := range []string{"target", "published"} {
v, ok := port[field]
if !ok || v == nil {
continue
}
s, ok := v.(string)
if !ok {
continue
}
u, err := strconv.ParseUint(s, 10, 16)
if err != nil {
return fmt.Errorf("invalid %s port %q", field, s)
}
port[field] = int(u)
Comment thread
github-advanced-security[bot] marked this conversation as resolved.
Fixed
}
return nil
}

var transformStringSourceMap TransformerFunc = func(data any) (any, error) {
switch value := data.(type) {
case string:
Expand Down
29 changes: 29 additions & 0 deletions cli/compose/loader/loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,35 @@ volumes:
assert.Check(t, is.Equal(home, config.Volumes["test"].Driver))
}

func TestLoadLongFormStringPorts(t *testing.T) {
config, err := loadYAML(`
version: "3.8"
services:
web:
image: nginx:alpine
ports:
- published: "8080"
target: "80"
protocol: tcp
`)
assert.NilError(t, err)
assert.Assert(t, len(config.Services) == 1)
assert.Check(t, is.DeepEqual(config.Services[0].Ports, []types.ServicePortConfig{
{Target: 80, Published: 8080, Protocol: "tcp"},
}))

_, err = loadYAML(`
version: "3.4"
services:
web:
image: nginx:alpine
ports:
- published: "abc"
target: 80
`)
assert.ErrorContains(t, err, `invalid published port "abc"`)
}

func TestLoadWithInterpolationCastFull(t *testing.T) {
dict, err := ParseYAML([]byte(`
version: "3.8"
Expand Down
4 changes: 2 additions & 2 deletions cli/compose/schema/data/config_schema_v3.10.json
Original file line number Diff line number Diff line change
Expand Up @@ -222,8 +222,8 @@
"type": "object",
"properties": {
"mode": {"type": "string"},
"target": {"type": "integer"},
"published": {"type": "integer"},
"target": {"type": ["integer", "string"]},
"published": {"type": ["integer", "string"]},
"protocol": {"type": "string"}
},
"additionalProperties": false
Expand Down
4 changes: 2 additions & 2 deletions cli/compose/schema/data/config_schema_v3.11.json
Original file line number Diff line number Diff line change
Expand Up @@ -222,8 +222,8 @@
"type": "object",
"properties": {
"mode": {"type": "string"},
"target": {"type": "integer"},
"published": {"type": "integer"},
"target": {"type": ["integer", "string"]},
"published": {"type": ["integer", "string"]},
"protocol": {"type": "string"}
},
"additionalProperties": false
Expand Down
4 changes: 2 additions & 2 deletions cli/compose/schema/data/config_schema_v3.12.json
Original file line number Diff line number Diff line change
Expand Up @@ -222,8 +222,8 @@
"type": "object",
"properties": {
"mode": {"type": "string"},
"target": {"type": "integer"},
"published": {"type": "integer"},
"target": {"type": ["integer", "string"]},
"published": {"type": ["integer", "string"]},
"protocol": {"type": "string"}
},
"additionalProperties": false
Expand Down
4 changes: 2 additions & 2 deletions cli/compose/schema/data/config_schema_v3.13.json
Original file line number Diff line number Diff line change
Expand Up @@ -228,8 +228,8 @@
"type": "object",
"properties": {
"mode": {"type": "string"},
"target": {"type": "integer"},
"published": {"type": "integer"},
"target": {"type": ["integer", "string"]},
"published": {"type": ["integer", "string"]},
"protocol": {"type": "string"}
},
"additionalProperties": false
Expand Down
4 changes: 2 additions & 2 deletions cli/compose/schema/data/config_schema_v3.2.json
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,8 @@
"type": "object",
"properties": {
"mode": {"type": "string"},
"target": {"type": "integer"},
"published": {"type": "integer"},
"target": {"type": ["integer", "string"]},
"published": {"type": ["integer", "string"]},
"protocol": {"type": "string"}
},
"additionalProperties": false
Expand Down
4 changes: 2 additions & 2 deletions cli/compose/schema/data/config_schema_v3.3.json
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,8 @@
"type": "object",
"properties": {
"mode": {"type": "string"},
"target": {"type": "integer"},
"published": {"type": "integer"},
"target": {"type": ["integer", "string"]},
"published": {"type": ["integer", "string"]},
"protocol": {"type": "string"}
},
"additionalProperties": false
Expand Down
4 changes: 2 additions & 2 deletions cli/compose/schema/data/config_schema_v3.4.json
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,8 @@
"type": "object",
"properties": {
"mode": {"type": "string"},
"target": {"type": "integer"},
"published": {"type": "integer"},
"target": {"type": ["integer", "string"]},
"published": {"type": ["integer", "string"]},
"protocol": {"type": "string"}
},
"additionalProperties": false
Expand Down
4 changes: 2 additions & 2 deletions cli/compose/schema/data/config_schema_v3.5.json
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,8 @@
"type": "object",
"properties": {
"mode": {"type": "string"},
"target": {"type": "integer"},
"published": {"type": "integer"},
"target": {"type": ["integer", "string"]},
"published": {"type": ["integer", "string"]},
"protocol": {"type": "string"}
},
"additionalProperties": false
Expand Down
4 changes: 2 additions & 2 deletions cli/compose/schema/data/config_schema_v3.6.json
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,8 @@
"type": "object",
"properties": {
"mode": {"type": "string"},
"target": {"type": "integer"},
"published": {"type": "integer"},
"target": {"type": ["integer", "string"]},
"published": {"type": ["integer", "string"]},
"protocol": {"type": "string"}
},
"additionalProperties": false
Expand Down
4 changes: 2 additions & 2 deletions cli/compose/schema/data/config_schema_v3.7.json
Original file line number Diff line number Diff line change
Expand Up @@ -219,8 +219,8 @@
"type": "object",
"properties": {
"mode": {"type": "string"},
"target": {"type": "integer"},
"published": {"type": "integer"},
"target": {"type": ["integer", "string"]},
"published": {"type": ["integer", "string"]},
"protocol": {"type": "string"}
},
"additionalProperties": false
Expand Down
4 changes: 2 additions & 2 deletions cli/compose/schema/data/config_schema_v3.8.json
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,8 @@
"type": "object",
"properties": {
"mode": {"type": "string"},
"target": {"type": "integer"},
"published": {"type": "integer"},
"target": {"type": ["integer", "string"]},
"published": {"type": ["integer", "string"]},
"protocol": {"type": "string"}
},
"additionalProperties": false
Expand Down
4 changes: 2 additions & 2 deletions cli/compose/schema/data/config_schema_v3.9.json
Original file line number Diff line number Diff line change
Expand Up @@ -222,8 +222,8 @@
"type": "object",
"properties": {
"mode": {"type": "string"},
"target": {"type": "integer"},
"published": {"type": "integer"},
"target": {"type": ["integer", "string"]},
"published": {"type": ["integer", "string"]},
"protocol": {"type": "string"}
},
"additionalProperties": false
Expand Down
Loading