Skip to content

Commit a287ebc

Browse files
authored
revert: "feat: show fastapi routes in playground ui" (#5323)
Revert "feat: show fastapi routes in playground ui (#5314)" This reverts commit b9f63e6.
1 parent c9593af commit a287ebc

File tree

4 files changed

+15
-81
lines changed

4 files changed

+15
-81
lines changed

src/_bentoml_sdk/method.py

Lines changed: 11 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from __future__ import annotations
22

3-
import copy
43
import functools
54
import inspect
65
import sys
@@ -197,7 +196,7 @@ def schema(self) -> dict[str, t.Any]:
197196
def openapi_request(self) -> dict[str, t.Any]:
198197
from .service.openapi import REF_TEMPLATE
199198

200-
input = flatten_field(
199+
input = _flatten_field(
201200
_only_include(
202201
self.input_spec.model_json_schema(
203202
ref_template=REF_TEMPLATE, mode="serialization"
@@ -224,7 +223,7 @@ def openapi_request(self) -> dict[str, t.Any]:
224223
def openapi_response(self) -> dict[str, t.Any]:
225224
from .service.openapi import REF_TEMPLATE
226225

227-
output = flatten_field(
226+
output = _flatten_field(
228227
_only_include(
229228
self.output_spec.model_json_schema(
230229
ref_template=REF_TEMPLATE, mode="serialization"
@@ -243,45 +242,30 @@ def openapi_response(self) -> dict[str, t.Any]:
243242
}
244243

245244

246-
def flatten_field(
245+
def _flatten_field(
247246
schema: dict[str, t.Any],
248247
defs: dict[str, t.Any],
249248
max_depth: int | None = None,
250249
_depth: int = 0,
251-
def_prefix: str = "#/$defs/",
252250
) -> dict[str, t.Any]:
253251
if "allOf" in schema:
254252
schema.update(schema.pop("allOf")[0])
255253
if "anyOf" in schema:
256-
if len(schema["anyOf"]) == 2 and schema["anyOf"][1].get("type") == "null":
257-
schema.update(schema.pop("anyOf")[0])
258-
else: # we are not able to handle this, convert to a plain object
259-
schema.update(type="object")
260-
schema.pop("anyOf")
254+
schema.update(schema.pop("anyOf")[0])
261255
if max_depth is not None and _depth >= max_depth:
262256
return schema
263257
if "$ref" in schema:
264-
ref = schema.pop("$ref")[len(def_prefix) :]
265-
schema.update(
266-
flatten_field(
267-
copy.deepcopy(defs[ref]),
268-
defs,
269-
max_depth,
270-
_depth=_depth + 1,
271-
def_prefix=def_prefix,
272-
)
273-
)
258+
ref = schema.pop("$ref")[len("#/$defs/") :]
259+
schema.update(_flatten_field(defs[ref], defs, max_depth, _depth=_depth + 1))
274260
elif schema.get("type") == "object" and "properties" in schema:
275261
for k, v in schema["properties"].items():
276-
schema["properties"][k] = flatten_field(
277-
v, defs, max_depth, _depth=_depth + 1, def_prefix=def_prefix
262+
schema["properties"][k] = _flatten_field(
263+
v, defs, max_depth, _depth=_depth + 1
278264
)
279265
elif schema.get("type") == "array" and "items" in schema:
280-
schema["items"] = flatten_field(
281-
schema["items"], defs, max_depth, _depth=_depth + 1, def_prefix=def_prefix
266+
schema["items"] = _flatten_field(
267+
schema["items"], defs, max_depth, _depth=_depth + 1
282268
)
283-
elif schema.get("type") == "string" and schema.get("format") == "binary":
284-
schema["type"] = "file"
285269
return schema
286270

287271

@@ -290,4 +274,4 @@ def _flatten_model_schema(model: type[IODescriptor]) -> dict[str, t.Any]:
290274
if not schema.get("properties"):
291275
return schema
292276
defs = schema.pop("$defs", {})
293-
return flatten_field(schema, defs)
277+
return _flatten_field(schema, defs)

src/_bentoml_sdk/service/factory.py

Lines changed: 1 addition & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,8 @@
2323
from bentoml._internal.configuration.containers import BentoMLContainer
2424
from bentoml._internal.context import ServiceContext
2525
from bentoml._internal.models import Model as StoredModel
26-
from bentoml._internal.types import LazyType
2726
from bentoml._internal.utils import deprecated
2827
from bentoml._internal.utils import dict_filter_none
29-
from bentoml._internal.utils import join_path
3028
from bentoml.exceptions import BentoMLConfigException
3129
from bentoml.exceptions import BentoMLException
3230

@@ -42,8 +40,6 @@
4240
T = t.TypeVar("T", bound=object)
4341

4442
if t.TYPE_CHECKING:
45-
import fastapi as fastapi
46-
4743
from bentoml._internal import external_typing as ext
4844
from bentoml._internal.service.openapi.specification import OpenAPISpecification
4945
from bentoml._internal.utils.circus import Server
@@ -211,55 +207,11 @@ def doc(self) -> str:
211207
return get_default_svc_readme(self)
212208

213209
def schema(self) -> dict[str, t.Any]:
214-
from ..method import flatten_field
215-
216-
routes = [method.schema() for method in self.apis.values()]
217-
for app, path, _ in self.mount_apps:
218-
if not LazyType["fastapi.FastAPI"]("fastapi.FastAPI").isinstance(app):
219-
continue
220-
from fastapi.openapi.utils import get_openapi
221-
222-
openapi = get_openapi(
223-
title=app.title, version=app.version, routes=app.routes
224-
)
225-
defs = openapi.get("components", {}).get("schemas", {})
226-
for subpath, methods in openapi["paths"].items():
227-
if "post" not in methods:
228-
continue
229-
method = methods["post"]
230-
subpath = join_path(path, subpath)
231-
request_body = method.get("requestBody", {}).get("content", {})
232-
input_schema = next(iter(request_body.values())).get(
233-
"schema", {"type": "object"}
234-
)
235-
response_body = (
236-
method.get("responses", {}).get("200", {}).get("content", {})
237-
)
238-
output_schema = next(iter(response_body.values())).get(
239-
"schema", {"type": "object"}
240-
)
241-
route = dict_filter_none(
242-
{
243-
"name": subpath.strip("/").replace("/", "_"),
244-
"route": subpath,
245-
"description": method.get("summary", ""),
246-
"batchable": False,
247-
"input": flatten_field(
248-
input_schema, defs, def_prefix="#/components/schemas/"
249-
),
250-
"output": flatten_field(
251-
output_schema, defs, def_prefix="#/components/schemas/"
252-
),
253-
"is_task": False,
254-
}
255-
)
256-
routes.append(route)
257-
258210
return dict_filter_none(
259211
{
260212
"name": self.name,
261213
"type": "service",
262-
"routes": routes,
214+
"routes": [method.schema() for method in self.apis.values()],
263215
"description": getattr(self.inner, "__doc__", None),
264216
}
265217
)

src/_bentoml_sdk/service/openapi.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
from bentoml._internal.service.openapi.utils import exception_schema
2121
from bentoml._internal.types import LazyType
2222
from bentoml._internal.utils import deep_merge
23-
from bentoml._internal.utils import join_path
2423
from bentoml._internal.utils.cattr import bentoml_cattr
2524
from bentoml.exceptions import InternalServerError
2625
from bentoml.exceptions import InvalidArgument
@@ -39,6 +38,9 @@ def generate_spec(svc: Service[t.Any], *, openapi_version: str = "3.0.2"):
3938
mounted_app_paths = {}
4039
schema_components: dict[str, dict[str, Schema]] = {}
4140

41+
def join_path(prefix: str, path: str) -> str:
42+
return f"{prefix.rstrip('/')}/{path.lstrip('/')}"
43+
4244
for app, path, _ in svc.mount_apps:
4345
if LazyType["fastapi.FastAPI"]("fastapi.FastAPI").isinstance(app):
4446
from fastapi.openapi.utils import get_openapi

src/bentoml/_internal/utils/__init__.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -505,10 +505,6 @@ def wrapper(*args: P.args, **kwargs: P.kwargs) -> T:
505505
return decorator
506506

507507

508-
def join_path(prefix: str, path: str) -> str:
509-
return f"{prefix.rstrip('/')}/{path.lstrip('/')}"
510-
511-
512508
def is_jupyter() -> bool: # pragma: no cover
513509
"""Check if we're running in a Jupyter notebook."""
514510
try:

0 commit comments

Comments
 (0)