Giter Site home page Giter Site logo

Comments (4)

MattFisher avatar MattFisher commented on June 8, 2024 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.

Lucidiot avatar Lucidiot commented on June 8, 2024 1

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.

tomchristie avatar tomchristie commented on June 8, 2024

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.

pjxiao avatar pjxiao commented on June 8, 2024

+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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.