Skip to content

Commit

Permalink
fix: improve OpenAPI schema validation and type safety
Browse files Browse the repository at this point in the history
Co-Authored-By: Chaoyu Yang <paranoyang@gmail.com>
  • Loading branch information
devin-ai-integration[bot] and parano committed Jan 10, 2025
1 parent 5667f6c commit 47e39d4
Showing 1 changed file with 26 additions and 11 deletions.
37 changes: 26 additions & 11 deletions src/_bentoml_sdk/service/openapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@
merger = Merger(
# merge dicts recursively
[(dict, "merge")],
# merge lists by concatenating
["append"],
# override other types
# override all other types (including lists)
["override"],
# override conflicting types
["override"],
)

Expand Down Expand Up @@ -212,22 +212,37 @@ def _get_api_routes(svc: Service[t.Any]) -> dict[str, PathItem]:
raise TypeError(
f"Content for {content_type} must be a dictionary"
)
if "schema" in content:
schema = content["schema"]
if not isinstance(schema, dict):
raise TypeError("Schema must be a dictionary")
if "type" in schema and schema["type"] not in {
try:
content_dict: dict[str, t.Any] = {
"schema": content.get("schema"),
"example": content.get("example"),
"examples": content.get("examples"),
"encoding": content.get("encoding"),
}
content_obj = MediaType(**content_dict)
except (TypeError, ValueError) as e:
raise TypeError(f"Invalid content format: {e}")

if content_obj.schema is not None:
schema = content_obj.schema
if isinstance(schema, Reference):
continue
valid_types = {
"object",
"array",
"string",
"number",
"integer",
"boolean",
"null",
}:
}
if (
schema.type is not None
and schema.type not in valid_types
):
raise ValueError(
f"Invalid schema type: {schema['type']}. "
"Must be one of: object, array, string, number, integer, boolean, null"
f"Invalid schema type: {schema.type}. "
f"Must be one of: {', '.join(sorted(valid_types))}"
)
elif field == "tags":
if not isinstance(value, list):
Expand Down

0 comments on commit 47e39d4

Please sign in to comment.