Neoteroi / BlackSheep

Fast ASGI web framework for Python
https://www.neoteroi.dev/blacksheep/
MIT License
1.88k stars 77 forks source link

OpenAPI v3 Handling Issue #492

Open mmangione opened 7 months ago

mmangione commented 7 months ago

Hello,

We have had a BlackSheep app running for over a year. When we attempted to upgrade to 2.0.7, we ran into this error. It seems to be an error in the fundamental OpenAPI class. Since this was working fine until today, I think that it must be a bug in 1.0.9.

Here is the error message:


  File "/home/mistral/llm_server_env/lib/python3.10/site-packages/blacksheep/server/openapi/v3.py", line 307, in _get_array_outer_type
    return field_info.outer_type_
AttributeError: 'FieldInfo' object has no attribute 'outer_type_'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/mistral/llm_server_env/lib/python3.10/site-packages/blacksheep/server/application.py", line 726, in _handle_lifespan
    await self.start()
  File "/home/mistral/llm_server_env/lib/python3.10/site-packages/blacksheep/server/application.py", line 715, in start
    await self.after_start.fire()
  File "/home/mistral/llm_server_env/lib/python3.10/site-packages/blacksheep/server/application.py", line 126, in fire
    await handler(self.context, *args, **kwargs)
  File "/home/mistral/llm_server_env/lib/python3.10/site-packages/blacksheep/server/openapi/common.py", line 404, in build_docs
    docs = self.generate_documentation(app)
  File "/home/mistral/llm_server_env/datascience-llm-server/app/docs/handler.py", line 34, in generate_documentation
    paths=self.get_paths(app),
  File "/home/mistral/llm_server_env/lib/python3.10/site-packages/blacksheep/server/openapi/v3.py", line 449, in get_paths
    own_paths = self.get_routes_docs(app.router, path_prefix)
  File "/home/mistral/llm_server_env/lib/python3.10/site-packages/blacksheep/server/openapi/v3.py", line 1146, in get_routes_docs
    request_body=self.get_request_body(handler),
  File "/home/mistral/llm_server_env/lib/python3.10/site-packages/blacksheep/server/openapi/v3.py", line 847, in get_request_body
    content=self._get_body_binder_content_type(body_binder, body_examples),
  File "/home/mistral/llm_server_env/lib/python3.10/site-packages/blacksheep/server/openapi/v3.py", line 821, in _get_body_binder_content_type
    return {
  File "/home/mistral/llm_server_env/lib/python3.10/site-packages/blacksheep/server/openapi/v3.py", line 823, in <dictcomp>
    schema=self.get_schema_by_type(body_binder.expected_type),
  File "/home/mistral/llm_server_env/lib/python3.10/site-packages/blacksheep/server/openapi/v3.py", line 642, in get_schema_by_type
    schema = self._get_schema_by_type(child_type, type_args)
  File "/home/mistral/llm_server_env/lib/python3.10/site-packages/blacksheep/server/openapi/v3.py", line 663, in _get_schema_by_type
    return self._get_schema_for_class(object_type)
  File "/home/mistral/llm_server_env/lib/python3.10/site-packages/blacksheep/server/openapi/v3.py", line 567, in _get_schema_for_class
    for field in self.get_fields(object_type):
  File "/home/mistral/llm_server_env/lib/python3.10/site-packages/blacksheep/server/openapi/v3.py", line 739, in get_fields
    return handler.get_type_fields(
  File "/home/mistral/llm_server_env/lib/python3.10/site-packages/blacksheep/server/openapi/v3.py", line 342, in get_type_fields
    return [
  File "/home/mistral/llm_server_env/lib/python3.10/site-packages/blacksheep/server/openapi/v3.py", line 345, in <listcomp>
    self._open_api_v2_field_schema_to_type(
  File "/home/mistral/llm_server_env/lib/python3.10/site-packages/blacksheep/server/openapi/v3.py", line 297, in _open_api_v2_field_schema_to_type
    return self._get_array_outer_type(field_info)
  File "/home/mistral/llm_server_env/lib/python3.10/site-packages/blacksheep/server/openapi/v3.py", line 311, in _get_array_outer_type
    return List[field_info.annotation.__args__[0]]
AttributeError: type object 'list' has no attribute '__args__'. Did you mean: '__add__'?```
mmangione commented 7 months ago

We patched this issue by amending L311 in blacksheep/server/openapi/v3.py to read:

return field_info.annotation

mmangione commented 7 months ago

In order to ensure compatibility, the final change in the PR is as follows:

return field_info.annotation if type(field_info.annotation) is type else List[field_info.annotation.__args__[0]]