@@ -4,6 +4,7 @@ defmodule OpenApiSpex.PathItem do
44 """
55
66 alias OpenApiSpex . { Operation , Server , Parameter , PathItem , Reference }
7+
78 defstruct [
89 :"$ref" ,
910 :summary ,
@@ -29,31 +30,36 @@ defmodule OpenApiSpex.PathItem do
2930 but they will not know which operations and parameters are available.
3031 """
3132 @ type t :: % __MODULE__ {
32- "$ref": String . t | nil ,
33- summary: String . t | nil ,
34- description: String . t | nil ,
35- get: Operation . t | nil ,
36- put: Operation . t | nil ,
37- post: Operation . t | nil ,
38- delete: Operation . t | nil ,
39- options: Operation . t | nil ,
40- head: Operation . t | nil ,
41- patch: Operation . t | nil ,
42- trace: Operation . t | nil ,
43- servers: [ Server . t ] | nil ,
44- parameters: [ Parameter . t | Reference . t ] | nil
45- }
33+ "$ref": String . t ( ) | nil ,
34+ summary: String . t ( ) | nil ,
35+ description: String . t ( ) | nil ,
36+ get: Operation . t ( ) | nil ,
37+ put: Operation . t ( ) | nil ,
38+ post: Operation . t ( ) | nil ,
39+ delete: Operation . t ( ) | nil ,
40+ options: Operation . t ( ) | nil ,
41+ head: Operation . t ( ) | nil ,
42+ patch: Operation . t ( ) | nil ,
43+ trace: Operation . t ( ) | nil ,
44+ servers: [ Server . t ( ) ] | nil ,
45+ parameters: [ Parameter . t ( ) | Reference . t ( ) ] | nil
46+ }
4647
4748 @ typedoc """
4849 Represents a route from in a Plug/Phoenix application.
4950 Eg from the generated `__routes__` function in a Phoenix.Router module.
5051 """
51- @ type route :: % { verb: atom , plug: atom , opts: any }
52+ @ type route :: % {
53+ verb: atom ,
54+ path: String . t ( ) ,
55+ plug: atom ,
56+ opts: any
57+ }
5258
5359 @ doc """
5460 Builds a PathItem struct from a list of routes that share a path.
5561 """
56- @ spec from_routes ( [ route ] ) :: nil | t
62+ @ spec from_routes ( [ route ] ) :: { :ok , nil | t } | { :error , Sting . t ( ) }
5763 def from_routes ( routes ) do
5864 Enum . each ( routes , fn route ->
5965 Code . ensure_loaded ( route . plug )
@@ -64,9 +70,22 @@ defmodule OpenApiSpex.PathItem do
6470 |> from_valid_routes ( )
6571 end
6672
67- @ spec from_valid_routes ( [ route ] ) :: nil | t
68- defp from_valid_routes ( [ ] ) , do: nil
73+ @ spec from_valid_routes ( [ route ] ) :: { :ok , nil | t } | { :error , String . t ( ) }
74+ defp from_valid_routes ( [ ] ) , do: { :ok , nil }
75+
6976 defp from_valid_routes ( routes ) do
70- struct ( PathItem , Enum . map ( routes , & { & 1 . verb , Operation . from_route ( & 1 ) } ) )
77+ routes
78+ |> Enum . map ( fn route -> { route . verb , Operation . from_route ( route ) } end )
79+ |> Enum . reduce_while (
80+ % PathItem { } ,
81+ fn
82+ { verb , { :ok , operation } } , acc -> { :cont , Map . put ( acc , verb , operation ) }
83+ { _verb , { :error , reason } } , _acc -> { :halt , { :error , reason } }
84+ end
85+ )
86+ |> case do
87+ { :error , reason } -> { :error , reason }
88+ path_item = % PathItem { } -> { :ok , path_item }
89+ end
7190 end
7291end
0 commit comments