Skip to content

Commit b8b69fe

Browse files
committed
correct examples added to pydantic models @sanderegg
1 parent 3774f00 commit b8b69fe

File tree

3 files changed

+90
-71
lines changed
  • packages
    • models-library/src/models_library/api_schemas_long_running_tasks
    • service-library/src/servicelib/celery
  • services/api-server/src/simcore_service_api_server/models

3 files changed

+90
-71
lines changed

packages/models-library/src/models_library/api_schemas_long_running_tasks/base.py

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
from collections.abc import Awaitable, Callable
33
from typing import Annotated, TypeAlias
44

5-
from pydantic import BaseModel, Field, field_validator, validate_call
5+
from pydantic import BaseModel, ConfigDict, Field, field_validator, validate_call
6+
from pydantic.config import JsonDict
67

78
_logger = logging.getLogger(__name__)
89

@@ -23,17 +24,21 @@ class TaskProgress(BaseModel):
2324
message: ProgressMessage = ""
2425
percent: ProgressPercent = 0.0
2526

26-
model_config = {
27-
"json_schema_extra": {
28-
"examples": [
29-
{
30-
"task_id": "3ac48b54-a48d-4c5e-a6ac-dcaddb9eaa59",
31-
"message": "Halfway done",
32-
"percent": 0.5,
33-
}
34-
]
35-
}
36-
}
27+
@staticmethod
28+
def _update_json_schema_extra(schema: JsonDict) -> None:
29+
schema.update(
30+
{
31+
"examples": [
32+
{
33+
"task_id": "3ac48b54-a48d-4c5e-a6ac-dcaddb9eaa59",
34+
"message": "Halfway done",
35+
"percent": 0.5,
36+
}
37+
]
38+
}
39+
)
40+
41+
model_config = ConfigDict(json_schema_extra=_update_json_schema_extra)
3742

3843
# used to propagate progress updates internally
3944
_update_callback: Callable[["TaskProgress"], Awaitable[None]] | None = None

packages/service-library/src/servicelib/celery/models.py

Lines changed: 58 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
from uuid import UUID
55

66
from models_library.progress_bar import ProgressReport
7-
from pydantic import BaseModel, StringConstraints
7+
from pydantic import BaseModel, ConfigDict, StringConstraints
8+
from pydantic.config import JsonDict
89

910
TaskID: TypeAlias = str
1011
TaskName: TypeAlias = Annotated[
@@ -41,36 +42,40 @@ class Task(BaseModel):
4142
uuid: TaskUUID
4243
metadata: TaskMetadata
4344

44-
model_config = {
45-
"json_schema_extra": {
46-
"examples": [
47-
{
48-
"uuid": "123e4567-e89b-12d3-a456-426614174000",
49-
"metadata": {
50-
"name": "task1",
51-
"ephemeral": True,
52-
"queue": "default",
45+
@staticmethod
46+
def _update_json_schema_extra(schema: JsonDict) -> None:
47+
schema.update(
48+
{
49+
"examples": [
50+
{
51+
"uuid": "123e4567-e89b-12d3-a456-426614174000",
52+
"metadata": {
53+
"name": "task1",
54+
"ephemeral": True,
55+
"queue": "default",
56+
},
5357
},
54-
},
55-
{
56-
"uuid": "223e4567-e89b-12d3-a456-426614174001",
57-
"metadata": {
58-
"name": "task2",
59-
"ephemeral": False,
60-
"queue": "cpu_bound",
58+
{
59+
"uuid": "223e4567-e89b-12d3-a456-426614174001",
60+
"metadata": {
61+
"name": "task2",
62+
"ephemeral": False,
63+
"queue": "cpu_bound",
64+
},
6165
},
62-
},
63-
{
64-
"uuid": "323e4567-e89b-12d3-a456-426614174002",
65-
"metadata": {
66-
"name": "task3",
67-
"ephemeral": True,
68-
"queue": "default",
66+
{
67+
"uuid": "323e4567-e89b-12d3-a456-426614174002",
68+
"metadata": {
69+
"name": "task3",
70+
"ephemeral": True,
71+
"queue": "default",
72+
},
6973
},
70-
},
71-
]
72-
}
73-
}
74+
]
75+
}
76+
)
77+
78+
model_config = ConfigDict(json_schema_extra=_update_json_schema_extra)
7479

7580

7681
_TASK_DONE = {TaskState.SUCCESS, TaskState.FAILURE, TaskState.ABORTED}
@@ -104,27 +109,32 @@ class TaskStatus(BaseModel):
104109
task_state: TaskState
105110
progress_report: ProgressReport
106111

107-
model_config = {
108-
"json_schema_extra": {
109-
"examples": [
110-
{
111-
"task_uuid": "123e4567-e89b-12d3-a456-426614174000",
112-
"task_state": "SUCCESS",
113-
"progress_report": {
114-
"actual_value": 0.5,
115-
"total": 1.0,
116-
"attempts": 1,
117-
"unit": "Byte",
118-
"message": {
119-
"description": "some description",
120-
"current": 12.2,
121-
"total": 123,
112+
@staticmethod
113+
def _update_json_schema_extra(schema: JsonDict) -> None:
114+
115+
schema.update(
116+
{
117+
"examples": [
118+
{
119+
"task_uuid": "123e4567-e89b-12d3-a456-426614174000",
120+
"task_state": "SUCCESS",
121+
"progress_report": {
122+
"actual_value": 0.5,
123+
"total": 1.0,
124+
"attempts": 1,
125+
"unit": "Byte",
126+
"message": {
127+
"description": "some description",
128+
"current": 12.2,
129+
"total": 123,
130+
},
122131
},
123-
},
124-
}
125-
]
126-
}
127-
}
132+
}
133+
]
134+
}
135+
)
136+
137+
model_config = ConfigDict(json_schema_extra=_update_json_schema_extra)
128138

129139
@property
130140
def is_done(self) -> bool:

services/api-server/src/simcore_service_api_server/models/api_resources.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -103,17 +103,21 @@ def _url_missing_only_job_id(url: str | None) -> str | None:
103103

104104

105105
class JobLinks(BaseModel):
106-
model_config = ConfigDict(
107-
json_schema_extra={
108-
"examples": [
109-
{
110-
"url_template": "https://api.osparc.io/v0/jobs/{job_id}",
111-
"runner_url_template": "https://runner.osparc.io/dashboard",
112-
"outputs_url_template": "https://api.osparc.io/v0/jobs/{job_id}/outputs",
113-
}
114-
]
115-
}
116-
)
106+
@staticmethod
107+
def _update_json_schema_extra(schema: dict) -> None:
108+
schema.update(
109+
{
110+
"examples": [
111+
{
112+
"url_template": "https://api.osparc.io/v0/jobs/{job_id}",
113+
"runner_url_template": "https://runner.osparc.io/dashboard",
114+
"outputs_url_template": "https://api.osparc.io/v0/jobs/{job_id}/outputs",
115+
}
116+
]
117+
}
118+
)
119+
120+
model_config = ConfigDict(json_schema_extra=_update_json_schema_extra)
117121

118122
url_template: Annotated[str | None, AfterValidator(_url_missing_only_job_id)]
119123
runner_url_template: str | None

0 commit comments

Comments
 (0)