-
Notifications
You must be signed in to change notification settings - Fork 190
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
Alternative operation specs API #265
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@moxley I'm happy to go with this, perhaps with an experimental
note, until it gets some real usage (like we did with the @doc
DSL).
@@ -0,0 +1,96 @@ | |||
defmodule OpenApiSpex.OperationBuilder do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 Moving this into its own module looks good.
lib/open_api_spex/operation_dsl.ex
Outdated
@@ -0,0 +1,57 @@ | |||
defmodule OpenApiSpex.OperationDsl do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This may still need a __using__
macro to inject the open_api_operation(action)
function into the controller?
I like the way that having a callback between the framework and user code allows the operation DSL to be changed or customized.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indeed, this will need to automatically define operation_api_operation(action)
for each action. Commit 4e1e1e9 does that now. It still does this without leveraging the __using__
macro. If we can avoid requiring a call to use ...
, that may make a slightly better API.
test/support/dsl_controller.ex
Outdated
}) | ||
end | ||
|
||
operation(:show, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This DSL looks good.
I had a play around with some other syntaxes, eg
@operation show: [
...
]
operation :show do
...
end
But I think just dropping the parens makes it feel like a module level declaration, eg:
operation :show, [
summary: "Show user",
parameters: [
id: [
in: :path,
description: "User ID",
type: :integer,
example: 1001
]
]
]
def show(conn, %{id: id}) do
json(conn, %{
data: %{
id: id,
name: "joe user",
email: "joe@gmail.com"
}
})
end
And it will be easier find the operation/2
macro in the docs vs a magic @
attribute.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree. I'll look into how to get OpenApiSpex to expose Elixir Formatter rules so that users can make one small change to the import_deps: [ ... ]
list in their .formatter.exs
file to allow optional parentheses for these macro calls.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Elixir Formatter changes: 4cfdba2
lib/open_api_spex/operation_dsl.ex
Outdated
operation_def(action, spec) | ||
end | ||
|
||
defmacro tags(tags) do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It might be useful to allow security
to be defined like tags
at the module level, and have it apply to all operations in the controller.
@mbuhot Do you have any ideas for a better name for the module The new Other names I considered for the module are |
I wasn't confident that `import` was going to work without any future problems.
I came up with |
Fixes #242
Introduces an API for operation specs that is meant as an alternative to the
@doc
tag-based operation specs API. The syntax is just as clean and easy to use as the@doc
tag-based operations specs. Supports the same shortcuts that the@doc
tag-based API supports.See test/support/dsl_controller.ex for example usage.