Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 23 additions & 13 deletions clients/gax/lib/google_api/gax/connection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,12 @@ defmodule GoogleApi.Gax.Connection do
)
)

plug(Tesla.Middleware.DecompressResponse, [])
# turnhub patch: Tesla 1.21 made :max_body_size mandatory on the
# (de)compression middleware (decompression-bomb CVE fix). Upstream
# elixir-google-api was archived (2026-06) before adopting it, so pin
# :infinity here to preserve the pre-1.21 behaviour. Remove once we migrate
# off this library (turnhub/engage Req+Goth migration).
plug(Tesla.Middleware.DecompressResponse, max_body_size: :infinity)

plug(Tesla.Middleware.EncodeJson, engine: Poison)

Expand Down Expand Up @@ -167,7 +172,7 @@ defmodule GoogleApi.Gax.Connection do
defp build_body(output, [], file_params) do
body =
Enum.reduce(file_params, Tesla.Multipart.new(), fn {file_name, file_path}, b ->
Tesla.Multipart.add_file(b, file_path, name: file_name)
Tesla.Multipart.add_file(b, file_path, name: to_string(file_name))
end)

Keyword.put(output, :body, body)
Expand All @@ -178,30 +183,35 @@ defmodule GoogleApi.Gax.Connection do

{meta, body_params} = extract_metadata(body_params)

body = case meta do
nil -> body
_ -> Tesla.Multipart.add_field(
body,
:metadata,
Poison.encode!(meta),
headers: [{:"Content-Type", "application/json"}]
)
end
body =
case meta do
nil ->
body

_ ->
Tesla.Multipart.add_field(
body,
"metadata",
Poison.encode!(meta),
headers: [{:"Content-Type", "application/json"}]
)
end

body =
Enum.reduce(body_params, body, fn {body_name, data}, b ->
{res, type} = try_encode_multipart_field(data, meta)

Tesla.Multipart.add_field(
b,
body_name,
to_string(body_name),
res,
headers: [{:"Content-Type", type}]
)
end)

body =
Enum.reduce(file_params, body, fn {file_name, file_path}, b ->
Tesla.Multipart.add_file(b, file_path, name: file_name)
Tesla.Multipart.add_file(b, file_path, name: to_string(file_name))
end)

Keyword.put(output, :body, body)
Expand Down
2 changes: 1 addition & 1 deletion clients/gax/mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ defmodule GoogleApi.Gax.MixProject do
defp deps() do
[
{:tesla, "~> 1.2"},
{:mime, "~> 1.0"},
{:mime, "~> 1.0 or ~> 2.0"},
{:poison, ">= 3.0.0 and < 5.0.0"},
{:ex_doc, "~> 0.16", only: :dev},
{:dialyxir, "~> 0.5", only: [:dev], runtime: false}
Expand Down
6 changes: 6 additions & 0 deletions clients/gax/test/gax/connection_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ defmodule Gax.ConnectionTest do
test "builds a multipart upload request with iodata and content type" do
metadata = %{contentType: "text/plain"}
data = ["1", ["2"]]

request =
Request.new()
|> Request.add_param(:body, :metadata, metadata)
Expand All @@ -94,13 +95,16 @@ defmodule Gax.ConnectionTest do
[part1, part2] = body.parts
assert "{\"contentType\":\"text/plain\"}" == part1.body
assert [{:"Content-Type", "application/json"}] == part1.headers
assert [name: "metadata"] == part1.dispositions
assert data == part2.body
assert [{:"Content-Type", "text/plain"}] == part2.headers
assert [name: "data"] == part2.dispositions
end

test "builds a multipart upload request with iodata but no content type" do
metadata = %{foo: "bar"}
data = ["1", ["2"]]

request =
Request.new()
|> Request.add_param(:body, :metadata, metadata)
Expand All @@ -118,6 +122,7 @@ defmodule Gax.ConnectionTest do
test "builds a multipart upload request with a JSON decodable struct" do
metadata = %{foo: "bar"}
data = %{baz: "qux"}

request =
Request.new()
|> Request.add_param(:body, :metadata, metadata)
Expand All @@ -135,6 +140,7 @@ defmodule Gax.ConnectionTest do
test "builds a multipart upload request with a non-JSON struct" do
metadata = %{foo: "bar"}
data = %{baz: {}}

assert_raise(Poison.EncodeError, fn ->
Request.new()
|> Request.add_param(:body, :metadata, metadata)
Expand Down