Skip to content

Commit

Permalink
Merge pull request #8 from pinx/master
Browse files Browse the repository at this point in the history
Update version, remove warnings
  • Loading branch information
msawka authored Nov 9, 2016
2 parents 7c94121 + 55f0bbd commit e4f95be
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 134 deletions.
95 changes: 45 additions & 50 deletions lib/mix/tasks/swagger.ex
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,7 @@ defmodule Mix.Tasks.Swagger do
else
swagger_path = path_from_route(String.split(route.path, "/"), nil)

path = swagger[:paths][swagger_path]
if path == nil do
path = %{}
end
path = swagger[:paths][swagger_path] || %{}

func_name = "swaggerdoc_#{route.opts}"
verb = if route.plug != nil && Keyword.has_key?(route.plug.__info__(:functions), String.to_atom(func_name)) do
Expand All @@ -141,25 +138,15 @@ defmodule Mix.Tasks.Swagger do
parse_default_verb(route.path)
end

response_schema = verb[:response_schema]
verb = Map.delete(verb, :response_schema)

verb_string = String.downcase("#{route.verb}")
if verb[:responses] == nil do
verb = Map.put(verb, :responses, default_responses(verb_string, response_schema))
end

if verb[:produces] == nil do
verb = Map.put(verb, :produces, Application.get_env(:swaggerdoc, :produces, []))
end

if verb[:operationId] == nil do
verb = Map.put(verb, :operationId, "#{route.opts}")
end

if verb[:description] == nil do
verb = Map.put(verb, :description, "")
end
response_schema = verb[:response_schema]
verb = verb
|> Map.delete(:response_schema)
|> Map.put(:responses, verb[:responses] || default_responses(verb_string, response_schema))
|> Map.put(:produces, verb[:produces] || Application.get_env(:swaggerdoc, :produces, []))
|> Map.put(:operationId, verb[:operationId] || "#{route.opts}")
|> Map.put(:description, verb[:description] || "")

path = Map.put(path, verb_string, verb)
paths = Map.put(swagger[:paths], swagger_path, path)
Expand Down Expand Up @@ -191,19 +178,17 @@ defmodule Mix.Tasks.Swagger do
if String.first(path_segment) == ":" do

#http://swagger.io/specification/#parameterObject
name = String.slice(path_segment, 1..String.length(path_segment))
#assumes all params named "id" are integers
type = if name == "id", do: "integer", else: "string"
parameter = %{
"name" => String.slice(path_segment, 1..String.length(path_segment)),
"name" => name,
"in" => "path",
"description" => "",
"required" => true,
"type" => "string"
"type" => type
}

#assumes all params named "id" are integers
if parameter["name"] == "id" do
parameter = Map.put(parameter, "type", "integer")
end

parameters ++ [parameter]
else
parameters
Expand All @@ -223,20 +208,25 @@ defmodule Mix.Tasks.Swagger do
responses = %{
"404" => %{"description" => "Resource not found"},
"401" => %{"description" => "Request is not authorized"},
"500" => %{"description" => "Internal Server Error"}
}
case verb_string do
"get" ->
response = %{"description" => "Resource Content"}
if response_schema != nil do
response = Map.put(response, "schema", response_schema)
end
Map.merge(responses, %{"200" => response})
"delete" -> Map.merge(responses, %{"204" => %{"description" => "No Content"}})
"post" -> Map.merge(responses, %{"201" => %{"description" => "Resource created"}, "400" => %{"description" => "Request contains bad values"}})
"put" -> Map.merge(responses, %{"204" => %{"description" => "No Content"}, "400" => %{"description" => "Request contains bad values"}})
_ -> responses
end
"500" => %{"description" => "Internal Server Error"} }
|> Map.merge(
case verb_string do
"get" ->
%{"200" =>
%{"description" => "Resource Content"}
|> Map.merge(if !is_nil(response_schema), do: %{"schema" => response_schema}, else: %{})}
"delete" ->
%{"204" => %{"description" => "No Content"}}
"post" ->
%{"201" => %{"description" => "Resource created"},
"400" => %{"description" => "Request contains bad values"}}
"put" ->
%{"204" => %{"description" => "No Content"},
"400" => %{"description" => "Request contains bad values"}}
_ ->
%{}
end
)
end

