Skip to content

Commit 7767e22

Browse files
authored
Fix server compatibility with 0.20.[27-29] clients (#4148)
Ignore `GatewayConfiguration.router` in client requests if `null`, which is what 0.20.27, 0.20.28, and 0.20.29 clients send by default.
1 parent df6b54d commit 7767e22

3 files changed

Lines changed: 73 additions & 4 deletions

File tree

‎src/dstack/_internal/core/models/common.py‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,25 @@ class FrozenCoreModel(CoreModel):
5757
model_config = ConfigDict(frozen=True)
5858

5959

60+
def pop_null_field(values: Any, *path: str) -> Any:
61+
"""
62+
Drop a field at `path` from the `values` nested dict if it's set to `null`.
63+
64+
Mutates and returns the same `values` object.
65+
"""
66+
if not isinstance(values, dict):
67+
return values
68+
node = values
69+
for key in path[:-1]:
70+
node = node.get(key)
71+
if not isinstance(node, dict):
72+
return values
73+
field = path[-1]
74+
if field in node and node[field] is None:
75+
del node[field]
76+
return values
77+
78+
6079
T = TypeVar("T")
6180

6281
_type_adapters: dict[Any, TypeAdapter] = {}

‎src/dstack/_internal/server/schemas/gateways.py‎

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
from typing import Annotated, List, Optional
1+
from typing import Annotated, Any, List, Optional
22

3-
from pydantic import Field
3+
from pydantic import Field, model_validator
44

5-
from dstack._internal.core.models.common import CoreModel
5+
from dstack._internal.core.models.common import CoreModel, pop_null_field
66
from dstack._internal.core.models.gateways import (
77
ApplyGatewayPlanInput,
88
GatewayConfiguration,
@@ -25,6 +25,12 @@ class GetGatewayRequest(CoreModel):
2525
class GetGatewayPlanRequest(CoreModel):
2626
spec: GatewaySpec
2727

28+
@model_validator(mode="before")
29+
@classmethod
30+
def _drop_null_router(cls, values: Any) -> Any:
31+
# Compatibility with 0.20.27, 0.20.28, 0.20.29 clients
32+
return pop_null_field(values, "spec", "configuration", "router")
33+
2834

2935
class ApplyGatewayPlanRequest(CoreModel):
3036
plan: ApplyGatewayPlanInput
@@ -35,6 +41,14 @@ class ApplyGatewayPlanRequest(CoreModel):
3541
),
3642
]
3743

44+
@model_validator(mode="before")
45+
@classmethod
46+
def _drop_null_router(cls, values: Any) -> Any:
47+
# Compatibility with 0.20.27, 0.20.28, 0.20.29 clients
48+
values = pop_null_field(values, "plan", "spec", "configuration", "router")
49+
values = pop_null_field(values, "plan", "current_resource", "configuration", "router")
50+
return values
51+
3852

3953
class DeleteGatewaysRequest(CoreModel):
4054
names: List[str]

‎src/tests/_internal/core/models/test_common.py‎

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import pytest
22

3-
from dstack._internal.core.models.common import EntityReference
3+
from dstack._internal.core.models.common import EntityReference, pop_null_field
44

55

66
class TestEntityReferenceParse:
@@ -25,3 +25,39 @@ def test_valid(self, value, expected):
2525
def test_invalid(self, value: str):
2626
with pytest.raises(ValueError, match="Invalid entity reference"):
2727
EntityReference.parse(value)
28+
29+
30+
class TestPopNullField:
31+
def test_drops_null_field_at_top_level(self):
32+
values = {"router": None, "backend": "aws"}
33+
assert pop_null_field(values, "router") == {"backend": "aws"}
34+
35+
def test_drops_null_field_in_nested_dict(self):
36+
values = {"spec": {"configuration": {"router": None, "backend": "aws"}}}
37+
assert pop_null_field(values, "spec", "configuration", "router") == {
38+
"spec": {"configuration": {"backend": "aws"}}
39+
}
40+
41+
def test_leaves_non_null_field_untouched(self):
42+
values = {"configuration": {"router": "some-router"}}
43+
assert pop_null_field(values, "configuration", "router") == {
44+
"configuration": {"router": "some-router"}
45+
}
46+
47+
def test_field_absent(self):
48+
values = {"configuration": {"backend": "aws"}}
49+
assert pop_null_field(values, "configuration", "router") == {
50+
"configuration": {"backend": "aws"}
51+
}
52+
53+
def test_intermediate_path_missing(self):
54+
values = {"backend": "aws"}
55+
assert pop_null_field(values, "configuration", "router") == {"backend": "aws"}
56+
57+
def test_intermediate_path_not_a_dict(self):
58+
values = {"configuration": None}
59+
assert pop_null_field(values, "configuration", "router") == {"configuration": None}
60+
61+
def test_top_level_not_a_dict(self):
62+
values = "not-a-dict"
63+
assert pop_null_field(values, "configuration", "router") == "not-a-dict"

0 commit comments

Comments
 (0)