Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Elixir] Adds workaround for httpc for post/put/patch requests #5682

Merged
merged 3 commits into from
Jan 26, 2021
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -594,12 +594,12 @@ public ExtendedCodegenResponse(CodegenResponse o) {
this.isDefinedDefault = (this.code.equals("0") || this.code.equals("default"));
}

public String codeMappingKey(){
if(this.isDefinedDefault) {
public String codeMappingKey() {
if (this.isDefinedDefault) {
return ":default";
}

if(code.matches("^\\d{3}$")){
if (code.matches("^\\d{3}$")) {
return code;
}

Expand Down Expand Up @@ -820,6 +820,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