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

[erlang] erlang-server generator fixes #20717

Merged
merged 3 commits into from
Feb 26, 2025
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 @@ -179,7 +179,7 @@ populate_request_params(_, [], Req, _, Model) ->
populate_request_params(OperationID, [ReqParamName | T], Req0, ValidatorState, Model0) ->
case populate_request_param(OperationID, ReqParamName, Req0, ValidatorState) of
{ok, V, Req} ->
Model = maps:put(ReqParamName, V, Model0),
Model = Model0#{ReqParamName => V},
populate_request_params(OperationID, T, Req, ValidatorState, Model);
Error ->
Error
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ Exposes the following operation IDs:
{operation_id :: operation_id(),
accept_callback :: {{packageName}}_logic_handler:accept_callback(),
provide_callback :: {{packageName}}_logic_handler:provide_callback(),
api_key_handler :: {{packageName}}_logic_handler:api_key_callback(),
api_key_callback :: {{packageName}}_logic_handler:api_key_callback(),
context = #{} :: {{packageName}}_logic_handler:context()}).

-type state() :: #state{}.
Expand All @@ -52,7 +52,7 @@ init(Req, {Operations, Module}) ->
State = #state{operation_id = OperationID,
accept_callback = fun Module:accept_callback/4,
provide_callback = fun Module:provide_callback/4,
api_key_handler = fun Module:authorize_api_key/2},
api_key_callback = fun Module:api_key_callback/2},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For a moment I feared this would be a breaking change, but actually, this is really a bugfix, the callback was declared as api_key_callback/2 and dialyzer would complain, so the previous code would fail to bind to the right function and crash. Awesome find, thank you very much!

{cowboy_rest, Req, State}.

