-
Couldn't load subscription status.
- Fork 42
add paging links tests #195
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
@vincentsarago thanks for looking into this, it could very well be an interaction with this is roughly how my current setup looks prefix = "/stac"
_app = FastAPI(
root_path=prefix,
lifespan=lifespan,
)
# ... more setup
_api = StacApi(
title=title,
description=description,
settings=settings,
extensions=application_extensions,
client=CoreCrudClient(pgstac_search_model=post_request_model),
items_get_request_model=items_get_request_model,
search_get_request_model=get_request_model,
search_post_request_model=post_request_model,
collections_get_request_model=collections_get_request_model,
middlewares=middlewares,
app=_app,
**kw,
)
# AWS Lambda wrapper
handler = Mangum(
app,
api_gateway_base_path=app.root_path,
lifespan="off",
text_mime_types=text_mime_types,
)In the debugger I see that |
I wonder if this is because we don't set 🤔 |
|
@vincentsarago I'm not too familiar with FastAPI or web programming in Python in general, but my understanding is this
So I think in the test you should NOT set |
The test added in this PR test endpoint with/without root-path or prefix (2x2 matrix) if we look at https://github.com/encode/starlette/blob/a7d0b14c7378aa2e95b0b13583fe0dadace363be/tests/test_routing.py#L568-L575, it seems that we have to call |
|
@vincentsarago see #201 With that change to the test, I DO get test failure for link generation on my fork: |
| endpoint_prefix = root_path + prefix | ||
| url_prefix = "http://stac.io" + endpoint_prefix |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
see #201 , but this should also work:
| endpoint_prefix = root_path + prefix | |
| url_prefix = "http://stac.io" + endpoint_prefix | |
| endpoint_prefix = prefix | |
| url_prefix = "http://stac.io" + root_path + endpoint_prefix |
we want to model situation when root_path is dropped by the reverse proxy, but needs to be present in the output links as this is the only way to reach the API.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🤔 I'm not sure to get this to be honest
In all cases, the client should be called {root-path}/{prefix}/endpoint as shown in https://github.com/encode/starlette/blob/a7d0b14c7378aa2e95b0b13583fe0dadace363be/tests/test_routing.py#L573-L575
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🤔 I'm not sure to get this to be honest
In all cases, the client should be called
{root-path}/{prefix}/endpointas shown in https://github.com/encode/starlette/blob/a7d0b14c7378aa2e95b0b13583fe0dadace363be/tests/test_routing.py#L573-L575
I don't understand how you come to this conclusion from that one test alone. I'm just trying to replicate the scenario I see in the debugger of a real system. Omitting the root path in the call to the test client still routes to the endpoint in question, but this time with the request object behaving the same way as in the real system behind a reverse proxy that removes routing prefix.
stac_fastapi/pgstac/models/links.py
Outdated
| def url(self): | ||
| """Get the current request url.""" | ||
| return str(self.request.url) | ||
| return urljoin(str(self.request.base_url), str(self.request.url.path).lstrip("/")) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok I think we're good with this now
self.base_url would return http://0.0.0.0/{root-path}/{router-prefix}/collection while self.url.path returns {/router-prefix}/collections so using self.request.base_url gives the correct result
self.request.url will return http://0.0.0.0/{root-path}/collection because it doesn't know the router-prefix (weird configuration in stac-fastapi it self, because we don't mount the router with the prefix set 🤷)
This comment was marked as resolved.
This comment was marked as resolved.
Sorry, something went wrong.
| return str(self.request.url) | ||
| url = urljoin(str(self.request.base_url), self.request.url.path.lstrip("/")) | ||
| if qs := self.request.url.query: | ||
| url += f"?{qs}" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This feels over engineered but it's the only way I could find to make it works 🤷
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This feels like a FastAPI problem to me since self.request.url is not respecting the root_path setting, but this solution fixes the link problem!
|
I'm going to merge this PR and make a new release. I believe this fix most of the issue with root-path/prefix links. Please feel free to open new issues if you encounter more issues 🙏 |
trying to replicate issue from #194
cc @Kirill888