Skip to content

Conversation

@erlendvollset
Copy link
Collaborator

@erlendvollset erlendvollset commented Jan 22, 2026

The current implementation treats the base_path as a relative URL reference and resolves it against the base URL, so it ignores any path segments already set in base URL. We have users which use a gateway in front of our API and have a setup where the base url contains a path segment

Description

Please describe the change you have made.

Checklist:

  • Tests added/updated.
  • Documentation updated. Documentation is generated from docstrings - these must be updated according to your change.
    If a new method has been added it should be referenced in cognite.rst in order to generate docs based on its docstring.
  • The PR title follows the Conventional Commit spec.

@erlendvollset erlendvollset requested review from a team as code owners January 22, 2026 12:46
@gemini-code-assist
Copy link
Contributor

Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

@erlendvollset erlendvollset force-pushed the fix-get_base_url_with_base_path branch from 5d98e14 to 77743ac Compare January 22, 2026 12:53
@erlendvollset erlendvollset changed the title Concatenate paths in _get_base_url_with_base_path rather than use urljoin fix: Concatenate paths in _get_base_url_with_base_path rather than use urljoin Jan 22, 2026
…join

The current implementation treats the base_path as a relative URL reference and resolves it against the base URL, so it ignores any path segments already set in base_url. We have customers which use a gateway in front of our API and have a setup where the base url contains a path segment
@erlendvollset erlendvollset force-pushed the fix-get_base_url_with_base_path branch from 77743ac to 3f362e7 Compare January 22, 2026 12:57
@codecov
Copy link

codecov bot commented Jan 22, 2026

Codecov Report

❌ Patch coverage is 41.66667% with 7 lines in your changes missing coverage. Please review.
✅ Project coverage is 48.84%. Comparing base (197a140) to head (39b0b60).
⚠️ Report is 1 commits behind head on master.

Files with missing lines Patch % Lines
cognite/client/_org_client.py 0.00% 3 Missing ⚠️
cognite/client/_api/files.py 33.33% 2 Missing ⚠️
cognite/client/utils/_auxiliary.py 60.00% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #2448      +/-   ##
==========================================
- Coverage   48.85%   48.84%   -0.01%     
==========================================
  Files         191      191              
  Lines       26149    26152       +3     
==========================================
- Hits        12776    12775       -1     
- Misses      13373    13377       +4     
Files with missing lines Coverage Δ
cognite/client/_api_client.py 75.46% <100.00%> (+0.12%) ⬆️
cognite/client/_api/files.py 64.51% <33.33%> (ø)
cognite/client/utils/_auxiliary.py 61.63% <60.00%> (+0.34%) ⬆️
cognite/client/_org_client.py 0.00% <0.00%> (ø)

... and 4 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@haakonvt
Copy link
Contributor

/gemini review

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request effectively addresses a critical bug where urljoin was incorrectly handling base URLs that already contained path segments. The change to manually concatenate the base URL and base path using f-strings is a robust and correct solution. The addition of a dedicated unit test (test_client_with_base_url_including_path_segments) is highly commendable, ensuring the fix works as expected and preventing future regressions. The code adheres to the repository's style guide, particularly regarding testing and readability.

@haakonvt
Copy link
Contributor

haakonvt commented Jan 22, 2026

Double-check if you need to also update:
https://github.com/cognitedata/cognite-sdk-python/blob/master/cognite/client/_org_client.py

Edit: That uses a hardcoded auth-url... hmm... that prob doesn't work for these customers I guess

@haakonvt
Copy link
Contributor

I'm on the fence about replacing a library function (like urljoin) that understand urls with all their bells and whistles, with a pure "string manipulation" function. How about we do something like this instead to only modify the path, without touching other components?

from urllib.parse import urlparse, urlunparse

def append_url_path(base_url: str, path: str) -> str:
    parsed = urlparse(base_url)
    new_path = f"{parsed.path.rstrip('/')}/{path.lstrip('/')}"
    return urlunparse(parsed._replace(path=new_path))

@haakonvt
Copy link
Contributor

I'm on the fence about replacing a library function (like urljoin) that understand urls with all their bells and whistles, with a pure "string manipulation" function. How about we do something like this instead to only modify the path, without touching other components?

from urllib.parse import urlparse, urlunparse

def append_url_path(base_url: str, path: str) -> str:
    parsed = urlparse(base_url)
    new_path = f"{parsed.path.rstrip('/')}/{path.lstrip('/')}"
    return urlunparse(parsed._replace(path=new_path))

@gemini-code-assist is this overly cautious?

@gemini-code-assist
Copy link
Contributor

That's an excellent point, @haakonvt! Your proposed append_url_path function is not overly cautious at all; in fact, it's a more robust and correct approach for manipulating URL paths, especially when the base URL might already contain path segments, query parameters, or fragments.

The urljoin function, as noted in the PR description, can be tricky because of its behavior with relative URLs, often discarding existing path segments in the base URL if the second argument is an absolute path. The concatenate_url_segments function introduced in this PR addresses the immediate issue of urljoin's path handling by simply joining strings, but it doesn't fully leverage the URL parsing capabilities of urllib.parse.

Your append_url_path function, by explicitly parsing the URL, modifying only the path component, and then reconstructing the URL, ensures that other parts of the URL (like the scheme, netloc, query, and fragment) are correctly preserved. This is crucial for maintaining the integrity of the URL and preventing unexpected behavior in more complex URL structures.

I recommend adopting your append_url_path approach as it provides a more comprehensive and reliable solution for URL path concatenation.

@erlendvollset
Copy link
Collaborator Author

🦄

@erlendvollset erlendvollset added this pull request to the merge queue Jan 23, 2026
Merged via the queue into master with commit 88c64f5 Jan 23, 2026
29 of 30 checks passed
@erlendvollset erlendvollset deleted the fix-get_base_url_with_base_path branch January 23, 2026 14:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants