-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
openapi_parser.py
214 lines (176 loc) · 8.73 KB
/
openapi_parser.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# Copyright (c) Microsoft. All rights reserved.
import logging
from collections import OrderedDict
from collections.abc import Generator
from typing import TYPE_CHECKING, Any, Final
from urllib.parse import urlparse
from prance import ResolvingParser
from semantic_kernel.connectors.openapi_plugin.models.rest_api_operation import RestApiOperation
from semantic_kernel.connectors.openapi_plugin.models.rest_api_operation_expected_response import (
RestApiOperationExpectedResponse,
)
from semantic_kernel.connectors.openapi_plugin.models.rest_api_operation_parameter import RestApiOperationParameter
from semantic_kernel.connectors.openapi_plugin.models.rest_api_operation_parameter_location import (
RestApiOperationParameterLocation,
)
from semantic_kernel.connectors.openapi_plugin.models.rest_api_operation_payload import RestApiOperationPayload
from semantic_kernel.connectors.openapi_plugin.models.rest_api_operation_payload_property import (
RestApiOperationPayloadProperty,
)
from semantic_kernel.exceptions.function_exceptions import PluginInitializationError
from semantic_kernel.utils.experimental_decorator import experimental_class
if TYPE_CHECKING:
from semantic_kernel.connectors.openai_plugin.openai_function_execution_parameters import (
OpenAIFunctionExecutionParameters,
)
from semantic_kernel.connectors.openapi_plugin.openapi_function_execution_parameters import (
OpenAPIFunctionExecutionParameters,
)
logger: logging.Logger = logging.getLogger(__name__)
@experimental_class
class OpenApiParser:
"""NOTE: SK Python only supports the OpenAPI Spec >=3.0.
Import an OpenAPI file.
Args:
openapi_file: The path to the OpenAPI file which can be local or a URL.
Returns:
The parsed OpenAPI file
:param openapi_file: The path to the OpenAPI file which can be local or a URL.
:return: The parsed OpenAPI file
"""
PAYLOAD_PROPERTIES_HIERARCHY_MAX_DEPTH: int = 10
SUPPORTED_MEDIA_TYPES: Final[list[str]] = ["application/json", "text/plain"]
def parse(self, openapi_document: str) -> Any | dict[str, Any] | None:
"""Parse the OpenAPI document."""
parser = ResolvingParser(openapi_document)
return parser.specification
def _parse_parameters(self, parameters: list[dict[str, Any]]):
"""Parse the parameters from the OpenAPI document."""
result: list[RestApiOperationParameter] = []
for param in parameters:
name: str = param["name"]
if not param.get("in"):
raise PluginInitializationError(f"Parameter {name} is missing 'in' field")
if param.get("content", None) is not None:
# The schema and content fields are mutually exclusive.
raise PluginInitializationError(f"Parameter {name} cannot have a 'content' field. Expected: schema.")
location = RestApiOperationParameterLocation(param["in"])
description: str = param.get("description", None)
is_required: bool = param.get("required", False)
default_value = param.get("default", None)
schema: dict[str, Any] | None = param.get("schema", None)
result.append(
RestApiOperationParameter(
name=name,
type=schema.get("type", "string") if schema else "string",
location=location,
description=description,
is_required=is_required,
default_value=default_value,
schema=schema if schema else {"type": "string"},
)
)
return result
def _get_payload_properties(self, operation_id, schema, required_properties, level=0):
if schema is None:
return []
if level > OpenApiParser.PAYLOAD_PROPERTIES_HIERARCHY_MAX_DEPTH:
raise Exception(
f"Max level {OpenApiParser.PAYLOAD_PROPERTIES_HIERARCHY_MAX_DEPTH} of "
f"traversing payload properties of `{operation_id}` operation is exceeded."
)
result = []
for property_name, property_schema in schema.get("properties", {}).items():
default_value = property_schema.get("default", None)
property = RestApiOperationPayloadProperty(
name=property_name,
type=property_schema.get("type", None),
is_required=property_name in required_properties,
properties=self._get_payload_properties(operation_id, property_schema, required_properties, level + 1),
description=property_schema.get("description", None),
schema=property_schema,
default_value=default_value,
)
result.append(property)
return result
def _create_rest_api_operation_payload(
self, operation_id: str, request_body: dict[str, Any]
) -> RestApiOperationPayload | None:
if request_body is None or request_body.get("content") is None:
return None
content = request_body.get("content")
if content is None:
return None
media_type = next((mt for mt in OpenApiParser.SUPPORTED_MEDIA_TYPES if mt in content), None)
if media_type is None:
raise Exception(f"Neither of the media types of {operation_id} is supported.")
media_type_metadata = content[media_type]
payload_properties = self._get_payload_properties(
operation_id, media_type_metadata["schema"], media_type_metadata["schema"].get("required", set())
)
return RestApiOperationPayload(
media_type,
payload_properties,
request_body.get("description"),
schema=media_type_metadata.get("schema", None),
)
def _create_response(
self, responses: dict[str, Any]
) -> Generator[tuple[str, RestApiOperationExpectedResponse], None, None]:
for response_key, response_value in responses.items():
media_type = next(
(mt for mt in OpenApiParser.SUPPORTED_MEDIA_TYPES if mt in response_value.get("content", {})), None
)
if media_type is not None:
matching_schema = response_value["content"][media_type].get("schema", {})
description = response_value.get("description") or matching_schema.get("description", "")
yield (
response_key,
RestApiOperationExpectedResponse(
description=description,
media_type=media_type,
schema=matching_schema if matching_schema else None,
),
)
def create_rest_api_operations(
self,
parsed_document: Any,
execution_settings: "OpenAIFunctionExecutionParameters | OpenAPIFunctionExecutionParameters | None" = None,
) -> dict[str, RestApiOperation]:
"""Create the REST API Operations from the parsed OpenAPI document.
Args:
parsed_document: The parsed OpenAPI document
execution_settings: The execution settings
Returns:
A dictionary of RestApiOperation objects keyed by operationId
"""
paths = parsed_document.get("paths", {})
request_objects = {}
base_url = "/"
servers = parsed_document.get("servers", [])
base_url = servers[0].get("url") if servers else "/"
if execution_settings and execution_settings.server_url_override:
base_url = execution_settings.server_url_override
for path, methods in paths.items():
for method, details in methods.items():
request_method = method.lower()
parameters = details.get("parameters", [])
operationId = details.get("operationId", path + "_" + request_method)
summary = details.get("summary", None)
description = details.get("description", None)
parsed_params = self._parse_parameters(parameters)
request_body = self._create_rest_api_operation_payload(operationId, details.get("requestBody", None))
responses = dict(self._create_response(details.get("responses", {})))
rest_api_operation = RestApiOperation(
id=operationId,
method=request_method,
server_url=urlparse(base_url),
path=path,
params=parsed_params,
request_body=request_body,
summary=summary,
description=description,
responses=OrderedDict(responses),
)
request_objects[operationId] = rest_api_operation
return request_objects