Skip to content

Commit

Permalink
Merge pull request #118 from brucify/add_definition_array
Browse files Browse the repository at this point in the history
New function add_definition_array/2 in cowboy_swagger
  • Loading branch information
elbrujohalcon authored Dec 16, 2019
2 parents d16034d + 06fb386 commit 5c4cd20
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 27 deletions.
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">>
}
}.

0 comments on commit 5c4cd20

Please sign in to comment.