Skip to content

Commit ede8a16

Browse files
author
Andrei Neagu
committed
fix broken test
1 parent 7e56899 commit ede8a16

File tree

4 files changed

+40
-31
lines changed

4 files changed

+40
-31
lines changed

packages/common-library/src/common_library/serialization.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
from datetime import timedelta
22
from typing import Any
33

4-
from pydantic import BaseModel, SecretStr
4+
from pydantic import BaseModel, SecretStr, TypeAdapter
55
from pydantic_core import Url
66

7-
from .pydantic_fields_extension import get_type
8-
97

108
def model_dump_with_secrets(
119
settings_obj: BaseModel, *, show_secrets: bool, **pydantic_export_options
@@ -31,12 +29,12 @@ def model_dump_with_secrets(
3129
data[field_name] = str(field_data)
3230

3331
elif isinstance(field_data, dict):
34-
field_type = get_type(settings_obj.model_fields[field_name])
35-
if issubclass(field_type, BaseModel):
36-
data[field_name] = model_dump_with_secrets(
37-
field_type.model_validate(field_data),
38-
show_secrets=show_secrets,
39-
**pydantic_export_options,
40-
)
32+
field_type = settings_obj.model_fields[field_name].annotation
33+
34+
data[field_name] = model_dump_with_secrets(
35+
TypeAdapter(field_type).validate_python(field_data),
36+
show_secrets=show_secrets,
37+
**pydantic_export_options,
38+
)
4139

4240
return data

packages/models-library/src/models_library/api_schemas_clusters_keeper/clusters.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import datetime
22
from enum import auto
33

4-
from pydantic import AnyUrl, BaseModel
4+
from pydantic import AnyUrl, BaseModel, Field
55

66
from ..clusters import ClusterAuthentication
77
from ..users import UserID
@@ -17,7 +17,7 @@ class ClusterState(StrAutoEnum):
1717

1818
class OnDemandCluster(BaseModel):
1919
endpoint: AnyUrl
20-
authentication: ClusterAuthentication
20+
authentication: ClusterAuthentication = Field(discriminator="discriminator_type")
2121
state: ClusterState
2222
user_id: UserID
2323
wallet_id: WalletID | None

packages/models-library/src/models_library/api_schemas_directorv2/clusters.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,9 @@ class ClusterDetailsGet(ClusterDetails):
114114

115115
class ClusterCreate(BaseCluster):
116116
owner: GroupID | None = None # type: ignore[assignment]
117-
authentication: ExternalClusterAuthentication
117+
authentication: ExternalClusterAuthentication = Field(
118+
discriminator="discriminator_type"
119+
)
118120
access_rights: dict[GroupID, ClusterAccessRights] = Field(
119121
alias="accessRights", default_factory=dict
120122
)
@@ -174,7 +176,7 @@ class ClusterPatch(BaseCluster):
174176
owner: GroupID | None = None # type: ignore[assignment]
175177
thumbnail: HttpUrl | None = None
176178
endpoint: AnyUrl | None = None # type: ignore[assignment]
177-
authentication: ExternalClusterAuthentication | None = None # type: ignore[assignment]
179+
authentication: ExternalClusterAuthentication | None = Field(None, discriminator="discriminator_type") # type: ignore[assignment]
178180
access_rights: dict[GroupID, ClusterAccessRights] | None = Field( # type: ignore[assignment]
179181
default=None, alias="accessRights"
180182
)
@@ -203,5 +205,7 @@ class ClusterPatch(BaseCluster):
203205
class ClusterPing(BaseModel):
204206
endpoint: AnyHttpUrl
205207
authentication: ClusterAuthentication = Field(
206-
..., description="Dask gateway authentication"
208+
...,
209+
description="Dask gateway authentication",
210+
discriminator="discriminator_type",
207211
)

packages/models-library/src/models_library/clusters.py

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -42,21 +42,21 @@ class ClusterAccessRights(BaseModel):
4242

4343

4444
class BaseAuthentication(BaseModel):
45-
type: str
45+
discriminator_type: str
4646

4747
model_config = ConfigDict(frozen=True, extra="forbid")
4848

4949

5050
class SimpleAuthentication(BaseAuthentication):
51-
type: Literal["simple"] = "simple"
51+
discriminator_type: Literal["simple"] = "simple"
5252
username: str
5353
password: SecretStr
5454

5555
model_config = ConfigDict(
5656
json_schema_extra={
5757
"examples": [
5858
{
59-
"type": "simple",
59+
"discriminator_type": "simple",
6060
"username": "someuser",
6161
"password": "somepassword",
6262
},
@@ -66,40 +66,45 @@ class SimpleAuthentication(BaseAuthentication):
6666

6767

6868
class KerberosAuthentication(BaseAuthentication):
69-
type: Literal["kerberos"] = "kerberos"
69+
discriminator_type: Literal["kerberos"] = "kerberos"
7070

7171
model_config = ConfigDict(
7272
json_schema_extra={
7373
"examples": [
7474
{
75-
"type": "kerberos",
75+
"discriminator_type": "kerberos",
7676
},
7777
]
7878
}
7979
)
8080

8181

8282
class JupyterHubTokenAuthentication(BaseAuthentication):
83-
type: Literal["jupyterhub"] = "jupyterhub"
83+
discriminator_type: Literal["jupyterhub"] = "jupyterhub"
8484
api_token: str
8585

8686
model_config = ConfigDict(
8787
json_schema_extra={
8888
"examples": [
89-
{"type": "jupyterhub", "api_token": "some_jupyterhub_token"},
89+
{
90+
"discriminator_type": "jupyterhub",
91+
"api_token": "some_jupyterhub_token",
92+
},
9093
]
9194
}
9295
)
9396

9497

9598
class NoAuthentication(BaseAuthentication):
96-
type: Literal["none"] = "none"
99+
discriminator_type: Literal["none"] = "none"
97100

98-
model_config = ConfigDict(json_schema_extra={"examples": [{"type": "none"}]})
101+
model_config = ConfigDict(
102+
json_schema_extra={"examples": [{"discriminator_type": "none"}]}
103+
)
99104

100105

101106
class TLSAuthentication(BaseAuthentication):
102-
type: Literal["tls"] = "tls"
107+
discriminator_type: Literal["tls"] = "tls"
103108
tls_ca_file: Path
104109
tls_client_cert: Path
105110
tls_client_key: Path
@@ -108,7 +113,7 @@ class TLSAuthentication(BaseAuthentication):
108113
json_schema_extra={
109114
"examples": [
110115
{
111-
"type": "tls",
116+
"discriminator_type": "tls",
112117
"tls_ca_file": "/path/to/ca_file",
113118
"tls_client_cert": "/path/to/cert_file",
114119
"tls_client_key": "/path/to/key_file",
@@ -140,7 +145,9 @@ class BaseCluster(BaseModel):
140145
)
141146
endpoint: AnyUrl
142147
authentication: ClusterAuthentication = Field(
143-
..., description="Dask gateway authentication"
148+
...,
149+
description="Dask gateway authentication",
150+
discriminator="discriminator_type",
144151
)
145152
access_rights: dict[GroupID, ClusterAccessRights] = Field(default_factory=dict)
146153

@@ -169,7 +176,7 @@ class Cluster(BaseCluster):
169176
"owner": 1456,
170177
"endpoint": "tcp://default-dask-scheduler:8786",
171178
"authentication": {
172-
"type": "simple",
179+
"discriminator_type": "simple",
173180
"username": "someuser",
174181
"password": "somepassword",
175182
},
@@ -181,7 +188,7 @@ class Cluster(BaseCluster):
181188
"owner": 12,
182189
"endpoint": "https://registry.osparc-development.fake.dev",
183190
"authentication": {
184-
"type": "simple",
191+
"discriminator_type": "simple",
185192
"username": "someuser",
186193
"password": "somepassword",
187194
},
@@ -193,7 +200,7 @@ class Cluster(BaseCluster):
193200
"type": ClusterTypeInModel.AWS,
194201
"owner": 154,
195202
"endpoint": "https://registry.osparc-development.fake.dev",
196-
"authentication": {"type": "kerberos"},
203+
"authentication": {"discriminator_type": "kerberos"},
197204
"access_rights": {
198205
154: CLUSTER_ADMIN_RIGHTS, # type: ignore[dict-item]
199206
12: CLUSTER_MANAGER_RIGHTS, # type: ignore[dict-item]
@@ -208,7 +215,7 @@ class Cluster(BaseCluster):
208215
"owner": 2321,
209216
"endpoint": "https://registry.osparc-development.fake2.dev",
210217
"authentication": {
211-
"type": "jupyterhub",
218+
"discriminator_type": "jupyterhub",
212219
"api_token": "some_fake_token",
213220
},
214221
"access_rights": {

0 commit comments

Comments
 (0)