Skip to content

Commit 37fc12c

Browse files
pierrejeambrunjason810496
authored andcommitted
Fix OpenAPI schema for get_log API (#50547) (#51357)
* Fix openapi schema for get_log API * Fix test_log (cherry picked from commit 08cc57d) Co-authored-by: LIU ZHE YOU <[email protected]>
1 parent 0794a70 commit 37fc12c

File tree

11 files changed

+52
-23
lines changed

11 files changed

+52
-23
lines changed

airflow-core/src/airflow/api_fastapi/common/headers.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,30 @@ def header_accept_json_or_text_depends(
4747

4848

4949
HeaderAcceptJsonOrText = Annotated[Mimetype, Depends(header_accept_json_or_text_depends)]
50+
51+
52+
def header_accept_json_or_ndjson_depends(
53+
accept: Annotated[
54+
str,
55+
Header(
56+
json_schema_extra={
57+
"type": "string",
58+
"enum": [Mimetype.JSON, Mimetype.NDJSON, Mimetype.ANY],
59+
}
60+
),
61+
] = Mimetype.ANY,
62+
) -> Mimetype:
63+
if accept.startswith(Mimetype.ANY):
64+
return Mimetype.ANY
65+
if accept.startswith(Mimetype.JSON):
66+
return Mimetype.JSON
67+
if accept.startswith(Mimetype.NDJSON) or accept.startswith(Mimetype.ANY):
68+
return Mimetype.NDJSON
69+
70+
raise HTTPException(
71+
status_code=status.HTTP_406_NOT_ACCEPTABLE,
72+
detail="Only application/json or application/x-ndjson is supported",
73+
)
74+
75+
76+
HeaderAcceptJsonOrNdjson = Annotated[Mimetype, Depends(header_accept_json_or_ndjson_depends)]

airflow-core/src/airflow/api_fastapi/common/types.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ class Mimetype(str, Enum):
7272

7373
TEXT = "text/plain"
7474
JSON = "application/json"
75+
NDJSON = "application/x-ndjson"
7576
ANY = "*/*"
7677

7778

airflow-core/src/airflow/api_fastapi/core_api/openapi/v1-rest-api-generated.yaml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6354,7 +6354,7 @@ paths:
63546354
type: string
63556355
enum:
63566356
- application/json
6357-
- text/plain
6357+
- application/x-ndjson
63586358
- '*/*'
63596359
default: '*/*'
63606360
title: Accept
@@ -6365,10 +6365,12 @@ paths:
63656365
application/json:
63666366
schema:
63676367
$ref: '#/components/schemas/TaskInstancesLogResponse'
6368-
text/plain:
6368+
application/x-ndjson:
63696369
schema:
63706370
type: string
6371-
example: 'content
6371+
example: '{"content": "content"}
6372+
6373+
{"content": "content"}
63726374
63736375
'
63746376
'401':

airflow-core/src/airflow/api_fastapi/core_api/routes/public/log.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828

2929
from airflow.api_fastapi.common.dagbag import DagBagDep
3030
from airflow.api_fastapi.common.db.common import SessionDep
31-
from airflow.api_fastapi.common.headers import HeaderAcceptJsonOrText
31+
from airflow.api_fastapi.common.headers import HeaderAcceptJsonOrNdjson
3232
from airflow.api_fastapi.common.router import AirflowRouter
3333
from airflow.api_fastapi.common.types import Mimetype
3434
from airflow.api_fastapi.core_api.datamodels.log import TaskInstancesLogResponse
@@ -43,13 +43,14 @@
4343
tags=["Task Instance"], prefix="/dags/{dag_id}/dagRuns/{dag_run_id}/taskInstances"
4444
)
4545

46-
text_example_response_for_get_log = {
47-
Mimetype.TEXT: {
46+
ndjson_example_response_for_get_log = {
47+
Mimetype.NDJSON: {
4848
"schema": {
4949
"type": "string",
5050
"example": textwrap.dedent(
5151
"""\
52-
content
52+
{"content": "content"}
53+
{"content": "content"}
5354
"""
5455
),
5556
}
@@ -63,7 +64,7 @@
6364
**create_openapi_http_exception_doc([status.HTTP_404_NOT_FOUND]),
6465
status.HTTP_200_OK: {
6566
"description": "Successful Response",
66-
"content": text_example_response_for_get_log,
67+
"content": ndjson_example_response_for_get_log,
6768
},
6869
},
6970
dependencies=[Depends(requires_access_dag("GET", DagAccessEntity.TASK_LOGS))],
@@ -75,7 +76,7 @@ def get_log(
7576
dag_run_id: str,
7677
task_id: str,
7778
try_number: PositiveInt,
78-
accept: HeaderAcceptJsonOrText,
79+
accept: HeaderAcceptJsonOrNdjson,
7980
request: Request,
8081
dag_bag: DagBagDep,
8182
session: SessionDep,

airflow-core/src/airflow/ui/openapi-gen/queries/common.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1267,7 +1267,7 @@ export const UseTaskInstanceServiceGetLogKeyFn = (
12671267
token,
12681268
tryNumber,
12691269
}: {
1270-
accept?: "application/json" | "text/plain" | "*/*";
1270+
accept?: "application/json" | "*/*" | "application/x-ndjson";
12711271
dagId: string;
12721272
dagRunId: string;
12731273
fullContent?: boolean;

airflow-core/src/airflow/ui/openapi-gen/queries/ensureQueryData.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1744,7 +1744,7 @@ export const ensureUseTaskInstanceServiceGetLogData = (
17441744
token,
17451745
tryNumber,
17461746
}: {
1747-
accept?: "application/json" | "text/plain" | "*/*";
1747+
accept?: "application/json" | "*/*" | "application/x-ndjson";
17481748
dagId: string;
17491749
dagRunId: string;
17501750
fullContent?: boolean;

airflow-core/src/airflow/ui/openapi-gen/queries/prefetch.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1744,7 +1744,7 @@ export const prefetchUseTaskInstanceServiceGetLog = (
17441744
token,
17451745
tryNumber,
17461746
}: {
1747-
accept?: "application/json" | "text/plain" | "*/*";
1747+
accept?: "application/json" | "*/*" | "application/x-ndjson";
17481748
dagId: string;
17491749
dagRunId: string;
17501750
fullContent?: boolean;

airflow-core/src/airflow/ui/openapi-gen/queries/queries.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2081,7 +2081,7 @@ export const useTaskInstanceServiceGetLog = <
20812081
token,
20822082
tryNumber,
20832083
}: {
2084-
accept?: "application/json" | "text/plain" | "*/*";
2084+
accept?: "application/json" | "*/*" | "application/x-ndjson";
20852085
dagId: string;
20862086
dagRunId: string;
20872087
fullContent?: boolean;

airflow-core/src/airflow/ui/openapi-gen/queries/suspense.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2058,7 +2058,7 @@ export const useTaskInstanceServiceGetLogSuspense = <
20582058
token,
20592059
tryNumber,
20602060
}: {
2061-
accept?: "application/json" | "text/plain" | "*/*";
2061+
accept?: "application/json" | "*/*" | "application/x-ndjson";
20622062
dagId: string;
20632063
dagRunId: string;
20642064
fullContent?: boolean;

airflow-core/src/airflow/ui/openapi-gen/requests/types.gen.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2386,7 +2386,7 @@ export type PatchTaskInstanceDryRunData = {
23862386
export type PatchTaskInstanceDryRunResponse = TaskInstanceCollectionResponse;
23872387

23882388
export type GetLogData = {
2389-
accept?: "application/json" | "text/plain" | "*/*";
2389+
accept?: "application/json" | "application/x-ndjson" | "*/*";
23902390
dagId: string;
23912391
dagRunId: string;
23922392
fullContent?: boolean;

0 commit comments

Comments
 (0)