Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: move mark openapi spec to global module #65

Merged
merged 1 commit into from
Jan 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 30 additions & 29 deletions projects/fal/src/fal/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,35 +125,7 @@ def openapi(self) -> dict[str, Any]:
"""
app = self._build_app()
spec = app.openapi()
self._mark_order_openapi(spec)
return spec

def _mark_order_openapi(self, spec: dict[str, Any]):
"""
Add x-fal-order-* keys to the OpenAPI specification to help the rendering of UI.

NOTE: We rely on the fact that fastapi and Python dicts keep the order of properties.
"""

def mark_order(obj: dict[str, Any], key: str):
obj[f"x-fal-order-{key}"] = list(obj[key].keys())

mark_order(spec, "paths")

def order_schema_object(schema: dict[str, Any]):
"""
Mark the order of properties in the schema object.
They can have 'allOf', 'properties' or '$ref' key.
"""
if "allOf" in schema:
for sub_schema in schema["allOf"]:
order_schema_object(sub_schema)
if "properties" in schema:
mark_order(schema, "properties")

for key in spec["components"].get("schemas") or {}:
order_schema_object(spec["components"]["schemas"][key])

_mark_order_openapi(spec)
return spec

def teardown(self):
Expand All @@ -174,3 +146,32 @@ def marker_fn(callable: EndpointT) -> EndpointT:
return callable

return marker_fn


def _mark_order_openapi(spec: dict[str, Any]):
"""
Add x-fal-order-* keys to the OpenAPI specification to help the rendering of UI.

NOTE: We rely on the fact that fastapi and Python dicts keep the order of properties.
"""

def mark_order(obj: dict[str, Any], key: str):
obj[f"x-fal-order-{key}"] = list(obj[key].keys())

mark_order(spec, "paths")

def order_schema_object(schema: dict[str, Any]):
"""
Mark the order of properties in the schema object.
They can have 'allOf', 'properties' or '$ref' key.
"""
if "allOf" in schema:
for sub_schema in schema["allOf"]:
order_schema_object(sub_schema)
if "properties" in schema:
mark_order(schema, "properties")

for key in spec["components"].get("schemas") or {}:
order_schema_object(spec["components"]["schemas"][key])

return spec
Loading