@@ -31,13 +31,21 @@ def get_dummy_value_for_schema(schema_obj) -> cst.BaseExpression:
3131 elif ann .startswith ("dict" ):
3232 return cst .Dict (elements = [])
3333 else :
34+ # Fallback to a string instead of None so urllib3 doesn't crash on encode_multipart_formdata
35+ t = getattr (schema_obj , "type" , None ) if schema_obj else None
36+ if t == "file" :
37+ return cst .SimpleString ('b"dummy_content"' )
3438 return cst .Name ("None" )
3539
3640
37- def get_stub_body (schema_obj ):
41+ def get_stub_body (schema_obj , spec = None ):
3842 """
3943 Generate a stub body AST node based on an OpenAPI schema object.
4044 """
45+ if schema_obj and getattr (schema_obj , "ref" , None ) and spec and spec .components and spec .components .schemas :
46+ ref_name = schema_obj .ref .split ('/' )[- 1 ]
47+ schema_obj = spec .components .schemas .get (ref_name , schema_obj )
48+
4149 if schema_obj and getattr (schema_obj , "properties" , None ):
4250 elements = []
4351 for prop_name , prop_schema in schema_obj .properties .items ():
@@ -48,7 +56,10 @@ def get_stub_body(schema_obj):
4856 elements = [cst .Element (cst .SimpleString ('"http://dummy"' ))]
4957 )
5058 else :
51- val = get_dummy_value_for_schema (prop_schema )
59+ if getattr (prop_schema , "ref" , None ) or getattr (prop_schema , "type" , None ) in ("object" , "array" ):
60+ val = get_stub_body (prop_schema , spec )
61+ else :
62+ val = get_dummy_value_for_schema (prop_schema )
5263 elements .append (
5364 cst .DictElement (
5465 key = cst .SimpleString (f'"{ prop_name } "' ), value = val
@@ -60,7 +71,7 @@ def get_stub_body(schema_obj):
6071 if items_schema and getattr (items_schema , "type" , None ) in ("string" , "integer" , "number" , "boolean" ):
6172 item_val = get_dummy_value_for_schema (items_schema )
6273 else :
63- item_val = get_stub_body (items_schema )
74+ item_val = get_stub_body (items_schema , spec )
6475 return cst .List (
6576 elements = [
6677 cst .Element (item_val )
@@ -70,7 +81,7 @@ def get_stub_body(schema_obj):
7081 return cst .Dict (elements = [])
7182
7283def emit_operation_test (
73- method : str , path : str , operation : Operation , composable : bool = False
84+ method : str , path : str , operation : Operation , composable : bool = False , spec = None
7485) -> cst .FunctionDef :
7586 """
7687 Emit a pytest unit test for an API operation.
@@ -96,11 +107,13 @@ def emit_operation_test(
96107 schema_obj = getattr (
97108 param , "schema_" , getattr (param , "schema" , None )
98109 )
99- val = get_stub_body (schema_obj )
110+ val = get_stub_body (schema_obj , spec )
100111 else :
101112 schema_obj = getattr (
102113 param , "schema_" , getattr (param , "schema" , None )
103114 )
115+ if not schema_obj and getattr (param , "type" , None ):
116+ schema_obj = param
104117 val = get_dummy_value_for_schema (schema_obj )
105118 args .append (cst .Arg (keyword = cst .Name (param_name ), value = val ))
106119
@@ -129,7 +142,7 @@ def emit_operation_test(
129142 getattr (content [first_key ], "schema" , None ),
130143 )
131144
132- val = get_stub_body (schema_obj )
145+ val = get_stub_body (schema_obj , spec )
133146 except Exception :
134147 val = cst .Dict (elements = [])
135148
@@ -463,7 +476,7 @@ def emit_tests(spec: OpenAPI, composable: bool = False) -> cst.Module:
463476 if operation :
464477 body .append (
465478 emit_operation_test (
466- method , path , operation , composable = composable
479+ method , path , operation , composable = composable , spec = spec
467480 )
468481 )
469482 body .append (cst .EmptyLine ())
0 commit comments