From 4f410eb8756e253cf224eb4ad8feb533ad2273c1 Mon Sep 17 00:00:00 2001 From: Vincent Sarago Date: Thu, 27 Jun 2024 17:59:40 +0200 Subject: [PATCH 1/3] remove middleware stack building to avoid conflict with exception handler (#721) * remove middleware stack building to avoid conflict with exception handler * update changelog --- CHANGES.md | 1 + stac_fastapi/api/stac_fastapi/api/app.py | 7 +----- stac_fastapi/api/tests/test_middleware.py | 30 +++++++++++++++++++++++ 3 files changed, 32 insertions(+), 6 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index ea0209926..011a7fde7 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -9,6 +9,7 @@ ### Removed * Removed the Filter Extension depenency from `AggregationExtensionPostRequest` and `AggregationExtensionGetRequest` [#716](https://github.com/stac-utils/stac-fastapi/pull/716) +* Removed `add_middleware` method in `StacApi` object and let starlette handle the middleware stack creation [721](https://github.com/stac-utils/stac-fastapi/pull/721) ## [3.0.0a3] - 2024-06-13 diff --git a/stac_fastapi/api/stac_fastapi/api/app.py b/stac_fastapi/api/stac_fastapi/api/app.py index 5fe7f9d08..b4f5125f0 100644 --- a/stac_fastapi/api/stac_fastapi/api/app.py +++ b/stac_fastapi/api/stac_fastapi/api/app.py @@ -439,11 +439,6 @@ def add_route_dependencies( """ return add_route_dependencies(self.app.router.routes, scopes, dependencies) - def add_middleware(self, middleware: Middleware): - """Add a middleware class to the application.""" - self.app.user_middleware.insert(0, middleware) - self.app.middleware_stack = self.app.build_middleware_stack() - def __attrs_post_init__(self): """Post-init hook. @@ -484,7 +479,7 @@ def __attrs_post_init__(self): # add middlewares for middleware in self.middlewares: - self.add_middleware(middleware) + self.app.user_middleware.insert(0, middleware) # customize route dependencies for scopes, dependencies in self.route_dependencies: diff --git a/stac_fastapi/api/tests/test_middleware.py b/stac_fastapi/api/tests/test_middleware.py index 041dc410c..00e7f8038 100644 --- a/stac_fastapi/api/tests/test_middleware.py +++ b/stac_fastapi/api/tests/test_middleware.py @@ -1,6 +1,8 @@ from unittest import mock import pytest +from fastapi import Request +from fastapi.responses import JSONResponse from starlette.applications import Starlette from starlette.testclient import TestClient @@ -166,3 +168,31 @@ def test_cors_middleware(test_client): resp = test_client.get("/_mgmt/ping", headers={"Origin": "http://netloc"}) assert resp.status_code == 200 assert resp.headers["access-control-allow-origin"] == "*" + + +def test_middleware_stack(): + stac_api = StacApi( + settings=ApiSettings(), client=mock.create_autospec(BaseCoreClient) + ) + + def exception_handler(request: Request, exc: Exception) -> JSONResponse: + return JSONResponse( + status_code=400, + content={"customerrordetail": "yoo", "body": "yo"}, + ) + + class CustomException(Exception): + "Custom Exception" + + pass + + stac_api.app.add_exception_handler(CustomException, exception_handler) + + @stac_api.app.get("/error") + def error_endpoint(): + raise CustomException("got you!") + + with TestClient(stac_api.app) as client: + resp = client.get("/error") + assert resp.status_code == 400 + assert resp.json()["customerrordetail"] == "yoo" From 63097135d72d1afe8731b6138af1974aace477af Mon Sep 17 00:00:00 2001 From: Vincent Sarago Date: Thu, 27 Jun 2024 22:19:43 +0200 Subject: [PATCH 2/3] raise RuntimeError if middleware stack has already been created when initialiazing StacApi (#722) --- stac_fastapi/api/stac_fastapi/api/app.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/stac_fastapi/api/stac_fastapi/api/app.py b/stac_fastapi/api/stac_fastapi/api/app.py index b4f5125f0..44a55764f 100644 --- a/stac_fastapi/api/stac_fastapi/api/app.py +++ b/stac_fastapi/api/stac_fastapi/api/app.py @@ -478,6 +478,9 @@ def __attrs_post_init__(self): self.app.openapi = self.customize_openapi # add middlewares + if self.middlewares and self.app.middleware_stack is not None: + raise RuntimeError("Cannot add middleware after an application has started") + for middleware in self.middlewares: self.app.user_middleware.insert(0, middleware) From 1916d44397733ff09c247c1578e113ef3f7a2501 Mon Sep 17 00:00:00 2001 From: Vincent Sarago Date: Fri, 28 Jun 2024 17:55:13 +0200 Subject: [PATCH 3/3] Release/v3.0.0a4 (#723) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * update changelog * Bump version: 3.0.0a3 → 3.0.0a4 --------- Co-authored-by: Jonathan Healy --- CHANGES.md | 5 ++++- VERSION | 2 +- pyproject.toml | 2 +- stac_fastapi/api/stac_fastapi/api/version.py | 2 +- stac_fastapi/extensions/stac_fastapi/extensions/version.py | 2 +- stac_fastapi/types/stac_fastapi/types/version.py | 2 +- 6 files changed, 9 insertions(+), 6 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 011a7fde7..2e32b5814 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -2,6 +2,8 @@ ## [Unreleased] - TBD +## [3.0.0a4] - 2024-06-27 + ### Fixed * Updated default filter language in filter extension's POST search request model to match the extension's documentation [#711](https://github.com/stac-utils/stac-fastapi/issues/711) @@ -407,7 +409,8 @@ * First PyPi release! -[Unreleased]: +[Unreleased]: +[3.0.0a4]: [3.0.0a3]: [3.0.0a2]: [3.0.0a1]: diff --git a/VERSION b/VERSION index 4f22bc78a..255dd065c 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -3.0.0a3 +3.0.0a4 diff --git a/pyproject.toml b/pyproject.toml index b56750993..fbe51fbf2 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -24,7 +24,7 @@ section-order = ["future", "standard-library", "third-party", "first-party", "lo quote-style = "double" [tool.bumpversion] -current_version = "3.0.0a3" +current_version = "3.0.0a4" parse = """(?x) (?P\\d+)\\. (?P\\d+)\\. diff --git a/stac_fastapi/api/stac_fastapi/api/version.py b/stac_fastapi/api/stac_fastapi/api/version.py index 0ead30261..f182485f7 100644 --- a/stac_fastapi/api/stac_fastapi/api/version.py +++ b/stac_fastapi/api/stac_fastapi/api/version.py @@ -1,2 +1,2 @@ """Library version.""" -__version__ = "3.0.0a3" +__version__ = "3.0.0a4" diff --git a/stac_fastapi/extensions/stac_fastapi/extensions/version.py b/stac_fastapi/extensions/stac_fastapi/extensions/version.py index 0ead30261..f182485f7 100644 --- a/stac_fastapi/extensions/stac_fastapi/extensions/version.py +++ b/stac_fastapi/extensions/stac_fastapi/extensions/version.py @@ -1,2 +1,2 @@ """Library version.""" -__version__ = "3.0.0a3" +__version__ = "3.0.0a4" diff --git a/stac_fastapi/types/stac_fastapi/types/version.py b/stac_fastapi/types/stac_fastapi/types/version.py index 0ead30261..f182485f7 100644 --- a/stac_fastapi/types/stac_fastapi/types/version.py +++ b/stac_fastapi/types/stac_fastapi/types/version.py @@ -1,2 +1,2 @@ """Library version.""" -__version__ = "3.0.0a3" +__version__ = "3.0.0a4"