From 39a7e3e02ee2d6045efffdd93809057d30ed4546 Mon Sep 17 00:00:00 2001 From: Danglewood <85772166+deeleeramone@users.noreply.github.com> Date: Thu, 30 Jan 2025 11:32:48 -0800 Subject: [PATCH] [BugFix] `openbb-platform-api`: Fix Unsafe Property Access In `get_data_schema_for_widget` (#7013) * fix unsafely accessing the property in get_data_schema_for_widget * make dataKey the root when no_validate=True * missed in commit --- .../platform_api/openbb_platform_api/utils/openapi.py | 8 +++++++- .../platform_api/openbb_platform_api/utils/widgets.py | 9 +++++++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/openbb_platform/extensions/platform_api/openbb_platform_api/utils/openapi.py b/openbb_platform/extensions/platform_api/openbb_platform_api/utils/openapi.py index 577dce98a64..3212d921491 100644 --- a/openbb_platform/extensions/platform_api/openbb_platform_api/utils/openapi.py +++ b/openbb_platform/extensions/platform_api/openbb_platform_api/utils/openapi.py @@ -349,7 +349,13 @@ def get_data_schema_for_widget(openapi_json, operation_id): # Get the reference to the schema from the successful response response_ref = details["responses"]["200"]["content"][ "application/json" - ]["schema"]["$ref"] + ].get("schema", {}).get("$ref") or details["responses"]["200"][ + "content" + ][ + "application/json" + ].get( + "schema", "" + ) # Extract the schema name from the reference schema_name = response_ref.split("/")[-1] # Fetch and return the schema from components diff --git a/openbb_platform/extensions/platform_api/openbb_platform_api/utils/widgets.py b/openbb_platform/extensions/platform_api/openbb_platform_api/utils/widgets.py index cb05b822825..7c4254d5de2 100644 --- a/openbb_platform/extensions/platform_api/openbb_platform_api/utils/widgets.py +++ b/openbb_platform/extensions/platform_api/openbb_platform_api/utils/widgets.py @@ -85,6 +85,9 @@ def build_json(openapi: dict, widget_exclude_filter: list): # Prepare the query schema of the widget query_schema, has_chart = get_query_schema_for_widget(openapi, route) + response_schema = route_api[method]["responses"]["200"]["content"][ + "application/json" + ].get("schema", {}) # Extract providers from the query schema providers: list = [] @@ -150,7 +153,7 @@ def build_json(openapi: dict, widget_exclude_filter: list): "endpoint": route.replace("/api", "api"), "gridData": {"w": 45, "h": 15}, "data": { - "dataKey": "results", + "dataKey": "results" if response_schema else "", "table": { "showAll": True, }, @@ -200,7 +203,9 @@ def build_json(openapi: dict, widget_exclude_filter: list): widget_config_chart["gridData"]["h"] = 20 widget_config_chart["gridData"]["w"] = 50 widget_config_chart["defaultViz"] = "chart" - widget_config_chart["data"]["dataKey"] = "chart.content" + widget_config_chart["data"]["dataKey"] = ( + "chart.content" if response_schema else "" + ) if widget_config_chart["widgetId"] not in widget_exclude_filter: widgets_json[widget_config_chart["widgetId"]] = widget_config_chart