googleapis / googleapis/google-cloud-swift
`AuthHTTPClient.post` Fails to Encode Request Bodies in snake_case
- Dominant language
- Swift
- Stars
- 26
- Forks
- 10
- Avg merge
- 12h 56m
- Merged PRs (30d)
- 211
Description
## Location
- [pkgs/swift-google-auth/Sources/GoogleCloudAuth/Http/AuthHTTPClient.swift:L184-L186](file:///usr/local/google/home/coryan/google-cloud-swift/pkgs/swift-google-auth/Sources/GoogleCloudAuth/Http/AuthHTTPClient.swift#L184-L186)
- [pkgs/swift-google-auth/Sources/GoogleCloudAuth/Http/AuthHTTPClient.swift:L261-L265](file:///usr/local/google/home/coryan/google-cloud-swift/pkgs/swift-google-auth/Sources/GoogleCloudAuth/Http/AuthHTTPClient.swift#L261-L265)
## Description
`AuthHTTPClient.post` creates a default `JSONEncoder()` without configuring `keyEncodingStrategy = .convertToSnakeCase`, despite the method documentation explicitly stating that Encodable bodies are converted using snake_case key encoding. While a helper method `makeEncoder()` was implemented to provide a snake_case-configured `JSONEncoder`, `post()` does not call it.
As a result, Encodable request bodies such as `Oauth2RefreshRequest` (used in `UserAccountTokenProvider`) are serialized with camelCase JSON keys (`grantType`, `clientId`, `clientSecret`, `refreshToken`) instead of the `snake_case` keys (`grant_type`, `client_id`, `client_secret`, `refresh_token`) required by Google's OAuth 2.0 token endpoints.
## Steps to Reproduce / Verification
Post an Encodable struct with camelCase property names and inspect the outgoing request body bytes:
```swift
struct TestBody: Encodable {
let grantType: String
}
let mock = MockHTTPClient([
{ (request: HTTPClientRequest) in
// Extract request body and verify keys
// Currently receives {"grantType":"refresh_token"} instead of {"grant_type":"refresh_token"}
return HTTPClientResponse(status: .ok, body: .bytes(.init(data: Data("{}".utf8))))
}
])
let client = AuthHTTPClient(mock: mock)
let _: MockResponse = try await client.post(url: URL(string: "https://oauth2.googleapis.com/token")!, body: TestBody(grantType: "refresh_token"))
```
Contributor guide
Assessment
This issue has not been assessed yet.