@@ -62,7 +62,10 @@ defmodule OpenAPI.Generator.Operation do
62
62
"""
63
63
@ spec names ( Operation . t ( ) ) :: [ { [ String . t ( ) ] , String . t ( ) } ]
64
64
def names ( % Operation { operation_id: id , tags: [ ] } ) do
65
- [ function | modules ] = String . replace ( id , "-" , "_" ) |> String . split ( "/" ) |> Enum . reverse ( )
65
+ [ function | modules ] =
66
+ String . replace ( id , ~r| [-_ ]+| , "_" )
67
+ |> String . split ( "/" )
68
+ |> Enum . reverse ( )
66
69
67
70
modules =
68
71
Enum . reverse ( modules )
@@ -72,10 +75,10 @@ defmodule OpenAPI.Generator.Operation do
72
75
end
73
76
74
77
def names ( % Operation { operation_id: id , tags: tags } ) do
75
- function = String . replace ( id , ~r | [-/ ]+ | , "_" ) |> Macro . underscore ( )
78
+ function = normalize_identifier ( id )
76
79
77
80
for tag <- tags do
78
- tag = String . replace ( tag , ~r | [-/ ]+ | , "_" )
81
+ tag = normalize_identifier ( tag )
79
82
camelized_tag = Macro . camelize ( tag )
80
83
underscored_tag = Macro . underscore ( tag )
81
84
@@ -85,6 +88,18 @@ defmodule OpenAPI.Generator.Operation do
85
88
end
86
89
end
87
90
91
+ defp normalize_identifier ( input ) do
92
+ input
93
+ |> String . split ( ~r| ([-/ _]+)?([A-Z]+)?([a-z]+)?| , include_captures: true , trim: true )
94
+ |> Enum . map ( fn segment ->
95
+ segment
96
+ |> String . replace ( ~r| ^[-/ _]+| , "" )
97
+ |> String . replace ( ~r| [-/ _]+$| , "" )
98
+ |> String . downcase ( )
99
+ end )
100
+ |> Enum . join ( "_" )
101
+ end
102
+
88
103
defp path_params ( state , path , % Operation { parameters: parameters } ) do
89
104
all_params =
90
105
Enum . map ( parameters , & parameter ( state , & 1 ) )
0 commit comments