Skip to content

Commit 25d537a

Browse files
committed
fixes
1 parent 9d6e432 commit 25d537a

File tree

9 files changed

+53
-47
lines changed

9 files changed

+53
-47
lines changed

.DS_Store

-6 KB
Binary file not shown.

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
/deps
44
erl_crash.dump
55
*.ez
6+
.DS_Store

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
* Add `nullable:` option to `JsonSchema.relationship` function
99
* Handle `x-nullable` schemas in `SchemaTest`
1010
* Add `deprecated` flag for operations
11+
* Add `add_module/2` function to add path and schema definitions not in controllers.
1112

1213
# 0.8.0
1314

lib/helpers/misc_helpers.ex renamed to lib/helpers/helpers.ex

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
defmodule Helpers.MiscHelpers do
1+
defmodule PhoenixSwagger.Helpers do
22

33
def merge_definitions(definitions, swagger_map = %{definitions: existing}) do
44
%{swagger_map | definitions: Map.merge(existing, definitions)}
@@ -9,6 +9,17 @@ defmodule Helpers.MiscHelpers do
99
%{swagger_map | paths: paths}
1010
end
1111

12+
def swagger_map(swagger_map) do
13+
Map.update(swagger_map, :definitions, %{}, &(&1))
14+
|> Map.update(:paths, %{}, &(&1))
15+
end
16+
17+
def extract_args(action) do
18+
[
19+
%{verb: action |> String.to_atom, path: ""}
20+
]
21+
end
22+
1223
defp merge_conflicts(_key, value1, value2) do
1324
Map.merge(value1, value2)
1425
end

lib/mix/tasks/swagger.generate.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
defmodule Mix.Tasks.Phx.Swagger.Generate do
22
use Mix.Task
33
require Logger
4-
alias Helpers.MiscHelpers, as: Helpers
4+
alias PhoenixSwagger.Helpers, as: Helpers
55

66
@recursive true
77

@@ -130,7 +130,7 @@ defmodule Mix.Tasks.Phx.Swagger.Generate do
130130
Code.ensure_compiled?(controller) ->
131131
%{
132132
controller: controller,
133-
path: path |> format_path,
133+
path: format_path(path),
134134
swagger_fun: swagger_fun,
135135
verb: verb
136136
}

lib/phoenix_swagger.ex

Lines changed: 25 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ defmodule PhoenixSwagger do
33
use Application
44
alias PhoenixSwagger.Path
55
alias PhoenixSwagger.Path.PathObject
6-
alias Helpers.MiscHelpers, as: Helpers
6+
alias PhoenixSwagger.Helpers, as: Helpers
77

88
@moduledoc """
99
The PhoenixSwagger module provides macros for defining swagger operations and schemas.
@@ -218,62 +218,50 @@ defmodule PhoenixSwagger do
218218
|> add_module(SimpleWeb.UserSocket)
219219
220220
"""
221-
def add_module(struct, module) do
221+
def add_module(swagger_map, module) do
222222
functions = module.__info__(:functions)
223223

224-
paths = get_paths(functions, struct, module)
224+
swagger_map
225+
|> get_paths(module, functions)
226+
|> get_schemas(module, functions)
227+
end
225228

229+
defp get_schemas(swagger_map, module, functions) do
226230
Enum.map(functions, fn {action, _arg} -> build_schemas(action, module) end)
227231
|> Enum.filter(&!is_nil(&1))
228-
|> Enum.reduce(swagger_map(paths), &Helpers.merge_definitions/2)
232+
|> Enum.reduce(Helpers.swagger_map(swagger_map), &Helpers.merge_definitions/2)
229233
end
230-
231-
def build_schemas(function, module) do
234+
defp build_schemas(function, module) do
232235
if is_schema?(function), do: apply(module, function, [])
233236
end
234-
235237
defp is_schema?(function) do
236238
function
237239
|> Atom.to_string
238240
|> String.contains?("swagger_definitions")
239241
end
240242

241-
defp get_paths(functions, struct, module) do
243+
defp get_paths(swagger_map, module, functions) do
242244
Enum.map(functions, fn {action, _arg} -> build_path(action, module) end)
243245
|> Enum.filter(&!is_nil(&1))
244-
|> Enum.reduce(swagger_map(struct), &Helpers.merge_paths/2)
246+
|> Enum.reduce(Helpers.swagger_map(swagger_map), &Helpers.merge_paths/2)
245247
end
246-
247-
def swagger_map(swagger_map) do
248-
Map.update(swagger_map, :definitions, %{}, &(&1))
249-
|> Map.update(:paths, %{}, &(&1))
250-
end
251-
252-
defp build_path(function, module) do #room for improvement here for sure, comments?
253-
case is_path?(function) do
254-
{true, action} ->
255-
apply(module, function, extract_args(action))
256-
{false, _action} ->
257-
nil
248+
defp build_path(function, module) do
249+
if is_path?(function) do
250+
action =
251+
function
252+
|> get_action
253+
apply(module, function, Helpers.extract_args(action))
258254
end
259255
end
260-
261-
defp is_path?(function) do #room for improvement here for sure, comments?
262-
funct_string =
263-
function
264-
|> Atom.to_string
265-
266-
contains =
267-
funct_string
268-
|> String.contains?("swagger_path")
269-
270-
{contains, String.replace(funct_string, "swagger_path_", "")}
256+
defp is_path?(function) do
257+
function
258+
|> Atom.to_string
259+
|> String.contains?("swagger_path")
271260
end
272-
273-
defp extract_args(action) do
274-
[
275-
%{verb: action |> String.to_atom, path: ""}
276-
]
261+
defp get_action(function) do
262+
function
263+
|> Atom.to_string
264+
|> String.replace("swagger_path_", "")
277265
end
278266

