-
Notifications
You must be signed in to change notification settings - Fork 187
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow defining operations by module attributes #76
Comments
@hauleth: What are the advantages you see from this approach? |
@moxley for me it is "more ergonomic" and I can reuse ExDocs as OAS documentation. But both of these are very opinionated. That is why I opened this issue, to know what other people think about such approach and whether it is worth to be submitted as a PR or not. |
@hauleth: Now that you mention it, it does seem to read more ergonomically, at least for your example. That's one possible advantage. When you say you can reuse ExDocs as OAS documentation, how would that work? ExDoc understands the |
@moxley I use |
I could parse |
@hauleth: I'm curious, why do you want to document a web API with ExDoc? Open API Spex already generates web API docs. That's a major benefit of it. |
@moxley it is not that I want to document API with ExDoc, I want to reuse the function documentation as description. These are slightly different things. |
I like this. It looks like what phoenix_swagger does. |
@hauleth Sorry for the long delay in responding to this. I appreciate the elegance of the syntax and the small benefit it brings in DRYing up endpoint/action titles in the docs. I feel it has some drawbacks:
My recommendation would be to provide this new syntax as an add-on alternative, but not as the canonical way to define operations. Your proposal reminds me of a concern I have with the current way of defining operation schemas. I would like to avoid this meta-programming: @spec open_api_operation(any) :: Operation.t
def open_api_operation(action), do: apply(__MODULE__, :"#{action}_operation", []) Also, I don't like how messy the controller gets when the operation schemas are fully defined there. The definition is considerably bigger than the action function it references. def show_operation() do
... # 10-20 lines of code
end
def show(conn, %{"id" => id}) do
{:ok, user} = MyApp.Users.find_by_id(id)
json(conn, 200, user)
end In the codebase we're working on, we've pulled out the operation schemas from the controller to separate modules. The controllers now look more like this: def open_api_operation(action), do: MyApp.Schemas.UserOperations.operation(action)
def show(conn, params) do
# ...
end
def create(conn, params) do
# ...
end This eliminates the meta-programming, and it cleans up the controller. A small downside is that there is no longer a reference to the operation next to each action function as there was before. I feel this drawback is small enough to outweigh the benefits. |
Example implementation that I have used in my project (still lacks a little bit options, but overall idea can be seen):
This causes controller to look like this:
I am opening this as an issue instead of PR as I would like to know opinions about this beforehand.
The text was updated successfully, but these errors were encountered: