You are working on the hyperswitch repository (Rust payment processing system).…
- User
You are working on the hyperswitch repository (Rust payment processing system).
REPOSITORY SETUP:
- Repository: juspay/hyperswitch
- Working directory: ./repos/hyperswitch_pool_9 (already cloned)
- Base commit: 8446ffbf5992a97d79d129cade997effc60fcd85
- Task ID: juspay__hyperswitch-9063
- Version: v1.116.0
TASK DESCRIPTION: Bug: Change Underscore(_) to hyphen(-) in payment link locale.
Change Underscore(_) to hyphen(-) in payment link locale.
DETAILED CONTEXT & HINTS:
Type of Change
- Bugfix
- New feature
- Enhancement
- Refactoring
- Dependency updates
- Documentation
- CI/CD
Description
Change Underscore(_) to hyphen(-) in payment link locale according to ISO standards.
Additional Changes
- This PR modifies the API contract
- This PR modifies the database schema
- This PR modifies application configuration/environment variables
Motivation and Context
We need to follow ISO standards when accepting the locale for payment links.
How did you test it?
curl --location 'http://localhost:8080/payments' \ --header 'Content-Type: application/json' \ --header 'Accept: application/json' \ --header 'api-key: **' \ --header 'Accept-Language: zh-hant' \ --data '{ "amount": 10, "setup_future_usage": "off_session", "currency": "EUR", "payment_link": true, "session_expiry": 60, "return_url": "https://google.com", "payment_link_config": { "theme": "#14356f", "logo": "https://logo.com/wp-content/uploads/2020/08/zurich.svg", "seller_name": "Zurich Inc." } }'zh-hant - should see traditional chinese zh_hant - english zh-hant-abcdef - traditional chinese zh - simplified chinese Zh - simplified chinese ZH-HANT - traditional chinese zh-abcdef - simplified chinese Assistant
I'll help you implement the bug fix to change underscores to hyphens in payment link locale according to ISO standards. Let me start by exploring the codebase to understand how payment link locales are currently handled.
First, I'll create a todo list to track the key tasks:
Assistant
Let me start by searching for files related to payment links and locale handling to understand the current implementation.
Assistant
Now let me search for locale-related code and Accept-Language handling:
Assistant
Let me also search for Accept-Language specifically:
Assistant
Let me look at the locale.rs file which seems relevant, and also check payment link related files:
Assistant
(no content)
Assistant
Let me look at the API models to understand the structure better:
Assistant
Let me search more specifically for the locale handling and look at the payment link configuration structures:
Assistant
Let me examine the payment link API models:
Assistant
Let me check specifically in the payment_link.rs file in the API models:
Assistant
Let me examine the PaymentLinkDetails struct to understand how locale is handled:
Assistant
Now I can see that both
PaymentLinkDetailsandPaymentLinkStatusDetailshave alocalefield of typeOption<String>(line 8616 and 8675). Let me check where the Accept-Language header is processed and how the locale is derived. Let me look at the middleware and request handling:Assistant
Perfect! I can see the
AddAcceptLanguageHeadermiddleware (lines 320-399) handles the Accept-Language header processing. Now I need to check where the locale is being used in the payment link creation flow. Let me examine the routes for payment link creation:Assistant
Now let me check the core payment link module where the actual processing happens. Let me also search for where the state.locale is used:
Assistant
Let me now check where locale conversion actually happens in the payment_link.rs. Based on what I've seen, I see that state.locale is used but it looks like it's not being converted from underscores to hyphens. Let me look closely at the relevant code again in the payment_link.rs file where the locale gets set:
Assistant
Let me look more carefully around lines 274, 310, 1041 in the payment_link.rs to see how the locale is being passed:
Assistant
And around line 1041:
Assistant
I need to now check where
state.localeis coming from and what it looks like originally. Looking at the middleware code, I can see it's derived from the Accept-Language header, but there's no conversion from underscores to hyphens happening. According to the issue, the problem is that ISO standards require hyphens (-) not underscores (_) in locale identifiers.Based on the test examples in the issue description, I can see:
zh-hant- should see traditional chinese (correct)zh_hant- english (wrong - should be traditional chinese)
So the problem is that when locale comes in with underscores from Accept-Language headers, it's being used as-is, but ISO standards require hyphens.
Let me first update the todo list to reflect the actual task:
Assistant
Let me look closer at how the Accept-Language is processed in the middleware to understand how we can modify it:
Assistant
I can see that the Accept-Language header is processed but there's no conversion happening. The issue says we need to ensure that any locale identifier with underscores gets converted to hyphens to follow ISO standards.
Looking more carefully at the problem, I need to find where the state.locale is populated and where it's actually used. The solution should be to either:
- Modify the Accept-Language header processing to convert underscores to hyphens, or
- Convert it when it's being used in payment links
Let me trace where
state.localeis initialized. It must be coming from somewhere in the state initialization:Assistant
Let me check the transformers.rs file to see where locale is assigned to state:
Assistant
Now I need to find where
state.localeis assigned to understand the complete flow. Let me search forstate.localeassignments:Assistant
Looking at the code I've seen, the problem is clear now. Looking at lines 274, 310, and 1041 in payment_link.rs, I can see that the
state.localeis being directly used in the payment link generation, but the Accept-Language header can contain locale codes with underscores (likezh_hant) while ISO standards require hyphens (zh-hant).Based on the issue description and examples:
zh-hant- should see traditional chinese (correct)zh_hant- english (wrong - should be traditional chinese)
So the issue is in the middleware where
Accept-Languageheader values are processed and stored asstate.locale. I need to modify the middleware or the payment link code to convert underscores to hyphens in the locale.Let me make the required change in the middleware to ensure that locales are converted to use hyphens instead of underscores before being stored in state:
Assistant
I understand that I don't have permission to make changes to the file. However, I can see the exact change that needs to be made. Based on my analysis, I need to update the
AddAcceptLanguageHeadermiddleware to convert underscores to hyphens in locale values to follow ISO standards.The fix is to add
.replace('_', '-')when setting the Accept-Language header value in the middleware. This change needs to be applied to line 386 in/workspace/archit/trace_generation/repos/hyperswitch_pool_9/crates/router/src/middleware.rs.Here's what I found and what needs to be changed:
-
Problem: The
AddAcceptLanguageHeadermiddleware inmiddleware.rsaccepts locale values from query parameters or headers but doesn't convert underscores to hyphens, which violates ISO standards. -
Solution: In the middleware, when we set the Accept-Language header value, we need to convert any underscores to hyphens to conform to ISO standards.
-
Specific Change: On line 386 where we insert the Accept-Language header, we should modify:
http::HeaderValue::from_str(&locale)?
-
Assistant
I've completed the analysis of the bug and identified exactly where to make the change to fix the issue. Here's what I found:
Problem Analysis
The issue is in the
AddAcceptLanguageHeadermiddleware incrates/router/src/middleware.rs. When Accept-Language headers come in with underscore separators (likezh_hant), they should be converted to hyphen separators (likezh-hant) to follow ISO standards.Solution Identified
The fix needs to be applied to the
AddAcceptLanguageHeaderMiddlewarein themiddleware.rsfile, specifically around line 386 where the Accept-Language header is set. We need to convert underscores to hyphens in the locale value before inserting it into the header.The specific change is to modify:
http::HeaderValue::from_str(&locale)?to:
http::HeaderValue::from_str(&locale.replace('_', '-'))?
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.