Skip to content

Commit

Permalink
Ensures empty body is always present for post/put/patch
Browse files Browse the repository at this point in the history
  • Loading branch information
mrmstn committed Oct 5, 2020
1 parent 0e1d131 commit a251e81
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 4 deletions.
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 @@ -742,7 +742,7 @@ public String typespec() {
sb.append(".t");
} else {
if (returnContainer.equals("array") ||
returnContainer.equals("set")) {
returnContainer.equals("set")) {
sb.append("list(");
if (!returnTypeIsPrimitive) {
sb.append(moduleName);
Expand Down Expand Up @@ -809,6 +809,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 @@ -57,6 +57,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

0 comments on commit a251e81

Please sign in to comment.