22Converting the processed OpenAPI Responses into something the CLI can work with
33"""
44
5+ from typing import Optional
6+
57from openapi3 .paths import MediaType
8+ from openapi3 .schemas import Schema
69
710from linodecli .baked .util import _aggregate_schema_properties
811
@@ -30,7 +33,13 @@ class OpenAPIResponseAttr:
3033 from it.
3134 """
3235
33- def __init__ (self , name , schema , prefix = None , nested_list_depth = 0 ):
36+ def __init__ (
37+ self ,
38+ name : str ,
39+ schema : Schema ,
40+ prefix : Optional [str ] = None ,
41+ nested_list_depth : int = 0 ,
42+ ) -> None :
3443 """
3544 :param name: The key that held this schema in the properties list, representing
3645 its name in a response.
@@ -84,10 +93,13 @@ def __init__(self, name, schema, prefix=None, nested_list_depth=0):
8493 self .item_type = schema .items .type
8594
8695 @property
87- def path (self ):
96+ def path (self ) -> str :
8897 """
8998 This is a helper for filterable fields to return the json path to this
9099 element in a response.
100+
101+ :returns: The json path to the element in a response.
102+ :rtype: str
91103 """
92104 return self .name
93105
@@ -129,6 +141,7 @@ def render_value(self, model, colorize=True):
129141 value = str (value )
130142 color = self .color_map .get (value ) or self .color_map ["default_" ]
131143 value = f"[{ color } ]{ value } [/]"
144+ # Convert None value to an empty string for better display
132145 if value is None :
133146 # Prints the word None if you don't change it
134147 value = ""
@@ -194,12 +207,14 @@ def _parse_response_model(schema, prefix=None, nested_list_depth=0):
194207 elif v .type == "object" :
195208 attrs += _parse_response_model (v , prefix = pref )
196209 elif v .type == "array" and v .items .type == "object" :
210+ # Parse arrays for objects recursively and increase the nesting depth
197211 attrs += _parse_response_model (
198212 v .items ,
199213 prefix = pref ,
200214 nested_list_depth = nested_list_depth + 1 ,
201215 )
202216 else :
217+ # Handle any other simple types
203218 attrs .append (
204219 OpenAPIResponseAttr (
205220 k , v , prefix = prefix , nested_list_depth = nested_list_depth
@@ -215,7 +230,7 @@ class OpenAPIResponse:
215230 responses section of an OpenAPI Operation
216231 """
217232
218- def __init__ (self , response : MediaType ):
233+ def __init__ (self , response : MediaType ) -> None :
219234 """
220235 :param response: The response's MediaType object in the OpenAPI spec,
221236 corresponding to the application/json response type
@@ -287,15 +302,22 @@ def _fix_nested_list(self, json):
287302
288303 nested_lists = [c .strip () for c in self .nested_list .split ("," )]
289304 result = []
305+
290306 for nested_list in nested_lists :
291307 path_parts = nested_list .split ("." )
308+
292309 if not isinstance (json , list ):
293310 json = [json ]
311+
294312 for cur in json :
313+ # Get the nested list using the path
295314 nlist_path = cur
296315 for p in path_parts :
297316 nlist_path = nlist_path .get (p )
298317 nlist = nlist_path
318+
319+ # For each item in the nested list,
320+ # combine the parent properties with the nested item
299321 for item in nlist :
300322 cobj = {k : v for k , v in cur .items () if k != path_parts [0 ]}
301323 cobj ["_split" ] = path_parts [- 1 ]
0 commit comments