Comments (4)
I just ran into this too.
According to the spec for the paths object, the path should be appended to the server object's url
The field name MUST begin with a slash. The path is appended (no relative URL resolution) to the expanded URL from the Server Object's url field in order to construct the full URL.
but OpenAPI.get_link
uses urljoin
to set the Link
's url, which strips any path
(like /api/v1
) off the url
that comes from the server object.
https://github.com/encode/apistar/blob/master/apistar/schemas/openapi.py#L465
from apistar.
The required leading slash is a requirement from OpenAPI itself (see the Paths Object specs).
Here is the workaround we used:
from urllib.parse import urlsplit
class MyClient(apistar.Client):
def __init__(self, *args, base_url=None, **kwargs):
# Let APIStar do its parsing
super().__init__(*args, **kwargs)
# Strip scheme, hostname and absolute path from all link URLs
for link_info in self.document.walk_links():
original_url = urlsplit(link_info.link.url)
new_url = ('', '', *original_url[2:])
link_info.link.url = urlunsplit(new_url).lstrip('/')
if base_url:
# Ensure the base URL ends with a slash to prevent issues:
# urljoin('http://a/b', 'c') → http://a/c
# urljoin('http://a/b/', 'c') → http://a/b/c
if not base_url.endswith('/'):
base_url += '/'
self.document.base_url = base_url
This goes through every parsed link in the Document
instance to remove the scheme, hostname and leading slash, resulting in every API endpoint having a relative URL (a/b/c
instead of http://myserver.com/a/b/c
), then updates Document.url
to a custom base URL when specified. This then allows Client.get_url to still use urljoin
and do any of its checks, but with any base URL, including custom base paths.
from apistar.
Yup - not really sure just yet.
Thanks for digging into all this so much!
I've a bit pushed for time right now so might not be reviewing apistar tickets for the next few days, but I'll get back onto it in due course.
from apistar.
+1
I tried to avoid this by stripping leading /
in paths
as follows, but this caused typesystem.base.ValidationError
.
doc['paths'] = {
path.lstrip('/'): path_item_obj
for path, path_item_obj in doc['paths'].items()
}
apistar.Client(doc)
This is because properties of Paths
object are required leading /
.
https://github.com/encode/apistar/blob/2edeb694/apistar/schemas/openapi.py#L102
from apistar.
Related Issues (20)
- Error: Missing option "--path" invoking `apistar validate` HOT 1
- KeyError on invoking `apistar validate` HOT 2
- CLI arguments are not validated correctly.
- Dependency incompatible with Python 3.5 HOT 2
- Sample API documentation
- No repr or str on some exceptions
- Schema Validation not correct for payload structure
- No validation of requestBody schemas HOT 3
- static_url kwarg to apistar.docs is not enough
- ValidationError: All object keys must be strings HOT 4
- Handle deprecated links and parameters
- no module named apistar HOT 2
- requestBody media types are not handled generically
- consistent file extension for YAML
- Fix simple typo: agnositic -> agnostic
- tests improvements HOT 1
- Unable to run .exe file
- Doing async requests HOT 3
- Can you publish a wheel?
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from apistar.