diff --git a/src/titiler/core/titiler/core/utils.py b/src/titiler/core/titiler/core/utils.py index 0f7310ef8..4ace640a3 100644 --- a/src/titiler/core/titiler/core/utils.py +++ b/src/titiler/core/titiler/core/utils.py @@ -312,10 +312,18 @@ def update_openapi(app: FastAPI) -> FastAPI: OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """ - # Find the route for the openapi_url in the app - openapi_route: Route = next( - route for route in app.router.routes if route.path == app.openapi_url - ) + # Use a for-loop to traverse and locate the openapi route, + # which avoids creating a potentially expensive generator. + openapi_url = app.openapi_url + for route in app.router.routes: + if route.path == openapi_url: + openapi_route: Route = route + break + else: + # Defensive: This is not in original logic; keep behavior identical by using next(...) + # if not found, next(...) would raise StopIteration, so we can raise the same for clarity. + raise StopIteration + # Store the old endpoint function so we can call it from the patched function old_endpoint = openapi_route.endpoint @@ -335,7 +343,6 @@ async def patched_openapi_endpoint(req: Request) -> Response: # our patched function and replace the existing app with it. openapi_route.app = request_response(patched_openapi_endpoint) - # return the patched app return app