@withgateway/sdk 0.2.6 → 0.2.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +24 -8
- package/dist/cli.js +15468 -11263
- package/dist/cli.js.map +1 -1
- package/dist/cli.mjs +15233 -11028
- package/dist/cli.mjs.map +1 -1
- package/dist/files/index.d.mts +1 -1
- package/dist/files/index.d.ts +1 -1
- package/dist/files/index.js +291 -40
- package/dist/files/index.js.map +1 -1
- package/dist/files/index.mjs +284 -37
- package/dist/files/index.mjs.map +1 -1
- package/dist/hooks/index.d.mts +1 -1
- package/dist/hooks/index.d.ts +1 -1
- package/dist/hooks/index.js +203 -47
- package/dist/hooks/index.js.map +1 -1
- package/dist/hooks/index.mjs +198 -42
- package/dist/hooks/index.mjs.map +1 -1
- package/dist/{index-BaKu5ZSU.d.mts → index-C3UzD0G3.d.mts} +75 -1
- package/dist/{index-BaKu5ZSU.d.ts → index-C3UzD0G3.d.ts} +75 -1
- package/dist/{index-CLkMr6VG.d.ts → index-D-_4Ibcn.d.ts} +36 -4
- package/dist/{index-CdbmO4aX.d.mts → index-FfK4Nump.d.mts} +36 -4
- package/dist/index.d.mts +3 -3
- package/dist/index.d.ts +3 -3
- package/dist/index.js +2149 -615
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +2053 -566
- package/dist/index.mjs.map +1 -1
- package/dist/traces/index.js +268 -31
- package/dist/traces/index.js.map +1 -1
- package/dist/traces/index.mjs +262 -28
- package/dist/traces/index.mjs.map +1 -1
- package/dist/tracing/index.js +19 -11
- package/dist/tracing/index.js.map +1 -1
- package/dist/tracing/index.mjs +19 -11
- package/dist/tracing/index.mjs.map +1 -1
- package/dist/worlds/index.d.mts +886 -117
- package/dist/worlds/index.d.ts +886 -117
- package/dist/worlds/index.js +1770 -452
- package/dist/worlds/index.js.map +1 -1
- package/dist/worlds/index.mjs +1752 -434
- package/dist/worlds/index.mjs.map +1 -1
- package/package.json +14 -10
- package/runtime/gatewaysdk/environment/_toml.py +3 -10
- package/runtime/gatewaysdk/environment/_world.py +95 -28
- package/runtime/gatewaysdk/environment/runtime.py +10 -6
- package/runtime/gatewaysdk/environment/schema/__main__.py +35 -0
- package/runtime/gatewaysdk/environment/schema/api.py +19 -23
- package/runtime/gatewaysdk/environment/schema/batch.py +68 -14
- package/runtime/gatewaysdk/environment/schema/bundle_runtime.py +4 -2
- package/runtime/gatewaysdk/environment/schema/cast.py +264 -0
- package/runtime/gatewaysdk/environment/schema/compiler.py +35 -1
- package/runtime/gatewaysdk/environment/schema/conform.py +28 -10
- package/runtime/gatewaysdk/environment/schema/connector.py +59 -14
- package/runtime/gatewaysdk/environment/schema/connectors/custom/handlers.py +38 -1
- package/runtime/gatewaysdk/environment/schema/connectors/google-drive/readers/merge.toml +3 -1
- package/runtime/gatewaysdk/environment/schema/credentials.py +20 -0
- package/runtime/gatewaysdk/environment/schema/csv_rows.py +117 -0
- package/runtime/gatewaysdk/environment/schema/cycles.py +121 -0
- package/runtime/gatewaysdk/environment/schema/data.py +21 -5
- package/runtime/gatewaysdk/environment/schema/grading.py +95 -13
- package/runtime/gatewaysdk/environment/schema/handler_errors.py +44 -0
- package/runtime/gatewaysdk/environment/schema/handler_scan.py +175 -0
- package/runtime/gatewaysdk/environment/schema/host.py +68 -115
- package/runtime/gatewaysdk/environment/schema/host_requests.py +78 -0
- package/runtime/gatewaysdk/environment/schema/host_surfaces.py +12 -0
- package/runtime/gatewaysdk/environment/schema/ingest.py +5 -18
- package/runtime/gatewaysdk/environment/schema/local.py +7 -10
- package/runtime/gatewaysdk/environment/schema/openapi_document.py +322 -0
- package/runtime/gatewaysdk/environment/schema/openapi_import.py +382 -0
- package/runtime/gatewaysdk/environment/schema/openapi_routes.py +441 -0
- package/runtime/gatewaysdk/environment/schema/openapi_schemas.py +629 -0
- package/runtime/gatewaysdk/environment/schema/openapi_source.py +210 -0
- package/runtime/gatewaysdk/environment/schema/platform.py +110 -299
- package/runtime/gatewaysdk/environment/schema/platform_import.py +182 -0
- package/runtime/gatewaysdk/environment/schema/platform_query.py +95 -0
- package/runtime/gatewaysdk/environment/schema/route_bindings.py +176 -0
- package/runtime/gatewaysdk/environment/schema/runtime.py +56 -7
- package/runtime/gatewaysdk/environment/schema/scaffold.py +64 -5
- package/runtime/gatewaysdk/environment/schema/skills/benchmarks-getting-started/SKILL.md +70 -22
- package/runtime/gatewaysdk/environment/schema/skills/connector-schema-authoring/SKILL.md +54 -4
- package/runtime/gatewaysdk/environment/schema/skills/hooks-getting-started/SKILL.md +27 -2
- package/runtime/gatewaysdk/environment/schema/skills/world-data-ingestion/ROWS.md +62 -0
- package/runtime/gatewaysdk/environment/schema/skills/world-data-ingestion/SKILL.md +91 -54
- package/runtime/gatewaysdk/environment/schema/skills/worlds-getting-started/SKILL.md +146 -146
- package/runtime/gatewaysdk/environment/schema/skills/worlds-getting-started/references/cli-mcp-map.md +11 -7
- package/runtime/gatewaysdk/environment/schema/skills/worlds-getting-started/references/gotchas.md +30 -0
- package/runtime/gatewaysdk/environment/schema/skills/worlds-getting-started/references/new-world-sandbox.md +43 -0
- package/runtime/gatewaysdk/environment/schema/skills/worlds-getting-started/references/openapi-import.md +90 -0
- package/runtime/gatewaysdk/environment/schema/store.py +15 -6
- package/runtime/gatewaysdk/environment/schema/store_contract.py +119 -0
- package/runtime/gatewaysdk/environment/schema/tasks.py +121 -32
- package/runtime/gatewaysdk/environment/schema/tools_world.py +32 -10
- package/runtime/gatewaysdk/environment/schema/tree_import.py +228 -0
- package/runtime/gatewaysdk/environment/schema/ui.py +18 -2
- package/runtime/gatewaysdk/environment/schema/validation.py +35 -0
- package/runtime/gatewaysdk/environment/schema/verbs.py +8 -2
- package/runtime/gatewaysdk/environment/world.py +42 -3
- package/runtime/gatewaysdk/environment/world_session_contract.py +57 -0
- package/skills/benchmarks-getting-started/SKILL.md +70 -22
- package/skills/connector-schema-authoring/SKILL.md +54 -4
- package/skills/connector-schema-authoring/connector-worlds.md +21 -7
- package/skills/connector-schema-authoring/world-schema.md +144 -83
- package/skills/hooks-getting-started/SKILL.md +27 -2
- package/skills/world-data-ingestion/ROWS.md +62 -0
- package/skills/world-data-ingestion/SKILL.md +91 -54
- package/skills/worlds-getting-started/SKILL.md +146 -146
- package/skills/worlds-getting-started/references/cli-mcp-map.md +11 -7
- package/skills/worlds-getting-started/references/gotchas.md +30 -0
- package/skills/worlds-getting-started/references/new-world-sandbox.md +43 -0
- package/skills/worlds-getting-started/references/openapi-import.md +90 -0
|
@@ -42,7 +42,7 @@ from .compiler import bundle_mode, fail
|
|
|
42
42
|
from .conform import PIN
|
|
43
43
|
from .connector import SPEC, load_spec
|
|
44
44
|
from .dependencies import local_dependency_trees
|
|
45
|
-
from .host_surfaces import UiSurface, api_target, load_ui_surface, static_file
|
|
45
|
+
from .host_surfaces import UiSurface, api_target, cross_site, load_ui_surface, static_file
|
|
46
46
|
from .runtime import call_operation, client_grant, task_card
|
|
47
47
|
from .tools_world import (
|
|
48
48
|
DependencyWorld,
|
|
@@ -362,9 +362,10 @@ def make_session_handler(
|
|
|
362
362
|
fail("host", "a schema world has no [redact] policy; seed it with redact=off")
|
|
363
363
|
return world.seed({"rows": rows, "mode": mode})
|
|
364
364
|
|
|
365
|
-
def grade(report: str | None = None) -> dict:
|
|
365
|
+
def grade(report: str | None = None, context: dict | None = None) -> dict:
|
|
366
366
|
# The local session grades by the world's own verifiers; an agent's
|
|
367
|
-
# report
|
|
367
|
+
# report and the other worlds' evidence are read by a session task's
|
|
368
|
+
# graders, which only the host runs.
|
|
368
369
|
return world.grade()
|
|
369
370
|
|
|
370
371
|
# The session protocol (:data:`verbs.SESSION_VERBS`) bound to this
|
|
@@ -373,6 +374,7 @@ def make_session_handler(
|
|
|
373
374
|
"GET tools": lambda: {"tools": list(world.served)},
|
|
374
375
|
"GET task": lambda: task_card(world),
|
|
375
376
|
"GET state": world.state,
|
|
377
|
+
"GET evidence": world.grader_state,
|
|
376
378
|
"POST grade": grade,
|
|
377
379
|
"POST reset": world.reset,
|
|
378
380
|
"POST seed": seed,
|
|
@@ -432,13 +434,8 @@ def make_session_handler(
|
|
|
432
434
|
assert app is not None and ui is not None
|
|
433
435
|
route = api_target(ui, path)
|
|
434
436
|
assert route is not None
|
|
435
|
-
#
|
|
436
|
-
|
|
437
|
-
# origin can POST to 127.0.0.1 without a preflight) and is refused.
|
|
438
|
-
# curl and the world's own tests send neither header.
|
|
439
|
-
site = self.headers.get("Sec-Fetch-Site")
|
|
440
|
-
origin = self.headers.get("Origin")
|
|
441
|
-
if (site and site not in ("same-origin", "none")) or (origin and not _local_origin(origin)):
|
|
437
|
+
# A page on any origin can POST to 127.0.0.1 without a preflight.
|
|
438
|
+
if cross_site({k.lower(): v for k, v in self.headers.items()}, _local_origin):
|
|
442
439
|
return self._reply(
|
|
443
440
|
403, {"error": "the ui's api prefix answers the page on this server, not another site"}
|
|
444
441
|
)
|
|
@@ -0,0 +1,322 @@
|
|
|
1
|
+
"""One view over Swagger 2.0, OpenAPI 3.0.x and 3.1 documents, for the importer.
|
|
2
|
+
|
|
3
|
+
The two generations spell the same things differently: ``definitions`` vs
|
|
4
|
+
``components.schemas``, ``host``+``basePath`` vs ``servers``, a body parameter
|
|
5
|
+
vs ``requestBody``, ``responses.200.schema`` vs ``responses.200.content.*.schema``.
|
|
6
|
+
:class:`OpenApiDocument` reads either and answers the importer's questions
|
|
7
|
+
(which schemas, which operations, what does this reference point at) in one
|
|
8
|
+
shape, so the conversion rules in ``openapi_import`` are written once.
|
|
9
|
+
|
|
10
|
+
Every reference is resolved against this document only (``#/...``); a reference
|
|
11
|
+
to another file or to nothing is refused with the JSON pointer of the ``$ref``.
|
|
12
|
+
Resolution is a lookup, not a walk, so a 990-schema document costs what its
|
|
13
|
+
size costs.
|
|
14
|
+
"""
|
|
15
|
+
|
|
16
|
+
from __future__ import annotations
|
|
17
|
+
|
|
18
|
+
import re
|
|
19
|
+
import urllib.parse
|
|
20
|
+
from collections.abc import Iterator
|
|
21
|
+
from dataclasses import dataclass, field
|
|
22
|
+
from typing import Any
|
|
23
|
+
|
|
24
|
+
from .validation import fail, pointer
|
|
25
|
+
|
|
26
|
+
#: The methods a connector operation may carry; the rest (TRACE, HEAD, OPTIONS)
|
|
27
|
+
#: are skipped with a reason.
|
|
28
|
+
METHODS = ("get", "post", "put", "patch", "delete")
|
|
29
|
+
_SKIPPED_METHODS = ("head", "options", "trace")
|
|
30
|
+
#: Response content types whose schema describes the rows a route answers.
|
|
31
|
+
_JSON_MEDIA = re.compile(r"^application/([a-z0-9.+-]*\+)?json\b", re.IGNORECASE)
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def unescape(part: str) -> str:
|
|
35
|
+
return part.replace("~1", "/").replace("~0", "~")
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def _first_line(text: Any) -> str:
|
|
39
|
+
lines = str(text).strip().splitlines() if isinstance(text, str) else []
|
|
40
|
+
return lines[0].strip() if lines else ""
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
@dataclass(frozen=True)
|
|
44
|
+
class Parameter:
|
|
45
|
+
name: str
|
|
46
|
+
location: str # path | query | header | cookie | body | formData
|
|
47
|
+
required: bool
|
|
48
|
+
schema: dict
|
|
49
|
+
pointer: str
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
@dataclass(frozen=True)
|
|
53
|
+
class OpenApiOperation:
|
|
54
|
+
"""One path+method of the document, parameters merged with the path item's."""
|
|
55
|
+
|
|
56
|
+
path: str
|
|
57
|
+
method: str
|
|
58
|
+
pointer: str
|
|
59
|
+
operation_id: str | None
|
|
60
|
+
summary: str
|
|
61
|
+
tags: tuple[str, ...]
|
|
62
|
+
parameters: tuple[Parameter, ...]
|
|
63
|
+
request_body: dict | None
|
|
64
|
+
responses: dict[str, dict | None] # status → JSON response schema (None: no body)
|
|
65
|
+
deprecated: bool = False
|
|
66
|
+
|
|
67
|
+
@property
|
|
68
|
+
def path_parameters(self) -> tuple[Parameter, ...]:
|
|
69
|
+
return tuple(p for p in self.parameters if p.location == "path")
|
|
70
|
+
|
|
71
|
+
@property
|
|
72
|
+
def query_parameters(self) -> tuple[Parameter, ...]:
|
|
73
|
+
return tuple(p for p in self.parameters if p.location == "query")
|
|
74
|
+
|
|
75
|
+
@property
|
|
76
|
+
def success(self) -> tuple[str, dict | None] | None:
|
|
77
|
+
"""The first documented 2xx response, status and schema."""
|
|
78
|
+
for status in sorted(self.responses):
|
|
79
|
+
if status.startswith("2"):
|
|
80
|
+
return status, self.responses[status]
|
|
81
|
+
return None
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
@dataclass
|
|
85
|
+
class OpenApiDocument:
|
|
86
|
+
raw: dict
|
|
87
|
+
warnings: list[str] = field(default_factory=list)
|
|
88
|
+
#: ``$ref`` → the node its chain ends at, so a chain of n hops is walked once.
|
|
89
|
+
_resolved: dict[str, Any] = field(default_factory=dict, repr=False)
|
|
90
|
+
|
|
91
|
+
# ------------------------------------------------------------- identity
|
|
92
|
+
|
|
93
|
+
@property
|
|
94
|
+
def is_swagger(self) -> bool:
|
|
95
|
+
return "swagger" in self.raw
|
|
96
|
+
|
|
97
|
+
@property
|
|
98
|
+
def version(self) -> str:
|
|
99
|
+
if self.is_swagger:
|
|
100
|
+
return f"swagger {self.raw.get('swagger')}"
|
|
101
|
+
return f"openapi {self.raw.get('openapi', '?')}"
|
|
102
|
+
|
|
103
|
+
@property
|
|
104
|
+
def info(self) -> dict:
|
|
105
|
+
info = self.raw.get("info")
|
|
106
|
+
return info if isinstance(info, dict) else {}
|
|
107
|
+
|
|
108
|
+
@property
|
|
109
|
+
def title(self) -> str:
|
|
110
|
+
title = self.info.get("title")
|
|
111
|
+
return title if isinstance(title, str) and title.strip() else "Imported API"
|
|
112
|
+
|
|
113
|
+
@property
|
|
114
|
+
def schemas_pointer(self) -> str:
|
|
115
|
+
return "/definitions" if self.is_swagger else "/components/schemas"
|
|
116
|
+
|
|
117
|
+
@property
|
|
118
|
+
def schemas(self) -> dict[str, Any]:
|
|
119
|
+
node = self.raw.get("definitions") if self.is_swagger else (self.raw.get("components") or {}).get("schemas")
|
|
120
|
+
if node is None:
|
|
121
|
+
return {}
|
|
122
|
+
if not isinstance(node, dict):
|
|
123
|
+
fail("spec", f"{self.schemas_pointer} must be an object", self.schemas_pointer)
|
|
124
|
+
return node
|
|
125
|
+
|
|
126
|
+
def base_url(self) -> str | None:
|
|
127
|
+
"""The first server's URL (variables at their defaults); Swagger's
|
|
128
|
+
``schemes``+``host``+``basePath``. None when the document names no host."""
|
|
129
|
+
if self.is_swagger:
|
|
130
|
+
return self._swagger_base_url()
|
|
131
|
+
servers = self.raw.get("servers") or []
|
|
132
|
+
server = servers[0] if isinstance(servers, list) and servers and isinstance(servers[0], dict) else None
|
|
133
|
+
url = server.get("url") if server else None
|
|
134
|
+
if not isinstance(url, str) or not url:
|
|
135
|
+
return None
|
|
136
|
+
for name, variable in (server.get("variables") or {}).items():
|
|
137
|
+
default = variable.get("default", "") if isinstance(variable, dict) else ""
|
|
138
|
+
url = url.replace("{" + name + "}", str(default))
|
|
139
|
+
if "://" not in url:
|
|
140
|
+
return None
|
|
141
|
+
return url.rstrip("/")
|
|
142
|
+
|
|
143
|
+
def _swagger_base_url(self) -> str | None:
|
|
144
|
+
host = self.raw.get("host")
|
|
145
|
+
if not isinstance(host, str) or not host:
|
|
146
|
+
return None
|
|
147
|
+
schemes = self.raw.get("schemes") or ["https"]
|
|
148
|
+
scheme = schemes[0] if isinstance(schemes, list) and schemes else "https"
|
|
149
|
+
return f"{scheme}://{host}{(self.raw.get('basePath') or '').rstrip('/')}"
|
|
150
|
+
|
|
151
|
+
def security_schemes(self) -> dict[str, dict]:
|
|
152
|
+
node = (
|
|
153
|
+
self.raw.get("securityDefinitions")
|
|
154
|
+
if self.is_swagger
|
|
155
|
+
else (self.raw.get("components") or {}).get("securitySchemes")
|
|
156
|
+
)
|
|
157
|
+
return {k: v for k, v in (node or {}).items() if isinstance(v, dict)} if isinstance(node, dict) else {}
|
|
158
|
+
|
|
159
|
+
# ------------------------------------------------------------- references
|
|
160
|
+
|
|
161
|
+
def resolve(self, node: Any, at: str) -> Any:
|
|
162
|
+
"""Follow ``$ref`` hops until a non-reference; refuse unknown targets."""
|
|
163
|
+
seen: list[str] = []
|
|
164
|
+
while isinstance(node, dict) and isinstance(node.get("$ref"), str):
|
|
165
|
+
ref = node["$ref"]
|
|
166
|
+
if ref in self._resolved:
|
|
167
|
+
node = self._resolved[ref]
|
|
168
|
+
break
|
|
169
|
+
if ref in seen:
|
|
170
|
+
fail("reference", f"$ref cycle {' -> '.join([*seen, ref])} never reaches a schema", at)
|
|
171
|
+
seen.append(ref)
|
|
172
|
+
node = self.lookup(ref, at)
|
|
173
|
+
for ref in seen:
|
|
174
|
+
self._resolved[ref] = node
|
|
175
|
+
return node
|
|
176
|
+
|
|
177
|
+
def lookup(self, ref: str, at: str) -> Any:
|
|
178
|
+
if not ref.startswith("#/"):
|
|
179
|
+
fail("reference", f"Only references into this document are supported, got {ref}", at)
|
|
180
|
+
node: Any = self.raw
|
|
181
|
+
for part in ref[2:].split("/"):
|
|
182
|
+
key = unescape(urllib.parse.unquote(part))
|
|
183
|
+
if isinstance(node, list) and key.isdigit() and int(key) < len(node):
|
|
184
|
+
node = node[int(key)]
|
|
185
|
+
continue
|
|
186
|
+
if not isinstance(node, dict) or key not in node:
|
|
187
|
+
fail("reference", f"Unknown $ref {ref}", at)
|
|
188
|
+
node = node[key]
|
|
189
|
+
return node
|
|
190
|
+
|
|
191
|
+
def schema_name(self, ref: str) -> str | None:
|
|
192
|
+
"""The name of a top-level schema a reference points at, or None."""
|
|
193
|
+
prefix = "#" + self.schemas_pointer + "/"
|
|
194
|
+
if not ref.startswith(prefix):
|
|
195
|
+
return None
|
|
196
|
+
rest = ref[len(prefix) :]
|
|
197
|
+
return unescape(urllib.parse.unquote(rest)) if "/" not in rest else None
|
|
198
|
+
|
|
199
|
+
# ------------------------------------------------------------- operations
|
|
200
|
+
|
|
201
|
+
def operations(self) -> Iterator[OpenApiOperation]:
|
|
202
|
+
paths = self.raw.get("paths")
|
|
203
|
+
if not isinstance(paths, dict):
|
|
204
|
+
fail("spec", "paths must be an object", "/paths")
|
|
205
|
+
for path in sorted(paths):
|
|
206
|
+
item = self.resolve(paths[path], f"/paths/{pointer(path)}")
|
|
207
|
+
if not isinstance(item, dict):
|
|
208
|
+
fail("spec", "A path item must be an object", f"/paths/{pointer(path)}")
|
|
209
|
+
shared = self._parameters(item.get("parameters"), f"/paths/{pointer(path)}/parameters")
|
|
210
|
+
for method in METHODS:
|
|
211
|
+
if method not in item:
|
|
212
|
+
continue
|
|
213
|
+
yield self._operation(path, method, item[method], shared)
|
|
214
|
+
|
|
215
|
+
def skipped_methods(self) -> Iterator[tuple[str, str, str]]:
|
|
216
|
+
"""(path, method, pointer) for methods a connector cannot serve."""
|
|
217
|
+
paths = self.raw.get("paths")
|
|
218
|
+
if not isinstance(paths, dict):
|
|
219
|
+
return
|
|
220
|
+
for path in sorted(paths):
|
|
221
|
+
item = paths[path]
|
|
222
|
+
if not isinstance(item, dict):
|
|
223
|
+
continue
|
|
224
|
+
for method in _SKIPPED_METHODS:
|
|
225
|
+
if method in item:
|
|
226
|
+
yield path, method, f"/paths/{pointer(path)}/{method}"
|
|
227
|
+
|
|
228
|
+
def _operation(self, path: str, method: str, node: Any, shared: list[Parameter]) -> OpenApiOperation:
|
|
229
|
+
at = f"/paths/{pointer(path)}/{method}"
|
|
230
|
+
if not isinstance(node, dict):
|
|
231
|
+
fail("spec", "An operation must be an object", at)
|
|
232
|
+
own = self._parameters(node.get("parameters"), f"{at}/parameters")
|
|
233
|
+
merged = {(p.location, p.name): p for p in shared}
|
|
234
|
+
merged.update({(p.location, p.name): p for p in own})
|
|
235
|
+
body = next((p.schema for p in merged.values() if p.location == "body"), None)
|
|
236
|
+
parameters = tuple(p for p in merged.values() if p.location != "body")
|
|
237
|
+
operation_id = node.get("operationId")
|
|
238
|
+
tags = (
|
|
239
|
+
tuple(str(t) for t in node.get("tags", []) if isinstance(t, str))
|
|
240
|
+
if isinstance(node.get("tags"), list)
|
|
241
|
+
else ()
|
|
242
|
+
)
|
|
243
|
+
return OpenApiOperation(
|
|
244
|
+
path=path,
|
|
245
|
+
method=method.upper(),
|
|
246
|
+
pointer=at,
|
|
247
|
+
operation_id=operation_id if isinstance(operation_id, str) and operation_id.strip() else None,
|
|
248
|
+
summary=_first_line(node.get("summary") or node.get("description")),
|
|
249
|
+
tags=tags,
|
|
250
|
+
parameters=parameters,
|
|
251
|
+
request_body=body if self.is_swagger else self._request_body(node.get("requestBody"), f"{at}/requestBody"),
|
|
252
|
+
responses=self._responses(node.get("responses"), f"{at}/responses"),
|
|
253
|
+
deprecated=node.get("deprecated") is True,
|
|
254
|
+
)
|
|
255
|
+
|
|
256
|
+
def _parameters(self, node: Any, at: str) -> list[Parameter]:
|
|
257
|
+
if node is None:
|
|
258
|
+
return []
|
|
259
|
+
if not isinstance(node, list):
|
|
260
|
+
fail("spec", "parameters must be an array", at)
|
|
261
|
+
out: list[Parameter] = []
|
|
262
|
+
for index, raw in enumerate(node):
|
|
263
|
+
here = f"{at}/{index}"
|
|
264
|
+
parameter = self.resolve(raw, here)
|
|
265
|
+
if not isinstance(parameter, dict) or not isinstance(parameter.get("name"), str):
|
|
266
|
+
fail("spec", "A parameter needs a name", here)
|
|
267
|
+
location = parameter.get("in")
|
|
268
|
+
if not isinstance(location, str):
|
|
269
|
+
fail("spec", "A parameter needs `in`", here)
|
|
270
|
+
schema = parameter.get("schema")
|
|
271
|
+
if schema is None:
|
|
272
|
+
schema = {k: v for k, v in parameter.items() if k in _SWAGGER_PARAMETER_SCHEMA_KEYS}
|
|
273
|
+
out.append(
|
|
274
|
+
Parameter(
|
|
275
|
+
name=parameter["name"],
|
|
276
|
+
location=location,
|
|
277
|
+
required=parameter.get("required") is True or location == "path",
|
|
278
|
+
schema=schema if isinstance(schema, dict) else {},
|
|
279
|
+
pointer=here,
|
|
280
|
+
)
|
|
281
|
+
)
|
|
282
|
+
return out
|
|
283
|
+
|
|
284
|
+
def _request_body(self, node: Any, at: str) -> dict | None:
|
|
285
|
+
if node is None:
|
|
286
|
+
return None
|
|
287
|
+
body = self.resolve(node, at)
|
|
288
|
+
if not isinstance(body, dict):
|
|
289
|
+
fail("spec", "requestBody must be an object", at)
|
|
290
|
+
return self._json_schema(body.get("content"), f"{at}/content")
|
|
291
|
+
|
|
292
|
+
def _responses(self, node: Any, at: str) -> dict[str, dict | None]:
|
|
293
|
+
if node is None:
|
|
294
|
+
return {}
|
|
295
|
+
if not isinstance(node, dict):
|
|
296
|
+
fail("spec", "responses must be an object", at)
|
|
297
|
+
out: dict[str, dict | None] = {}
|
|
298
|
+
for status, raw in node.items():
|
|
299
|
+
here = f"{at}/{pointer(status)}"
|
|
300
|
+
response = self.resolve(raw, here)
|
|
301
|
+
if not isinstance(response, dict):
|
|
302
|
+
fail("spec", "A response must be an object", here)
|
|
303
|
+
if self.is_swagger:
|
|
304
|
+
schema = response.get("schema")
|
|
305
|
+
out[str(status)] = schema if isinstance(schema, dict) else None
|
|
306
|
+
continue
|
|
307
|
+
out[str(status)] = self._json_schema(response.get("content"), f"{here}/content")
|
|
308
|
+
return out
|
|
309
|
+
|
|
310
|
+
def _json_schema(self, content: Any, at: str) -> dict | None:
|
|
311
|
+
if not isinstance(content, dict):
|
|
312
|
+
return None
|
|
313
|
+
for media, body in content.items():
|
|
314
|
+
if _JSON_MEDIA.match(str(media)) and isinstance(body, dict) and isinstance(body.get("schema"), dict):
|
|
315
|
+
return body["schema"]
|
|
316
|
+
return None
|
|
317
|
+
|
|
318
|
+
|
|
319
|
+
#: Swagger 2.0 spells a non-body parameter's type on the parameter itself.
|
|
320
|
+
_SWAGGER_PARAMETER_SCHEMA_KEYS = frozenset(
|
|
321
|
+
{"type", "format", "items", "enum", "default", "minimum", "maximum", "minLength", "maxLength", "pattern"}
|
|
322
|
+
)
|