From cdc00769aadb46d4617fbec35daee643e95b7926 Mon Sep 17 00:00:00 2001 From: Mikael Lixenstrand <78344223+lixen-wg2@users.noreply.github.com> Date: Thu, 3 Jun 2021 03:49:32 +0200 Subject: [PATCH] [Erlang] return empty body for 204 (#9512) * fix handle_request_json spec res * fix client spec * make it possible to answer with 0 size binary for 204 * only generate empty bin for 204 * only generate empty bin for 204 --- .../src/main/resources/erlang-client/api.mustache | 4 ++-- .../src/main/resources/erlang-server/handler.mustache | 9 +++++++-- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/erlang-client/api.mustache b/modules/openapi-generator/src/main/resources/erlang-client/api.mustache index a2335745bbb..ddb5dee0138 100644 --- a/modules/openapi-generator/src/main/resources/erlang-client/api.mustache +++ b/modules/openapi-generator/src/main/resources/erlang-client/api.mustache @@ -3,7 +3,7 @@ -export([{{#operations}}{{#operation}}{{^-first}}, {{/-first}}{{operationId}}/{{arityRequired}}, {{operationId}}/{{arityOptional}}{{/operation}}{{/operations}}]). --define(BASE_URL, "{{{basePathWithoutHost}}}"). +-define(BASE_URL, <<"{{{basePathWithoutHost}}}">>). {{#operations}} {{#operation}} @@ -21,7 +21,7 @@ Cfg = maps:get(cfg, Optional, application:get_env(kuberl, config, #{})), Method = {{httpMethod}}, - Path = ["{{{replacedPathName}}}"], + Path = [<<"{{{replacedPathName}}}">>], QS = {{#queryParams.isEmpty}}[]{{/queryParams.isEmpty}}{{^queryParams.isEmpty}}lists:flatten([{{#joinWithComma}}{{#queryParams}}{{#required}}{{#qsEncode}}{{this}}{{/qsEncode}} {{/required}}{{/queryParams}}{{/joinWithComma}}])++{{packageName}}_utils:optional_params([{{#joinWithComma}}{{#queryParams}}{{^required}} '{{baseName}}'{{/required}}{{/queryParams}}{{/joinWithComma}}], _OptionalParams){{/queryParams.isEmpty}}, Headers = {{#headerParams.isEmpty}}[]{{/headerParams.isEmpty}}{{^headerParams.isEmpty}}[{{#headerParams}}{{#required}} {<<"{{baseName}}">>, {{paramName}}}{{/required}}{{/headerParams}}]++{{packageName}}_utils:optional_params([{{#joinWithComma}}{{#headerParams}}{{^required}} '{{baseName}}'{{/required}}{{/headerParams}}{{/joinWithComma}}], _OptionalParams){{/headerParams.isEmpty}}, Body1 = {{^formParams.isEmpty}}{form, [{{#joinWithComma}}{{#formParams}}{{#required}} {<<"{{baseName}}">>, {{paramName}}}{{/required}}{{/formParams}}{{/joinWithComma}}]++{{packageName}}_utils:optional_params([{{#joinWithComma}}{{#formParams}}{{^required}} '{{baseName}}'{{/required}}{{/formParams}}{{/joinWithComma}}], _OptionalParams)}{{/formParams.isEmpty}}{{#formParams.isEmpty}}{{#bodyParams.isEmpty}}[]{{/bodyParams.isEmpty}}{{^bodyParams.isEmpty}}{{#bodyParams}}{{paramName}}{{/bodyParams}}{{/bodyParams.isEmpty}}{{/formParams.isEmpty}}, diff --git a/modules/openapi-generator/src/main/resources/erlang-server/handler.mustache b/modules/openapi-generator/src/main/resources/erlang-server/handler.mustache index 80314899a38..dc1c47cc20e 100644 --- a/modules/openapi-generator/src/main/resources/erlang-server/handler.mustache +++ b/modules/openapi-generator/src/main/resources/erlang-server/handler.mustache @@ -209,7 +209,7 @@ process_response(Response, Req0, State = #state{operation_id = OperationID}) -> {stop, Req, State} end. --spec handle_request_json(cowboy_req:req(), state()) -> {cowboy_req:resp_body(), cowboy_req:req(), state()}. +-spec handle_request_json(cowboy_req:req(), state()) -> processed_response(). handle_request_json( Req0, @@ -233,7 +233,7 @@ handle_request_json( Body, ValidatorState ), - PreparedBody = jsx:encode(Body), + PreparedBody = prepare_body(Code, Body), Response = {ok, {Code, Headers, PreparedBody}}, process_response(Response, Req1, State); {error, Reason, Req1} -> @@ -241,3 +241,8 @@ handle_request_json( end. validate_headers(_, Req) -> {true, Req}. + +prepare_body(204, Body) when map_size(Body) == 0; length(Body) == 0 -> + <<>>; +prepare_body(_Code, Body) -> + jsx:encode(Body).