-spec allowed_methods(cowboy_req:req(), state()) ->
Expand All @@ -69,8 +69,8 @@ init(Req, {Operations, Module}) ->
{{#authMethods.size}}
is_authorized(Req0,
#state{operation_id = '{{operationIdOriginal}}' = OperationID,
api_key_handler = Handler} = State) ->
case {{packageName}}_auth:authorize_api_key(Handler, OperationID, {{#isApiKey.isKeyInQuery}}qs_val, {{/isApiKey.isKeyInQuery}}{{^isApiKey.isKeyInQuery}}header, {{/isApiKey.isKeyInQuery}}{{#isApiKey}}"{{keyParamName}}", {{/isApiKey}}{{^isApiKey}}"authorization", {{/isApiKey}}Req0) of
api_key_callback = Handler} = State) ->
case {{packageName}}_auth:authorize_api_key(Handler, OperationID, {{#isApiKey.isKeyInQuery}}qs_val, {{/isApiKey.isKeyInQuery}}{{^isApiKey.isKeyInQuery}}header, {{/isApiKey.isKeyInQuery}}{{#isApiKey}}"{{keyParamName}}", {{/isApiKey}}{{^isApiKey}}<<"authorization">>, {{/isApiKey}}Req0) of
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, so this needed to be a binary, that looks good 👌🏽

{true, Context, Req} ->
{true, Req, State#state{context = Context}};
{false, AuthHeader, Req} ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ get_cowboy_config(LogicHandler, ExtraOpts) ->
maps:fold(fun get_cowboy_config/3, DefaultOpts, ExtraOpts).

get_cowboy_config(env, #{dispatch := _Dispatch} = Env, AccIn) ->
maps:put(env, Env, AccIn);
AccIn#{env => Env};
get_cowboy_config(env, NewEnv, #{env := OldEnv} = AccIn) ->
Env = maps:merge(OldEnv, NewEnv),
maps:put(env, Env, AccIn);
AccIn#{env => Env};
get_cowboy_config(Key, Value, AccIn) ->
maps:put(Key, Value, AccIn).
AccIn#{Key => Value}.

get_default_dispatch(LogicHandler) ->
Paths = {{packageName}}_router:get_paths(LogicHandler),
Expand Down
2 changes: 1 addition & 1 deletion samples/server/echo_api/erlang-server/src/openapi_api.erl
Original file line number Diff line number Diff line change
Expand Up @@ -686,7 +686,7 @@ populate_request_params(_, [], Req, _, Model) ->
populate_request_params(OperationID, [ReqParamName | T], Req0, ValidatorState, Model0) ->
case populate_request_param(OperationID, ReqParamName, Req0, ValidatorState) of
{ok, V, Req} ->
Model = maps:put(ReqParamName, V, Model0),
Model = Model0#{ReqParamName => V},
populate_request_params(OperationID, T, Req, ValidatorState, Model);
Error ->
Error
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ To test HTTP bearer authentication
{operation_id :: operation_id(),
accept_callback :: openapi_logic_handler:accept_callback(),
provide_callback :: openapi_logic_handler:provide_callback(),
api_key_handler :: openapi_logic_handler:api_key_callback(),
api_key_callback :: openapi_logic_handler:api_key_callback(),
context = #{} :: openapi_logic_handler:context()}).

-type state() :: #state{}.
Expand All @@ -57,7 +57,7 @@ init(Req, {Operations, Module}) ->
State = #state{operation_id = OperationID,
accept_callback = fun Module:accept_callback/4,
provide_callback = fun Module:provide_callback/4,
api_key_handler = fun Module:authorize_api_key/2},
api_key_callback = fun Module:api_key_callback/2},
{cowboy_rest, Req, State}.

-spec allowed_methods(cowboy_req:req(), state()) ->
Expand All @@ -73,17 +73,17 @@ allowed_methods(Req, State) ->
{true | {false, iodata()}, cowboy_req:req(), state()}.
is_authorized(Req0,
#state{operation_id = 'test/auth/http/basic' = OperationID,
api_key_handler = Handler} = State) ->
case openapi_auth:authorize_api_key(Handler, OperationID, header, "authorization", Req0) of
api_key_callback = Handler} = State) ->
case openapi_auth:authorize_api_key(Handler, OperationID, header, <<"authorization">>, Req0) of
{true, Context, Req} ->
{true, Req, State#state{context = Context}};
{false, AuthHeader, Req} ->
{{false, AuthHeader}, Req, State}
end;
is_authorized(Req0,
#state{operation_id = 'test/auth/http/bearer' = OperationID,
api_key_handler = Handler} = State) ->
case openapi_auth:authorize_api_key(Handler, OperationID, header, "authorization", Req0) of
api_key_callback = Handler} = State) ->
case openapi_auth:authorize_api_key(Handler, OperationID, header, <<"authorization">>, Req0) of
{true, Context, Req} ->
{true, Req, State#state{context = Context}};
{false, AuthHeader, Req} ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ Test empty json (request body)
{operation_id :: operation_id(),
accept_callback :: openapi_logic_handler:accept_callback(),
provide_callback :: openapi_logic_handler:provide_callback(),
api_key_handler :: openapi_logic_handler:api_key_callback(),
api_key_callback :: openapi_logic_handler:api_key_callback(),
context = #{} :: openapi_logic_handler:context()}).

-type state() :: #state{}.
Expand All @@ -97,7 +97,7 @@ init(Req, {Operations, Module}) ->
State = #state{operation_id = OperationID,
accept_callback = fun Module:accept_callback/4,
provide_callback = fun Module:provide_callback/4,
api_key_handler = fun Module:authorize_api_key/2},
api_key_callback = fun Module:api_key_callback/2},
{cowboy_rest, Req, State}.

-spec allowed_methods(cowboy_req:req(), state()) ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ Test form parameter(s) for oneOf schema
{operation_id :: operation_id(),
accept_callback :: openapi_logic_handler:accept_callback(),
provide_callback :: openapi_logic_handler:provide_callback(),
api_key_handler :: openapi_logic_handler:api_key_callback(),
api_key_callback :: openapi_logic_handler:api_key_callback(),
context = #{} :: openapi_logic_handler:context()}).

-type state() :: #state{}.
Expand All @@ -62,7 +62,7 @@ init(Req, {Operations, Module}) ->
State = #state{operation_id = OperationID,
accept_callback = fun Module:accept_callback/4,
provide_callback = fun Module:provide_callback/4,
api_key_handler = fun Module:authorize_api_key/2},
api_key_callback = fun Module:api_key_callback/2},
{cowboy_rest, Req, State}.

-spec allowed_methods(cowboy_req:req(), state()) ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ Test header parameter(s)
{operation_id :: operation_id(),
accept_callback :: openapi_logic_handler:accept_callback(),
provide_callback :: openapi_logic_handler:provide_callback(),
api_key_handler :: openapi_logic_handler:api_key_callback(),
api_key_callback :: openapi_logic_handler:api_key_callback(),
context = #{} :: openapi_logic_handler:context()}).

-type state() :: #state{}.
Expand All @@ -52,7 +52,7 @@ init(Req, {Operations, Module}) ->
State = #state{operation_id = OperationID,
accept_callback = fun Module:accept_callback/4,
provide_callback = fun Module:provide_callback/4,
api_key_handler = fun Module:authorize_api_key/2},
api_key_callback = fun Module:api_key_callback/2},
{cowboy_rest, Req, State}.

-spec allowed_methods(cowboy_req:req(), state()) ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ Test path parameter(s)
{operation_id :: operation_id(),
accept_callback :: openapi_logic_handler:accept_callback(),
provide_callback :: openapi_logic_handler:provide_callback(),
api_key_handler :: openapi_logic_handler:api_key_callback(),
api_key_callback :: openapi_logic_handler:api_key_callback(),
context = #{} :: openapi_logic_handler:context()}).

-type state() :: #state{}.
Expand All @@ -52,7 +52,7 @@ init(Req, {Operations, Module}) ->
State = #state{operation_id = OperationID,
accept_callback = fun Module:accept_callback/4,
provide_callback = fun Module:provide_callback/4,
api_key_handler = fun Module:authorize_api_key/2},
api_key_callback = fun Module:api_key_callback/2},
{cowboy_rest, Req, State}.

-spec allowed_methods(cowboy_req:req(), state()) ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ Test query parameter(s)
{operation_id :: operation_id(),
accept_callback :: openapi_logic_handler:accept_callback(),
provide_callback :: openapi_logic_handler:provide_callback(),
api_key_handler :: openapi_logic_handler:api_key_callback(),
api_key_callback :: openapi_logic_handler:api_key_callback(),
context = #{} :: openapi_logic_handler:context()}).

-type state() :: #state{}.
Expand All @@ -97,7 +97,7 @@ init(Req, {Operations, Module}) ->
State = #state{operation_id = OperationID,
accept_callback = fun Module:accept_callback/4,
provide_callback = fun Module:provide_callback/4,
api_key_handler = fun Module:authorize_api_key/2},
api_key_callback = fun Module:api_key_callback/2},
{cowboy_rest, Req, State}.

-spec allowed_methods(cowboy_req:req(), state()) ->
Expand Down
6 changes: 3 additions & 3 deletions samples/server/echo_api/erlang-server/src/openapi_server.erl
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ get_cowboy_config(LogicHandler, ExtraOpts) ->
maps:fold(fun get_cowboy_config/3, DefaultOpts, ExtraOpts).

