How to write two swagger_path functions with same names for two different endpoints

Hello everyone,
I am working on API Swagger Specification.
I need some help :slight_smile:

In Phoenix app I have in the router.ex :

get "/txs/:direction", TxController, :txs
get "/txs/:scope_type/:range", TxController, :txs

I need to implement 2 different swagger_path functions, but with the same names.

swagger_path :txs do
    get("/txs/{scope_type}/{range}")
    .................
end

swagger_path :txs do
    get("/txs/{direction}")
    .................
end

but it is not possible. I can’t change the names of the functions.
Do you know how I can manage it?

Thank you in advance for your help :slight_smile:

I don’t think it’s possible using the PhoenixSwagger DSL.

You could try defining your own function swagger_path_txs(route) and returning the appropriate operation definition based on the route.path passed in.

Hi Mike,

Thank you for your help. Could you provide an example, please :slight_smile:

Here’s a trick that might just work :slight_smile: :

# using :txs_scope_range here just to ensure it is unique 
swagger_path :txs_scope_range do
  get("/txs/{scope_type}/{range}")
  ...
end

# using :txs_direction here just to ensure it is unique
swagger_path :txs_direction do
  get("/txs/{direction}")
  ...
end 

# manually defining swagger_path_txs here, normally it would be defined by `swagger_path(:txs)`
def swagger_path_txs(route = %{path: "/txs/{direction}"}), do: swagger_path_txs_direction(route)
def swagger_path_txs(route), do: swagger_path_txs_scope_range(route)

The swagger_path_txs function will be called from the phx.swagger.generate task, which will then delegate to one of the other swagger_path_xxx functions based on the route path.

1 Like

Mike, the trick is working :smiley:

Thank you very much for your help!