Expected Behaviour
When the API Gateway Swagger handler reads the local js and css files, it should always read them using a consistent encoding, regardless of how the environment has been configured.
Current Behaviour
When using python 3.12 on a windows machine, without configuring the default encoding, loading the swagger UI fails to load with a UnicodeDecodeError.
Code snippet
import uvicorn
from aws_lambda_powertools.event_handler import HttpResolverLocal
app = HttpResolverLocal(enable_validation=True)
app.enable_swagger()
if __name__ == "__main__":
uvicorn.run(app)
Possible Solution
Read the js and css files using a specified UTF-8 Encoding.
# We now inject CSS and JS into the SwaggerUI file
swagger_js = Path.open(
Path(__file__).parent / "openapi" / "swagger_ui" / "swagger-ui-bundle.min.js",
encoding="utf-8",
).read()
swagger_css = Path.open(
Path(__file__).parent / "openapi" / "swagger_ui" / "swagger-ui.min.css",
encoding="utf-8",
).read()
Steps to Reproduce
Using python 3.12, installed on a windows machine.
Create a project that uses the powertools and uvicorn to load the swagger UI locally.
import uvicorn
from aws_lambda_powertools.event_handler import HttpResolverLocal
app = HttpResolverLocal(enable_validation=True)
app.enable_swagger()
if __name__ == "__main__":
uvicorn.run(app)
Powertools for AWS Lambda (Python) version
3.24.0
AWS Lambda function runtime
3.10
Packaging format used
Lambda Layers
Debugging logs
Traceback (most recent call last):
File "C:\dev\nimbus\akyva-cloud\.venv\Lib\site-packages\uvicorn\protocols\http\h11_impl.py", line 410, in run_asgi
result = await app( # type: ignore[func-returns-value]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\dev\nimbus\akyva-cloud\.venv\Lib\site-packages\uvicorn\middleware\proxy_headers.py", line 60, in __call__
return await self.app(scope, receive, send)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\dev\nimbus\akyva-cloud\.venv\Lib\site-packages\aws_lambda_powertools\event_handler\http_resolver.py", line 449, in __call__
await self.asgi_handler(scope, receive, send)
File "C:\dev\nimbus\akyva-cloud\.venv\Lib\site-packages\aws_lambda_powertools\event_handler\http_resolver.py", line 434, in asgi_handler
response = await self._resolve_async()
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\dev\nimbus\akyva-cloud\.venv\Lib\site-packages\aws_lambda_powertools\event_handler\http_resolver.py", line 256, in _resolve_async
return await self._call_route_async(route, route_keys)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\dev\nimbus\akyva-cloud\.venv\Lib\site-packages\aws_lambda_powertools\event_handler\http_resolver.py", line 272, in _call_route_async
response = await self._run_middleware_chain_async(route)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\dev\nimbus\akyva-cloud\.venv\Lib\site-packages\aws_lambda_powertools\event_handler\http_resolver.py", line 324, in _run_middleware_chain_async
return await next_handler(self)
^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\dev\nimbus\akyva-cloud\.venv\Lib\site-packages\aws_lambda_powertools\event_handler\http_resolver.py", line 354, in wrapped
result = await future
^^^^^^^^^^^^
File "C:\dev\nimbus\akyva-cloud\.venv\Lib\site-packages\aws_lambda_powertools\event_handler\http_resolver.py", line 354, in wrapped
result = await future
^^^^^^^^^^^^
File "C:\dev\nimbus\akyva-cloud\.venv\Lib\site-packages\aws_lambda_powertools\event_handler\http_resolver.py", line 310, in final_handler
result = route.func(**route_args)
^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\dev\nimbus\akyva-cloud\.venv\Lib\site-packages\aws_lambda_powertools\event_handler\api_gateway.py", line 2640, in swagger_handler
).read()
^^^^^^
File "C:\Users\JustinBerger\AppData\Roaming\uv\python\cpython-3.12-windows-x86_64-none\Lib\encodings\cp1252.py", line 23, in decode
return codecs.charmap_decode(input,self.errors,decoding_table)[0]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
UnicodeDecodeError: 'charmap' codec can't decode byte 0x81 in position 284083: character maps to <undefined>
Expected Behaviour
When the API Gateway Swagger handler reads the local js and css files, it should always read them using a consistent encoding, regardless of how the environment has been configured.
Current Behaviour
When using python 3.12 on a windows machine, without configuring the default encoding, loading the swagger UI fails to load with a UnicodeDecodeError.
Code snippet
Possible Solution
Read the js and css files using a specified UTF-8 Encoding.
Steps to Reproduce
Using python 3.12, installed on a windows machine.
Create a project that uses the powertools and uvicorn to load the swagger UI locally.
Powertools for AWS Lambda (Python) version
3.24.0
AWS Lambda function runtime
3.10
Packaging format used
Lambda Layers
Debugging logs