Skip to content

Commit cbaf0ce

Browse files
committed
Handle operations with capitalized tags
1 parent 681815b commit cbaf0ce

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

lib/open_api/generator/operation.ex

+18-3
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,10 @@ defmodule OpenAPI.Generator.Operation do
6262
"""
6363
@spec names(Operation.t()) :: [{[String.t()], String.t()}]
6464
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()
6669

6770
modules =
6871
Enum.reverse(modules)
@@ -72,10 +75,10 @@ defmodule OpenAPI.Generator.Operation do
7275
end
7376

7477
def names(%Operation{operation_id: id, tags: tags}) do
75-
function = String.replace(id, ~r|[-/ ]+|, "_") |> Macro.underscore()
78+
function = normalize_identifier(id)
7679

7780
for tag <- tags do
78-
tag = String.replace(tag, ~r|[-/ ]+|, "_")
81+
tag = normalize_identifier(tag)
7982
camelized_tag = Macro.camelize(tag)
8083
underscored_tag = Macro.underscore(tag)
8184

@@ -85,6 +88,18 @@ defmodule OpenAPI.Generator.Operation do
8588
end
8689
end
8790

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+
88103
defp path_params(state, path, %Operation{parameters: parameters}) do
89104
all_params =
90105
Enum.map(parameters, &parameter(state, &1))

test/open_api/generator/operation_test.exs

+6
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,12 @@ defmodule OpenAPI.Generator.OperationTest do
8282
operation_id: "example -op",
8383
tags: ["mod-/ name"]
8484
})
85+
86+
assert [{["ModName"], "example_op"}] =
87+
Operation.names(%Path.Operation{
88+
operation_id: "Example -OP",
89+
tags: ["Mod Name"]
90+
})
8591
end
8692

8793
test "names an operation with multiple tags" do

0 commit comments

Comments
 (0)