@doc """
Expand All @@ -246,20 +236,25 @@ defmodule Mix.Tasks.Swagger do
def build_definitions([], def_json), do: def_json
def build_definitions([code_def | remaining_defs], def_json) do
module = elem(code_def, 0)
if :erlang.function_exported(module, :__schema__, 1) do
def_json = if :erlang.function_exported(module, :__schema__, 1) do
properties_json = Enum.reduce module.__schema__(:types), %{}, fn(type, properties_json) ->
Map.put(properties_json, "#{elem(type, 0)}", convert_property_type(elem(type, 1)))
end

module_json = %{"properties" => properties_json}
|> Map.merge(
if :erlang.function_exported(module, :changeset, 2) do
module_struct = module.changeset(module.__struct__, %{})
required = required_fields module_struct.errors
%{"required" => required}
else
%{}
end
)

if :erlang.function_exported(module, :changeset, 2) do
module_struct = module.changeset(module.__struct__, %{})
required = required_fields module_struct.errors
module_json = Map.put(module_json, "required", required)
end

def_json = Map.put(def_json, "#{inspect module}", module_json)
Map.put(def_json, "#{inspect module}", module_json)
else
def_json
end

build_definitions(remaining_defs, def_json)
Expand Down
8 changes: 4 additions & 4 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ defmodule SwaggerDoc.Mixfile do
# Type `mix help deps` for more examples and options
defp deps do
[
{:phoenix, ">= 1.0.0"},
{:ecto, ">= 1.0.0"},
{:poison, "~> 1.5.0"},
{:phoenix, "~> 1.2"},
{:ecto, "~> 2.0"},
{:poison, "~> 2.0"},
{:ex_doc, "~> 0.8.4", only: :docs},
{:earmark, "~> 0.1.17", only: :docs},
{:meck, "~> 0.8.3", only: :test},
Expand All @@ -64,5 +64,5 @@ defmodule SwaggerDoc.Mixfile do
files: ~w(lib) ++
~w(LICENSE mix.exs README.md)
]
end
end
end
20 changes: 11 additions & 9 deletions mix.lock
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
%{"decimal": {:hex, :decimal, "1.1.0"},
"earmark": {:hex, :earmark, "0.1.17"},
"ecto": {:hex, :ecto, "1.0.7"},
"ex_doc": {:hex, :ex_doc, "0.8.4"},
"meck": {:hex, :meck, "0.8.3"},
"phoenix": {:hex, :phoenix, "1.0.1"},
"plug": {:hex, :plug, "1.0.0"},
"poison": {:hex, :poison, "1.5.0"},
"poolboy": {:hex, :poolboy, "1.5.1"}}
%{"decimal": {:hex, :decimal, "1.2.0", "462960fd71af282e570f7b477f6be56bf8968e68277d4d0b641a635269bf4b0d", [:mix], []},
"earmark": {:hex, :earmark, "0.1.17", "a2269e72ff85501bdb58c2de9edc0a9a17a4be2757883eed1f601b30494ed2bf", [:mix], []},
"ecto": {:hex, :ecto, "2.0.5", "7f4c79ac41ffba1a4c032b69d7045489f0069c256de606523c65d9f8188e502d", [:mix], [{:db_connection, "~> 1.0-rc.4", [hex: :db_connection, optional: true]}, {:decimal, "~> 1.1.2 or ~> 1.2", [hex: :decimal, optional: false]}, {:mariaex, "~> 0.7.7", [hex: :mariaex, optional: true]}, {:poison, "~> 1.5 or ~> 2.0", [hex: :poison, optional: true]}, {:poolboy, "~> 1.5", [hex: :poolboy, optional: false]}, {:postgrex, "~> 0.12.0", [hex: :postgrex, optional: true]}, {:sbroker, "~> 1.0-beta", [hex: :sbroker, optional: true]}]},
"ex_doc": {:hex, :ex_doc, "0.8.4", "c74a30b09627ff22a2bb7f75d3b75dec3aedb2bd434bb3009a73a40425c2315d", [:mix], [{:earmark, "~> 0.1.17 or ~> 0.2", [hex: :earmark, optional: true]}]},
"meck": {:hex, :meck, "0.8.3", "4628a1334c69610c5bd558b04dc78d723d8ec5445c123856de34c77f462b5ee5", [:rebar], []},
"mime": {:hex, :mime, "1.0.1", "05c393850524767d13a53627df71beeebb016205eb43bfbd92d14d24ec7a1b51", [:mix], []},
"phoenix": {:hex, :phoenix, "1.2.1", "6dc592249ab73c67575769765b66ad164ad25d83defa3492dc6ae269bd2a68ab", [:mix], [{:cowboy, "~> 1.0", [hex: :cowboy, optional: true]}, {:phoenix_pubsub, "~> 1.0", [hex: :phoenix_pubsub, optional: false]}, {:plug, "~> 1.1", [hex: :plug, optional: false]}, {:poison, "~> 1.5 or ~> 2.0", [hex: :poison, optional: false]}]},
"phoenix_pubsub": {:hex, :phoenix_pubsub, "1.0.1", "c10ddf6237007c804bf2b8f3c4d5b99009b42eca3a0dfac04ea2d8001186056a", [:mix], []},
"plug": {:hex, :plug, "1.2.2", "cfbda521b54c92ab8ddffb173fbaabed8d8fc94bec07cd9bb58a84c1c501b0bd", [:mix], [{:cowboy, "~> 1.0", [hex: :cowboy, optional: true]}, {:mime, "~> 1.0", [hex: :mime, optional: false]}]},
"poison": {:hex, :poison, "2.2.0", "4763b69a8a77bd77d26f477d196428b741261a761257ff1cf92753a0d4d24a63", [:mix], []},
"poolboy": {:hex, :poolboy, "1.5.1", "6b46163901cfd0a1b43d692657ed9d7e599853b3b21b95ae5ae0a777cf9b6ca8", [:rebar], []}}
Loading

0 comments on commit e4f95be

Please sign in to comment.