Skip to content
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

urllib.parse.Misimplementation of urljoin #360

Open
SBlechmann opened this issue Nov 26, 2024 · 4 comments
Open

urllib.parse.Misimplementation of urljoin #360

SBlechmann opened this issue Nov 26, 2024 · 4 comments
Labels
bug Something isn't working

Comments

@SBlechmann
Copy link
Contributor

Describe the bug
The urllib.parse.urljoin, e.g. when using orion base url and the version endpoint in the get_version() function, works a little different than originally expected.

urljoin(url1, url2)

E.g. if orion is running behind "example.org/orion" and I run the get_version, the function will end up querying "example.org/version".
This is because urljoin does not simply concatenate the URLs but cuts the first url depending on how many "/" are part of second url.

To Reproduce
see above, give the orion client a url with a path /orion

Expected behavior
Get version; instead getting nothing cause url is wrong.

@SBlechmann SBlechmann added the bug Something isn't working label Nov 26, 2024
@djs0109
Copy link
Contributor

djs0109 commented Nov 27, 2024

Hi, can you try this code snippet? Make sure you put a trailing slash / at the end of the baseurl.

from urllib.parse import urljoin
base_url = "example.org/orion/"
url = urljoin(base_url, "version")
assert url == "example.org/orion/version"

If it is still an issue, please specify your python environment more specifically.

@tstorek
Copy link
Collaborator

tstorek commented Dec 10, 2024

@djs0109 The problem persists because the ngsi_version contains a '/' leading wrong concatenation. of the url.
This should be fixed soon for the other urls if "example.org/orion/" is used. Alternatively, the version check must be adapted

@djs0109
Copy link
Contributor

djs0109 commented Dec 11, 2024

@tstorek good point. I will fix the version enumeration, i.e. remove the starting / of the versions

class NgsiURLVersion(str, Enum):
    """
    URL part that defines the NGSI version for the API.
    """

    v2_url = "v2"
    ld_url = "ngsi-ld/v1"

To reproduce

from urllib.parse import urljoin
base_url = "example.org/orion/"
version = "v2"
url_correct = urljoin(base_url, f"{version}/entities")
assert url_correct == 'example.org/orion/v2/entities'

version = "/v2"
url_wrong = urljoin(base_url, f"{version}/entities")
assert url_wrong == '/v2/entities'

@tstorek
Copy link
Collaborator

tstorek commented Dec 12, 2024

@djs0109 thanks, at the same time it would be good to validate the baserul. Maybe using pydantics AnyHttpUrl. As I understand that one will always add the trailing slash if missing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants