Skip to content

Commit 528a0f3

Browse files
author
Max Lapshin
committed
Revert "Merge branch 'kn-51937-extra-object-key-pass' into 'master'"
This reverts merge request !59
1 parent 8a86dd3 commit 528a0f3

9 files changed

Lines changed: 6 additions & 160 deletions

.gitlab-ci.yml

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,3 @@ run-test:
2222
expire_in: 1 week
2323
paths:
2424
- test-logs
25-
26-
run-test-legacy:
27-
stage: test
28-
needs:
29-
- prepare-src
30-
script:
31-
- mkdir -p test-logs-legacy
32-
- docker run --rm -v ${PWD}/test-logs-legacy:/openapi_handler/test-logs openapi_handler make ci-test-legacy
33-
artifacts:
34-
reports:
35-
junit: test-logs-legacy/*/junit_report.xml
36-
when: always
37-
name: "${CI_JOB_STAGE}_${BRANCH_NAME}-test-legacy"
38-
expire_in: 1 week
39-
paths:
40-
- test-logs-legacy

Makefile

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,5 @@ all:
66
ci-test:
77
REBAR_CONFIG=rebar.config_ ./rebar3 as dev ct --logdir test-logs --readable true
88

9-
ci-test-legacy:
10-
REBAR_CONFIG=rebar.config_ ./rebar3 as dev_legacy ct --logdir test-logs --readable true
11-
129
clean:
1310
rm -rf _build rebar.lock

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@ Also see validation quirks below.
8787
You can customize the way objects are processed.
8888
Available options:
8989
* `#{extra_obj_key => drop}` -- the original object may contain keys not described by schema, just ignore them instead of raising an error
90-
* `#{extra_obj_key => pass}` -- the original object may contain keys not described by schema, just pass them as is instead of raising an error
9190
* `#{required_obj_keys => error}` -- raise an error when original object misses some required keys
9291
* `#{validators => #{Format :: atom() => Validator}}`
9392
`Validator :: fun(Value) -> {ok, ConvertedValue} | {error, #{}}`

rebar.config_

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,5 @@
1212
redbug
1313
]},
1414
{ct_opts, [{ct_hooks, [cth_surefire]}]}
15-
]},
16-
{dev_legacy, [
17-
{deps, [
18-
jsx,
19-
yamerl,
20-
{cowboy, "1.0.1"},
21-
{lhttpc, {git, "https://github.com/erlyvideo/lhttpc.git", {branch, "master"}}},
22-
redbug
23-
]},
24-
{erl_opts, [{d,legacy}]},
25-
{ct_opts, [{ct_hooks, [cth_surefire]}]}
2615
]}
2716
]}.

src/openapi_handler.erl

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44

55
-export([init/2, handle/2, terminate/3]).
6-
-export([routes/1, load_schema/2, choose_module/0]).
6+
-export([routes/1, load_schema/2]).
77
-export([read_schema/1]).
88
-export([routes_sort/1]). % for tests
99

@@ -14,8 +14,6 @@
1414
routes(#{schema := SchemaPath, module := Module, name := Name, prefix := Prefix} = Config) ->
1515
#{} = Schema = load_schema(SchemaPath, Name),
1616
SchemaOpts = get_schema_opts(Config),
17-
HandlerModule = choose_module(),
18-
1917
#{paths := Paths} = Schema,
2018
Routes = lists:map(fun({Path,PathSpec}) ->
2119
<<"/", _/binary>> = CowboyPath = re:replace(atom_to_list(Path), "{([^\\}]+)}", ":\\1",[global,{return,binary}]),
@@ -27,23 +25,13 @@ routes(#{schema := SchemaPath, module := Module, name := Name, prefix := Prefix}
2725
% It is too bad to pass all this stuff through cowboy options because it starts suffering
2826
% from GC on big state. Either ETS, either persistent_term, either compilation of custom code
2927
persistent_term:put({openapi_handler_route,Name,CowboyPath}, PathSpec1#{name => Name, module => Module, schema_opts => SchemaOpts}),
30-
{<<Prefix/binary, CowboyPath/binary>>, HandlerModule, {Name,CowboyPath}}
28+
{<<Prefix/binary, CowboyPath/binary>>, ?MODULE, {Name,CowboyPath}}
3129
end, maps:to_list(Paths)),
3230
% After sorting, bindings (starting with ":") must be after constant path segments, so generic routes only work after specific ones.
3331
% E.g. "/api/users/admin" must be before "/api/users/:id"
3432
% Thus special sorting function
3533
routes_sort(Routes).
3634

37-
choose_module() ->
38-
try
39-
% Check if cowboy uses modern Req (map)
40-
1234 = cowboy_req:port(#{port => 1234}),
41-
?MODULE
42-
catch
43-
error:{badrecord,_}:_ ->
44-
% cowboy uses record for Req, use legacy wrapper
45-
openapi_handler_legacy
46-
end.
4735

4836
prepare_operation_fm(_M, #{operationId := OperationId_} = Operation, Parameters) ->
4937
Op1 = maps:remove(description,Operation),
@@ -540,7 +528,7 @@ terminate(_,_,_) ->
540528

541529
get_schema_opts(#{schema_opts := #{} = SchemaOpts}) ->
542530
maps:map(fun
543-
(extra_obj_key,Flag) when Flag == drop; Flag == error; Flag == pass -> ok;
531+
(extra_obj_key,Flag) when Flag == drop; Flag == error -> ok;
544532
(K,V) -> error({bad_schema_opt,K,V})
545533
end, SchemaOpts),
546534
SchemaOpts;

src/openapi_handler_legacy.erl

Lines changed: 0 additions & 92 deletions
This file was deleted.

src/openapi_schema.erl

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ process(Input, #{} = Opts) ->
4545
(query,Flag) when Flag == true; Flag == false -> ok;
4646
(apply_defaults,Flag) when Flag == true; Flag == false -> ok;
4747
(patch,Flag) when Flag == true; Flag == false -> ok;
48-
(extra_obj_key,Flag) when Flag == drop; Flag == error; Flag == pass -> ok;
48+
(extra_obj_key,Flag) when Flag == drop; Flag == error -> ok;
4949
(required_obj_keys,Flag) when Flag == drop; Flag == error -> ok;
5050
(access_type,Flag) when Flag == read; Flag == write -> ok;
5151
(explain,FlagList) -> check_explain_keys(FlagList);
@@ -593,11 +593,6 @@ check_extra_keys(Input, Encoded, #{extra_obj_key := error} = Opts) when is_map(I
593593
_ -> Encoded
594594
end;
595595

596-
check_extra_keys(Input, Encoded, #{extra_obj_key := pass}) when is_map(Input) andalso is_map(Encoded) andalso map_size(Encoded) > 0 ->
597-
EncodedKeys = [K || K <- maps:keys(Encoded), is_atom(K)],
598-
ExtraKeys = maps:keys(Input) -- [atom_to_binary(K) || K <- EncodedKeys],
599-
maps:merge(maps:with(EncodedKeys, Encoded), maps:with(ExtraKeys, Input));
600-
601596
check_extra_keys(_Input, Encoded, _Opts) ->
602597
Encoded.
603598

test/openapi_handler_SUITE.erl

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,10 @@ groups() ->
3636
]}
3737
].
3838

39-
-ifdef(legacy).
40-
start_http(Routes, ApiName) ->
41-
cowboy:start_http(ApiName, 1, [{port, 0}],
42-
[{env, [{dispatch, cowboy_router:compile([{'_', Routes}])}]}]).
43-
-else.
4439
start_http(Routes, ApiName) ->
4540
cowboy:start_clear(ApiName, [{port, 0}],
4641
#{env => #{dispatch => cowboy_router:compile([{'_', Routes}])}}).
47-
-endif.
42+
4843

4944
init_per_suite(Config) ->
5045
{ok, _} = application:ensure_all_started(cowboy),
@@ -285,7 +280,7 @@ json_array_ok(_) ->
285280
ok.
286281

287282
putFile(#{req := Req, '$cowboy_req' := CowboyReq}) ->
288-
<<"PUT">> = ?MODULE:method(CowboyReq),
283+
<<"PUT">> = cowboy_req:method(CowboyReq),
289284
Body = <<"{\"size\":100}">>,
290285
Req1 = reply(200, #{<<"content-length">> => byte_size(Body), <<"content-type">> => <<"application/json">>}, Body, Req),
291286
{done, Req1}.

test/openapi_schema_SUITE.erl

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ groups() ->
1313
read_default,
1414
extra_keys_error,
1515
extra_keys_drop,
16-
extra_keys_pass,
1716
null_in_array,
1817
nullable_by_oneof,
1918
discriminator,
@@ -85,14 +84,6 @@ extra_keys_drop(_) ->
8584
#{inputs := [],name := <<"read_default">>,static := true} = openapi_schema:process(Json, #{type => stream_config, whole_schema => Schema, apply_defaults => true}),
8685
ok.
8786

88-
extra_keys_pass(_) ->
89-
Json = #{<<"name">> => <<"read_default">>, extra_key1 => <<"abc">>, <<"extrakey2">> => def},
90-
Schema = persistent_term:get({openapi_handler_schema,test_openapi}),
91-
#{inputs := [],name := <<"read_default">>,static := true,
92-
extra_key1 := <<"abc">>,<<"extrakey2">> := def
93-
} = openapi_schema:process(Json, #{type => stream_config, whole_schema => Schema, apply_defaults => true, extra_obj_key => pass}),
94-
ok.
95-
9687

9788
null_in_array(_) ->
9889
% When items are not nullable, passing null|undefined as a list element should return an error

0 commit comments

Comments
 (0)