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
Changes from 2 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
37 changes: 36 additions & 1 deletion 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,6 +42,13 @@
#{ type => binary()
, properties => property_obj()
}}.
-type parameters_definition_array() ::
#{parameter_definition_name() =>
#{ type => binary()
, items => #{ type => binary()
, properties => property_obj()
}
}}.
-export_type([parameter_definition_name/0, property_obj/0]).
Copy link
Member

Choose a reason for hiding this comment

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

Should we also export the new type? I think we should.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed


%% Swagger map spec
Expand Down Expand Up @@ -74,12 +81,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 +246,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
}
}}.