Skip to content

fix(http): use locale-independent header name comparator - #817

Closed
arimu1 wants to merge 1 commit into
openai:mainfrom
arimu1:fix/755-headers-turkish-locale
Closed

arimu1 wants to merge 1 commit into
openai:mainfrom
arimu1:fix/755-headers-turkish-locale

Conversation

@arimu1

@arimu1 arimu1 commented Jul 25, 2026

Copy link
Copy Markdown

Summary

Fixes #755.

Under locales such as Turkish (tr_TR), case conversion of I/i is not ASCII (I → ı, i → İ). Headers used TreeMap(String.CASE_INSENSITIVE_ORDER) for case-insensitive header names; this change pins comparison to Locale.ROOT so HTTP header names (e.g. OpenAI-Organization, OpenAI-Project) resolve consistently regardless of the JVM default locale.

This is the Java equivalent of the Node SDK Turkish-locale header normalization fix (openai/openai-node#1928 / #1941).

Change

In Headers.kt, both the builder map and the built immutable map now use:

Comparator { a, b ->
    a.lowercase(Locale.ROOT).compareTo(b.lowercase(Locale.ROOT))
}

instead of String.CASE_INSENSITIVE_ORDER.

Test plan

  • Added caseInsensitiveUnderTurkishLocale that sets Locale("tr", "TR"), puts OpenAI-Organization / OpenAI-Project, asserts case-insensitive get for mixed/upper/lower ASCII keys, restores locale in finally, and uses @ResourceLock(Resources.LOCALE) for parallel tests.
  • Ran: ./gradlew :openai-java-core:test --tests "com.openai.core.http.HeadersTest" → BUILD SUCCESSFUL

Notes

Hand-written core path only (Headers.kt + test). No generated API model changes.

AI-assisted implementation; human-reviewed before open.

TreeMap with String.CASE_INSENSITIVE_ORDER is not safe under locales like
Turkish where I/i case conversion is not ASCII. HTTP header names must
compare with Locale.ROOT rules so keys such as OpenAI-Organization resolve
reliably.

Fixes openai#755
@arimu1
arimu1 requested a review from a team as a code owner July 25, 2026 15:51

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 104ffc3a52

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

fun caseInsensitiveUnderTurkishLocale() {
val previous = Locale.getDefault()
try {
Locale.setDefault(Locale("tr", "TR"))

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Isolate the locale-changing test from parallel tests

Under this repo's openai.kotlin test configuration, JUnit runs tests concurrently (buildSrc/src/main/kotlin/openai.kotlin.gradle.kts lines 33-35), but this changes the JVM-wide default locale while unrelated tests may still be executing. @ResourceLock(Resources.LOCALE) only serializes tests that also take that lock, so concurrent tests that construct ClientOptions/OpenAIOkHttpClient can call LogLevel.fromEnv() under Turkish locale; when OPENAI_LOG is set to values like INFO, default-locale lowercasing turns it into ınfo and the SDK silently uses OFF. Please make this test truly isolated from the rest of the parallel suite, or avoid mutating the process default locale.

Useful? React with 👍 / 👎.

@arimu1

arimu1 commented Jul 25, 2026

Copy link
Copy Markdown
Author

Closing after re-verification: this PR does not fix a real defect in String.CASE_INSENSITIVE_ORDER.

Under a Turkish default locale (tr_TR), a small standalone check still shows:

  • String.CASE_INSENSITIVE_ORDER.compare("I", "i") == 0
  • TreeMap(String.CASE_INSENSITIVE_ORDER) resolves OpenAI-Organization / openai-organization correctly

OpenJDK implements that comparator with Unicode Character.toUpperCase / Character.toLowerCase, not String.toLowerCase() / locale-sensitive folding. The classic Turkish I → ı problem applies to String.toLowerCase() without an explicit locale, not to CASE_INSENSITIVE_ORDER.

The unit test added here would also pass on main without the production change, so it is not a regression test for #755.

Sorry for the noise — the Node SDK locale issue is a different API surface. Leaving #755 for maintainers to close or re-scope if they want defensive hardening anyway.

@arimu1

arimu1 commented Jul 25, 2026

Copy link
Copy Markdown
Author

Closing as non-fix: CASE_INSENSITIVE_ORDER is already locale-independent (verified under tr_TR).

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.

Bug: Turkish Locale - Potential Header/Key resolution failure due to case-insensitive TreeMap serialization

1 participant