33from datetime import datetime
44from pathlib import Path
55from tempfile import NamedTemporaryFile
6- from typing import Dict , Iterable , Tuple
6+ from typing import Any , Dict , Iterable , Tuple
77
88import httpx
99from inducoapi import build_openapi
1313 Project ,
1414 _get_project_for_url_or_path ,
1515)
16- from openapi_python_client .cli import handle_errors
1716
18- from webknossos .client import _get_generated_client
17+ from webknossos .client . context import get_generated_client
1918from webknossos .utils import snake_to_camel_case
2019
2120SCHEMA_URL = "https://converter.swagger.io/api/convert?url=https%3A%2F%2Fwebknossos.org%2Fswagger.json"
@@ -39,9 +38,10 @@ def generate_client(openapi_schema: Dict) -> None:
3938 meta = MetaType .POETRY ,
4039 config = generator_config ,
4140 )
42- assert isinstance (generator_project , Project )
43- errors = generator_project .update ()
44- # handle_errors(errors) # prints warnings
41+ assert isinstance (generator_project , Project ), generator_project .detail
42+ _errors = generator_project .update () # pylint: disable=no-member
43+ # from openapi_python_client.cli import handle_errors
44+ # handle_errors(_errors) # prints warnings
4545
4646
4747def add_api_prefix_for_non_data_paths (openapi_schema : Dict ) -> None :
@@ -68,7 +68,7 @@ def iterate_request_ids_with_responses() -> Iterable[Tuple[str, bytes]]:
6868
6969 d = datetime .utcnow ()
7070 unixtime = calendar .timegm (d .utctimetuple ())
71- client = _get_generated_client ( enforce_token = True )
71+ client = get_generated_client ( enforce_auth = True )
7272
7373 annotation_info_response = annotation_info .sync_detailed (
7474 typ = "Explorational" ,
@@ -91,9 +91,30 @@ def iterate_request_ids_with_responses() -> Iterable[Tuple[str, bytes]]:
9191 api_endpoint_name = api_endpoint .__name__ .split ("." )[- 1 ]
9292 api_endpoint_name = snake_to_camel_case (api_endpoint_name )
9393
94- response = api_endpoint .sync_detailed (client = client )
95- assert response .status_code == 200
96- yield api_endpoint_name , response .content
94+ api_endpoint_response = api_endpoint .sync_detailed (client = client )
95+ assert api_endpoint_response .status_code == 200
96+ yield api_endpoint_name , api_endpoint_response .content
97+
98+
99+ FIELDS_WITH_VARYING_CONTENT = ["adminViewConfiguration" ]
100+
101+
102+ def make_properties_required (x : Any ) -> None :
103+ if isinstance (x , dict ):
104+ for key , value in x .items ():
105+ # do not recurse into objects where the contents might be varying
106+ if key in FIELDS_WITH_VARYING_CONTENT :
107+ continue
108+ make_properties_required (value )
109+ elif isinstance (x , list ):
110+ for i in x :
111+ make_properties_required (i )
112+
113+ if isinstance (x , dict ) and "properties" in x :
114+ properties = x ["properties" ]
115+ if isinstance (properties , dict ) and len (properties ) > 0 :
116+ assert "required" not in x
117+ x ["required" ] = list (properties .keys ())
97118
98119
99120def set_response_schema_by_example (
@@ -120,16 +141,17 @@ def set_response_schema_by_example(
120141 for path_method in path .values ()
121142 if path_method ["operationId" ] == operation_id
122143 ][0 ]
144+ make_properties_required (recorded_response_schema )
123145 request_schema ["responses" ]["200" ]["content" ] = recorded_response_schema
124146
125147
126148def bootstrap_response_schemas (openapi_schema : Dict ) -> None :
127149 """Inserts the response schemas into openapi_schema (in-place),
128150 as recorded by example requests."""
129151 assert_valid_schema (openapi_schema )
130- for operation_id , response in iterate_request_ids_with_responses ():
152+ for operation_id , example_response in iterate_request_ids_with_responses ():
131153 set_response_schema_by_example (
132- openapi_schema , example_response = response , operation_id = operation_id
154+ openapi_schema , example_response = example_response , operation_id = operation_id
133155 )
134156
135157
0 commit comments