Skip to content

Commit

Permalink
[Elixir] Adds workaround for httpc for post/put/patch requests (#5682)
Browse files Browse the repository at this point in the history
* Ensures empty body is always present for post/put/patch

* Generate samples
  • Loading branch information
mrmstn authored Jan 26, 2021
1 parent a127aab commit 0ae5491
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -818,6 +818,23 @@ private void buildTypespec(CodegenProperty property, StringBuilder sb) {
sb.append(".t");
}
}

private boolean getRequiresHttpcWorkaround() {
// Only POST/PATCH/PUT are affected from the httpc bug
if (!(this.httpMethod.equals("POST") || this.httpMethod.equals("PATCH") || this.httpMethod.equals("PUT"))) {
return false;
}

// If theres something required for the body, the workaround is not required
for (CodegenParameter requiredParam : this.requiredParams) {
if (requiredParam.isBodyParam || requiredParam.isFormParam) {
return false;
}
}

// In case there is nothing for the body, the operation requires the workaround
return true;
}
}

class ExtendedCodegenModel extends CodegenModel {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ defmodule {{moduleName}}.Api.{{classname}} do
|> add_optional_params(optional_params, opts)
{{/-first}}
{{/optionalParams}}
{{#requiresHttpcWorkaround}}
|> ensure_body()
{{/requiresHttpcWorkaround}}
|> Enum.into([])
|> (&Connection.request(connection, &1)).()
|> evaluate_response({{#responses}}{{#-first}}[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,28 @@ defmodule {{moduleName}}.RequestBuilder do
Map.update(request, location, [{key, value}], &(&1 ++ [{key, value}]))
end

@doc """
Due to a bug in httpc, POST, PATCH and PUT requests will fail, if the body is empty

This function will ensure, that the body param is always set

## Parameters

- request (Map) - Collected request options

## Returns

Map
"""
@spec ensure_body(map()) :: map()
def ensure_body(%{body: nil} = request) do
%{request | body: ""}
end

def ensure_body(request) do
Map.put_new(request, :body, "")
end

@doc """
Handle the response for a Tesla request

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ defmodule OpenapiPetstore.Api.Fake do
|> method(:post)
|> url("/fake/outer/boolean")
|> add_optional_params(optional_params, opts)
|> ensure_body()
|> Enum.into([])
|> (&Connection.request(connection, &1)).()
|> evaluate_response([
Expand Down Expand Up @@ -89,6 +90,7 @@ defmodule OpenapiPetstore.Api.Fake do
|> method(:post)
|> url("/fake/outer/composite")
|> add_optional_params(optional_params, opts)
|> ensure_body()
|> Enum.into([])
|> (&Connection.request(connection, &1)).()
|> evaluate_response([
Expand Down Expand Up @@ -118,6 +120,7 @@ defmodule OpenapiPetstore.Api.Fake do
|> method(:post)
|> url("/fake/outer/number")
|> add_optional_params(optional_params, opts)
|> ensure_body()
|> Enum.into([])
|> (&Connection.request(connection, &1)).()
|> evaluate_response([
Expand Down Expand Up @@ -147,6 +150,7 @@ defmodule OpenapiPetstore.Api.Fake do
|> method(:post)
|> url("/fake/outer/string")
|> add_optional_params(optional_params, opts)
|> ensure_body()
|> Enum.into([])
|> (&Connection.request(connection, &1)).()
|> evaluate_response([
Expand Down Expand Up @@ -457,6 +461,7 @@ defmodule OpenapiPetstore.Api.Fake do
|> add_param(:query, :"http", http)
|> add_param(:query, :"url", url)
|> add_param(:query, :"context", context)
|> ensure_body()
|> Enum.into([])
|> (&Connection.request(connection, &1)).()
|> evaluate_response([
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ defmodule OpenapiPetstore.Api.Pet do
|> method(:post)
|> url("/pet/#{pet_id}")
|> add_optional_params(optional_params, opts)
|> ensure_body()
|> Enum.into([])
|> (&Connection.request(connection, &1)).()
|> evaluate_response([
Expand Down Expand Up @@ -239,6 +240,7 @@ defmodule OpenapiPetstore.Api.Pet do
|> method(:post)
|> url("/pet/#{pet_id}/uploadImage")
|> add_optional_params(optional_params, opts)
|> ensure_body()
|> Enum.into([])
|> (&Connection.request(connection, &1)).()
|> evaluate_response([
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,28 @@ defmodule OpenapiPetstore.RequestBuilder do
Map.update(request, location, [{key, value}], &(&1 ++ [{key, value}]))
end

@doc """
Due to a bug in httpc, POST, PATCH and PUT requests will fail, if the body is empty
This function will ensure, that the body param is always set
## Parameters
- request (Map) - Collected request options
## Returns
Map
"""
@spec ensure_body(map()) :: map()
def ensure_body(%{body: nil} = request) do
%{request | body: ""}
end

def ensure_body(request) do
Map.put_new(request, :body, "")
end

@doc """
Handle the response for a Tesla request
Expand Down

0 comments on commit 0ae5491

Please sign in to comment.