279267
@doc false

mix.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ defmodule PhoenixSwagger.Mixfile do
5555
defp deps do
5656
[
5757
{:poison, "~> 2.2 or ~> 3.0"},
58-
{:ex_json_schema, "~> 0.5", optional: true},
58+
{:ex_json_schema, "~> 0.5.0", optional: true},
5959
{:plug, "~> 1.4"},
6060
{:ex_doc, "~> 0.18", only: :dev, runtime: false},
6161
{:dialyxir, "~> 0.5", only: :dev, runtime: false}

mix.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"dialyxir": {:hex, :dialyxir, "0.5.1", "b331b091720fd93e878137add264bac4f644e1ddae07a70bf7062c7862c4b952", [:mix], [], "hexpm"},
33
"earmark": {:hex, :earmark, "1.1.1", "433136b7f2e99cde88b745b3a0cfc3fbc81fe58b918a09b40fce7f00db4d8187", [:mix], [], "hexpm"},
44
"ex_doc": {:hex, :ex_doc, "0.18.3", "f4b0e4a2ec6f333dccf761838a4b253d75e11f714b85ae271c9ae361367897b7", [:mix], [{:earmark, "~> 1.1", [hex: :earmark, repo: "hexpm", optional: false]}], "hexpm"},
5-
"ex_json_schema": {:hex, :ex_json_schema, "0.5.6", "9a96edd51a00fc005e12a773a7a79e507a77855e69fcb3d9a02f7f39c3265c03", [:mix], [], "hexpm"},
5+
"ex_json_schema": {:hex, :ex_json_schema, "0.5.8", "9758444f560ebf5e1c5cb37d41a122707939f75136c7a79c4ff4ca6ad5283fb9", [:mix], [], "hexpm"},
66
"mime": {:hex, :mime, "1.2.0", "78adaa84832b3680de06f88f0997e3ead3b451a440d183d688085be2d709b534", [:mix], [], "hexpm"},
77
"plug": {:hex, :plug, "1.5.0", "224b25b4039bedc1eac149fb52ed456770b9678bbf0349cdd810460e1e09195b", [:mix], [{:cowboy, "~> 1.0.1 or ~> 1.1 or ~> 2.1", [hex: :cowboy, repo: "hexpm", optional: true]}, {:mime, "~> 1.0", [hex: :mime, repo: "hexpm", optional: false]}], "hexpm"},
88
"poison": {:hex, :poison, "3.1.0", "d9eb636610e096f86f25d9a46f35a9facac35609a7591b3be3326e99a0484665", [:mix], [], "hexpm"},

test/validator_test.exs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,16 +62,21 @@ defmodule ValidatorTest do
6262
assert :ok = Validator.validate("/get/pets/{id}", %{"id" => 1})
6363
assert {:error, :resource_not_exists} = Validator.validate("/pets", %{"id" => 1})
6464
assert {:error,
65-
[{"Required property id was not present.", "#"},
66-
{"Required property pet was not present.", "#"}], "/post/pets"} = Validator.validate("/post/pets", %{})
65+
[
66+
{"Required property id was not present.", "#"},
67+
{"Required property pet was not present.", "#"}
68+
], "/post/pets"}
69+
= Validator.validate("/post/pets", %{})
6770
assert {:error,
6871
[{"Type mismatch. Expected Integer but got String.", "#/id"},
6972
{"Required property pet was not present.", "#"}], "/post/pets"} = Validator.validate("/post/pets", %{"id" => "wrong_id"})
7073
assert {:error, "Required property pet was not present.", "#"} = Validator.validate("/post/pets", %{"id" => 1})
7174
assert {:error,
72-
[{"Required property name was not present.", "#/pet"},
73-
{"Required property tag was not present.", "#/pet"}], "/post/pets"}
74-
= Validator.validate("/post/pets", %{"id" => 1, "pet" => %{}})
75+
[
76+
{"Required property name was not present.", "#/pet"},
77+
{"Required property tag was not present.", "#/pet"}
78+
], "/post/pets"}
79+
= Validator.validate("/post/pets", %{"id" => 1, "pet" => %{}})
7580
assert {:error, "Required property tag was not present.", "#/pet"}
7681
= Validator.validate("/post/pets", %{"id" => 1, "pet" => %{"name" => "pet_name"}})
7782
assert :ok = Validator.validate("/post/pets", %{"id" => 1, "pet" => %{"name" => "pet_name", "tag" => "pet_tag"}})

0 commit comments

Comments
 (0)