aj-foster / open-api-generator

Open API code generator for Elixir
MIT License
101 stars 13 forks source link

normalize_identifier(tag) clobbers acronyms in module names #17

Closed feynmanliang closed 1 year ago

feynmanliang commented 1 year ago

https://github.com/aj-foster/open-api-generator/commit/cbaf0ce6d89a4cf4fad3685457abc3c72f466e19#diff-dd87d6a5f9e67976199e9a1bf044c7785e0dc97d4045794df4585b96e1808090L78-R81 changes how tags are normalized between version 0.0.3 and 0.0.4 in a backwards incompatible manner.

Expected behavior: When the actual tag is "OpenAI", I expect the camelized tag to be "OpenAI". Actual behavior: After cbaf0ce6d89a4cf4fad3685457abc3c72f466e19, the camelized tag becomes "OpenAi".

A minimal repro:

Before:

iex(4)> "OpenAI" |> String.replace(~r|[-/ ]+|, "_") |> Macro.camelize()                   
"OpenAI"

After:

iex(3)> "OpenAI" |> OpenAPI.Generator.Operation.normalize_identifier() |> Macro.camelize()
"OpenAi"

Since the tag is used for module names (https://github.com/aj-foster/open-api-generator/blob/c3ec52ffca45bf34d6f5aaa36c9dd6c05f9392ae/lib/open_api/generator/operation.ex#L20) and elixir convention is to use CamelCase for these:

Aliases, commonly used as module names, are an exception as they must be capitalized and written in CamelCase, like OptionParser.

one possible resolution could be to avoid downcaseing tag names in normalize_identifier so that the trailing "I" in "OpenAI"'s case is preserved

feynmanliang commented 1 year ago

From reading https://hexdocs.pm/elixir/1.12/Macro.html#camelize/1

If uppercase characters are present, they are not modified in any way as a mechanism to preserve acronyms:

It seems like the RC is that we are downcaseing the module names and clobbering acronyms. I'll send a PR to preserve acronyms in module names.