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

New function add_definition_array/2 in cowboy_swagger #118

Merged
merged 4 commits into from
Dec 16, 2019
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
43 changes: 41 additions & 2 deletions src/cowboy_swagger.erl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
-module(cowboy_swagger).

%% API
-export([to_json/1, add_definition/2, schema/1]).
-export([to_json/1, add_definition/2, add_definition_array/2, schema/1]).

%% Utilities
-export([enc_json/1, dec_json/1]).
Expand Down Expand Up @@ -42,7 +42,18 @@
#{ type => binary()
, properties => property_obj()
}}.
-export_type([parameter_definition_name/0, property_obj/0]).
-type parameters_definition_array() ::
#{parameter_definition_name() =>
#{ type => binary()
, items => #{ type => binary()
, properties => property_obj()
}
}}.
-export_type([ parameter_definition_name/0
, property_obj/0
, parameters_definitions/0
, parameters_definition_array/0
]).

%% Swagger map spec
-opaque swagger_map() ::
Expand Down Expand Up @@ -74,12 +85,27 @@ to_json(Trails) ->
SwaggerSpec = create_swagger_spec(GlobalSpec, SanitizeTrails),
enc_json(SwaggerSpec).

-spec add_definition_array( Name::parameter_definition_name()
, Properties::property_obj()
) ->
ok.
add_definition_array(Name, Properties) ->
DefinitionArray = build_definition_array(Name, Properties),
add_definition(DefinitionArray).

-spec add_definition( Name::parameter_definition_name()
, Properties::property_obj()
) ->
ok.
add_definition(Name, Properties) ->
Definition = build_definition(Name, Properties),
add_definition(Definition).

-spec add_definition( Definition :: parameters_definitions()
| parameters_definition_array()
) ->
ok.
add_definition(Definition) ->
CurrentSpec = application:get_env(cowboy_swagger, global_spec, #{}),
ExistingDefinitions = maps:get(definitions, CurrentSpec, #{}),
NewSpec = CurrentSpec#{definitions => maps:merge( ExistingDefinitions
Expand Down Expand Up @@ -224,3 +250,16 @@ build_definition(Name, Properties) ->
#{Name => #{ type => <<"object">>
, properties => Properties
}}.

%% @private
-spec build_definition_array( Name::parameter_definition_name()
, Properties::property_obj()
) ->
parameters_definition_array().
build_definition_array(Name, Properties) ->
#{Name => #{ type => <<"array">>
, items => #{ type => <<"object">>
, properties => Properties
}
}}.

99 changes: 74 additions & 25 deletions test/cowboy_swagger_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@

-export([all/0]).
-export([ to_json_test/1
, add_definition_test/1]).
, add_definition_test/1
, add_definition_array_test/1
]).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Common test
Expand Down Expand Up @@ -136,43 +138,65 @@ to_json_test(_Config) ->
-spec add_definition_test(Config::cowboy_swagger_test_utils:config()) ->
{comment, string()}.
add_definition_test(_Config) ->
%%
%% Given
%%
ct:comment("Add first definition"),
Name1 = <<"CostumerDefinition">>,
Properties1 =
#{ <<"first_name">> =>
#{ type => <<"string">>
, description => <<"User first name">>
, example => <<"Pepito">>
}
, <<"last_name">> =>
#{ type => <<"string">>
, description => <<"User last name">>
, example => <<"Perez">>
}
},
ok = cowboy_swagger:add_definition(Name1, Properties1),
Properties1 = test_properties_one(),

ct:comment("Add second definition"),
Name2 = <<"CarDefinition">>,
Properties2 =
#{ <<"brand">> =>
#{ type => <<"string">>
, description => <<"Car brand">>
}
, <<"year">> =>
#{ type => <<"string">>
, description => <<"Production time">>
, example => <<"1995">>
}
},
Properties2 = test_properties_two(),

%%
%% When
%%
ok = cowboy_swagger:add_definition(Name1, Properties1),
ok = cowboy_swagger:add_definition(Name2, Properties2),

%%
%% Then
%%
{ok, SwaggerSpec1} = application:get_env(cowboy_swagger, global_spec),
JsonDefinitions = maps:get(definitions, SwaggerSpec1),
true = maps:is_key(Name1, JsonDefinitions),
true = maps:is_key(Name2, JsonDefinitions),

{comment, ""}.

-spec add_definition_array_test(Config::cowboy_swagger_test_utils:config()) ->
{comment, string()}.
add_definition_array_test(_Config) ->
%%
%% Given
%%
ct:comment("Add first definition"),
Name1 = <<"CostumerDefinition">>,
Properties1 = test_properties_one(),

ct:comment("Add second definition"),
Name2 = <<"CarDefinition">>,
Properties2 = test_properties_two(),

%%
%% When
%%
ok = cowboy_swagger:add_definition_array(Name1, Properties1),
ok = cowboy_swagger:add_definition_array(Name2, Properties2),

%%
%% Then
%%
{ok, SwaggerSpec1} = application:get_env(cowboy_swagger, global_spec),
JsonDefinitions = maps:get(definitions, SwaggerSpec1),
true = maps:is_key(items, maps:get(Name1, JsonDefinitions)),
true = maps:is_key(items, maps:get(Name2, JsonDefinitions)),
<<"array">> = maps:get(type, maps:get(Name1, JsonDefinitions)),
<<"array">> = maps:get(type, maps:get(Name2, JsonDefinitions)),

{comment, ""}.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Internal functions
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Expand Down Expand Up @@ -260,3 +284,28 @@ test_trails() ->
trails:trail("/a/:b/[:c]", handler2, [], Metadata),
trails:trail("/a", handler3, [], Metadata1)|
cowboy_swagger_handler:trails()].

test_properties_one() ->
#{ <<"first_name">> =>
#{ type => <<"string">>
, description => <<"User first name">>
, example => <<"Pepito">>
}
, <<"last_name">> =>
#{ type => <<"string">>
, description => <<"User last name">>
, example => <<"Perez">>
}
}.

test_properties_two() ->
#{ <<"brand">> =>
#{ type => <<"string">>
, description => <<"Car brand">>
}
, <<"year">> =>
#{ type => <<"string">>
, description => <<"Production time">>
, example => <<"1995">>
}
}.