Skip to content

Commit d695939

Browse files
authored
TPT-4280: linode-cli: Adjust parser for the OpenAPI changes (#888)
1 parent 13ddc88 commit d695939

1 file changed

Lines changed: 48 additions & 0 deletions

File tree

linodecli/cli.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,8 +305,56 @@ def _load_openapi_spec(spec_location: str) -> OpenAPI:
305305
with CLI._get_spec_file_reader(spec_location) as f:
306306
parsed = CLI._parse_spec_file(f)
307307

308+
CLI._normalize_content_parameters(parsed)
309+
308310
return OpenAPI(parsed)
309311

312+
@staticmethod
313+
def _normalize_content_parameters(parsed: Dict[str, Any]):
314+
"""
315+
The openapi3 library does not support the OpenAPI 3.0 ``content``
316+
form for Parameter objects. This method converts any such
317+
parameters (in components and inline on paths/operations) to use
318+
a top-level ``schema`` field so they can be parsed normally.
319+
320+
:param parsed: The raw spec dict to mutate in-place.
321+
"""
322+
323+
def _fix_param(param):
324+
if not isinstance(param, dict):
325+
return
326+
if "content" in param and "schema" not in param:
327+
content = param.pop("content")
328+
for media_obj in content.values():
329+
if isinstance(media_obj, dict) and "schema" in media_obj:
330+
param["schema"] = media_obj["schema"]
331+
break
332+
333+
for param in (
334+
parsed.get("components", {}).get("parameters", {}).values()
335+
):
336+
_fix_param(param)
337+
338+
for path_item in parsed.get("paths", {}).values():
339+
if not isinstance(path_item, dict):
340+
continue
341+
for p in path_item.get("parameters", []):
342+
_fix_param(p)
343+
for method in (
344+
"get",
345+
"put",
346+
"post",
347+
"delete",
348+
"options",
349+
"head",
350+
"patch",
351+
"trace",
352+
):
353+
operation = path_item.get(method)
354+
if isinstance(operation, dict):
355+
for p in operation.get("parameters", []):
356+
_fix_param(p)
357+
310358
@staticmethod
311359
@contextlib.contextmanager
312360
def _get_spec_file_reader(

0 commit comments

Comments
 (0)