get_cowboy_config(env, #{dispatch := _Dispatch} = Env, AccIn) ->
maps:put(env, Env, AccIn);
AccIn#{env => Env};
get_cowboy_config(env, NewEnv, #{env := OldEnv} = AccIn) ->
Env = maps:merge(OldEnv, NewEnv),
maps:put(env, Env, AccIn);
AccIn#{env => Env};
get_cowboy_config(Key, Value, AccIn) ->
maps:put(Key, Value, AccIn).
AccIn#{Key => Value}.

get_default_dispatch(LogicHandler) ->
Paths = openapi_router:get_paths(LogicHandler),
Expand Down
2 changes: 1 addition & 1 deletion samples/server/petstore/erlang-server/src/openapi_api.erl
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,7 @@ populate_request_params(_, [], Req, _, Model) ->
populate_request_params(OperationID, [ReqParamName | T], Req0, ValidatorState, Model0) ->
case populate_request_param(OperationID, ReqParamName, Req0, ValidatorState) of
{ok, V, Req} ->
Model = maps:put(ReqParamName, V, Model0),
Model = Model0#{ReqParamName => V},
populate_request_params(OperationID, T, Req, ValidatorState, Model);
Error ->
Error
Expand Down
36 changes: 18 additions & 18 deletions samples/server/petstore/erlang-server/src/openapi_pet_handler.erl
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ uploads an image.
{operation_id :: operation_id(),
accept_callback :: openapi_logic_handler:accept_callback(),
provide_callback :: openapi_logic_handler:provide_callback(),
api_key_handler :: openapi_logic_handler:api_key_callback(),
api_key_callback :: openapi_logic_handler:api_key_callback(),
context = #{} :: openapi_logic_handler:context()}).

-type state() :: #state{}.
Expand All @@ -87,7 +87,7 @@ init(Req, {Operations, Module}) ->
State = #state{operation_id = OperationID,
accept_callback = fun Module:accept_callback/4,
provide_callback = fun Module:provide_callback/4,
api_key_handler = fun Module:authorize_api_key/2},
api_key_callback = fun Module:api_key_callback/2},
{cowboy_rest, Req, State}.

-spec allowed_methods(cowboy_req:req(), state()) ->
Expand Down Expand Up @@ -115,71 +115,71 @@ allowed_methods(Req, State) ->
{true | {false, iodata()}, cowboy_req:req(), state()}.
is_authorized(Req0,
#state{operation_id = 'addPet' = OperationID,
api_key_handler = Handler} = State) ->
case openapi_auth:authorize_api_key(Handler, OperationID, header, "authorization", Req0) of
api_key_callback = Handler} = State) ->
case openapi_auth:authorize_api_key(Handler, OperationID, header, <<"authorization">>, Req0) of
{true, Context, Req} ->
{true, Req, State#state{context = Context}};
{false, AuthHeader, Req} ->
{{false, AuthHeader}, Req, State}
end;
is_authorized(Req0,
#state{operation_id = 'deletePet' = OperationID,
api_key_handler = Handler} = State) ->
case openapi_auth:authorize_api_key(Handler, OperationID, header, "authorization", Req0) of
api_key_callback = Handler} = State) ->
case openapi_auth:authorize_api_key(Handler, OperationID, header, <<"authorization">>, Req0) of
{true, Context, Req} ->
{true, Req, State#state{context = Context}};
{false, AuthHeader, Req} ->
{{false, AuthHeader}, Req, State}
end;
is_authorized(Req0,
#state{operation_id = 'findPetsByStatus' = OperationID,
api_key_handler = Handler} = State) ->
case openapi_auth:authorize_api_key(Handler, OperationID, header, "authorization", Req0) of
api_key_callback = Handler} = State) ->
case openapi_auth:authorize_api_key(Handler, OperationID, header, <<"authorization">>, Req0) of
{true, Context, Req} ->
{true, Req, State#state{context = Context}};
{false, AuthHeader, Req} ->
{{false, AuthHeader}, Req, State}
end;
is_authorized(Req0,
#state{operation_id = 'findPetsByTags' = OperationID,
api_key_handler = Handler} = State) ->
case openapi_auth:authorize_api_key(Handler, OperationID, header, "authorization", Req0) of
api_key_callback = Handler} = State) ->
case openapi_auth:authorize_api_key(Handler, OperationID, header, <<"authorization">>, Req0) of
{true, Context, Req} ->
{true, Req, State#state{context = Context}};
{false, AuthHeader, Req} ->
{{false, AuthHeader}, Req, State}
end;
is_authorized(Req0,
#state{operation_id = 'getPetById' = OperationID,
api_key_handler = Handler} = State) ->
case openapi_auth:authorize_api_key(Handler, OperationID, header, "authorization", Req0) of
api_key_callback = Handler} = State) ->
case openapi_auth:authorize_api_key(Handler, OperationID, header, <<"authorization">>, Req0) of
{true, Context, Req} ->
{true, Req, State#state{context = Context}};
{false, AuthHeader, Req} ->
{{false, AuthHeader}, Req, State}
end;
is_authorized(Req0,
#state{operation_id = 'updatePet' = OperationID,
api_key_handler = Handler} = State) ->
case openapi_auth:authorize_api_key(Handler, OperationID, header, "authorization", Req0) of
api_key_callback = Handler} = State) ->
case openapi_auth:authorize_api_key(Handler, OperationID, header, <<"authorization">>, Req0) of
{true, Context, Req} ->
{true, Req, State#state{context = Context}};
{false, AuthHeader, Req} ->
{{false, AuthHeader}, Req, State}
end;
is_authorized(Req0,
#state{operation_id = 'updatePetWithForm' = OperationID,
api_key_handler = Handler} = State) ->
case openapi_auth:authorize_api_key(Handler, OperationID, header, "authorization", Req0) of
api_key_callback = Handler} = State) ->
case openapi_auth:authorize_api_key(Handler, OperationID, header, <<"authorization">>, Req0) of
{true, Context, Req} ->
{true, Req, State#state{context = Context}};
{false, AuthHeader, Req} ->
{{false, AuthHeader}, Req, State}
end;
is_authorized(Req0,
#state{operation_id = 'uploadFile' = OperationID,
api_key_handler = Handler} = State) ->
case openapi_auth:authorize_api_key(Handler, OperationID, header, "authorization", Req0) of
api_key_callback = Handler} = State) ->
case openapi_auth:authorize_api_key(Handler, OperationID, header, <<"authorization">>, Req0) of
{true, Context, Req} ->
{true, Req, State#state{context = Context}};
{false, AuthHeader, Req} ->
Expand Down
6 changes: 3 additions & 3 deletions samples/server/petstore/erlang-server/src/openapi_server.erl
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ get_cowboy_config(LogicHandler, ExtraOpts) ->
maps:fold(fun get_cowboy_config/3, DefaultOpts, ExtraOpts).

get_cowboy_config(env, #{dispatch := _Dispatch} = Env, AccIn) ->
maps:put(env, Env, AccIn);
AccIn#{env => Env};
get_cowboy_config(env, NewEnv, #{env := OldEnv} = AccIn) ->
Env = maps:merge(OldEnv, NewEnv),
maps:put(env, Env, AccIn);
AccIn#{env => Env};
get_cowboy_config(Key, Value, AccIn) ->
maps:put(Key, Value, AccIn).
AccIn#{Key => Value}.

get_default_dispatch(LogicHandler) ->
Paths = openapi_router:get_paths(LogicHandler),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ Place an order for a pet.
{operation_id :: operation_id(),
accept_callback :: openapi_logic_handler:accept_callback(),
provide_callback :: openapi_logic_handler:provide_callback(),
api_key_handler :: openapi_logic_handler:api_key_callback(),
api_key_callback :: openapi_logic_handler:api_key_callback(),
context = #{} :: openapi_logic_handler:context()}).

-type state() :: #state{}.
Expand All @@ -67,7 +67,7 @@ init(Req, {Operations, Module}) ->
State = #state{operation_id = OperationID,
accept_callback = fun Module:accept_callback/4,
provide_callback = fun Module:provide_callback/4,
api_key_handler = fun Module:authorize_api_key/2},
api_key_callback = fun Module:api_key_callback/2},
{cowboy_rest, Req, State}.

-spec allowed_methods(cowboy_req:req(), state()) ->
Expand All @@ -87,8 +87,8 @@ allowed_methods(Req, State) ->
{true | {false, iodata()}, cowboy_req:req(), state()}.
is_authorized(Req0,
#state{operation_id = 'getInventory' = OperationID,
api_key_handler = Handler} = State) ->
case openapi_auth:authorize_api_key(Handler, OperationID, header, "authorization", Req0) of
api_key_callback = Handler} = State) ->
case openapi_auth:authorize_api_key(Handler, OperationID, header, <<"authorization">>, Req0) of
{true, Context, Req} ->
{true, Req, State#state{context = Context}};
{false, AuthHeader, Req} ->
Expand Down
Loading
Loading