Skip to content
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

Merged
merged 17 commits into from
Sep 5, 2020
Merged

Alternative operation specs API #265

merged 17 commits into from
Sep 5, 2020

Conversation

moxley
Copy link
Contributor

@moxley moxley commented Aug 17, 2020

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.

defmodule MyController do
  use Phoenix.Controller
  use OpenApiSpex.ControllerSpecs

  tags ["users"]
  security [%{}, %{"petstore_auth" => ["write:pets", "read:pets"]}]

  operation :update,
    summary: "Update user",
    parameters: [
      id: [
        in: :path,
        description: "User ID",
        type: :integer,
        example: 1001
      ]
    ],
    request_body: {"User params", "application/json", UserParams},
    responses: [
      ok: {"User response", "application/json", UserResponse}
    ]

  def update(conn, %{"id" => id}) do
    json(conn, %{
      data: %{
        id: id,
        name: "joe user",
        email: "joe@gmail.com"
      }
    })
  end

@moxley moxley changed the title DSL proposal Operation specs DSL proposal Aug 17, 2020
@moxley moxley changed the title Operation specs DSL proposal Alternative operation specs API proposal Aug 17, 2020
@moxley moxley changed the title Alternative operation specs API proposal Proposal for an alternative operation specs API Aug 17, 2020
Copy link
Collaborator

@mbuhot mbuhot left a 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
Copy link
Collaborator

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.

@@ -0,0 +1,57 @@
defmodule OpenApiSpex.OperationDsl do
Copy link
Collaborator

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.

Copy link
Contributor Author

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.

})
end

operation(:show,
Copy link
Collaborator

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.

Copy link
Contributor Author

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Elixir Formatter changes: 4cfdba2

operation_def(action, spec)
end

defmacro tags(tags) do
Copy link
Collaborator

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.

@moxley
Copy link
Contributor Author

moxley commented Sep 4, 2020

@mbuhot Do you have any ideas for a better name for the module OperationDsl? The "DSL" part of the name seems unfitting because the syntax looks more like a data structure-based configuration rather than a true DSL (like PhoenixSwagger's DSL).

The new operation/2 and tags/1 macros could be moved to OpenApiSpex.Controller or to OpenApiSpex.Operation. Both those places have drawbacks, but I could be convinced otherwise.

Other names I considered for the module are OperationDefs, OperationHelpers, and ControllerOperations.

@moxley moxley marked this pull request as ready for review September 5, 2020 05:10
I wasn't confident that `import` was going to work without any future problems.
@moxley moxley changed the title Proposal for an alternative operation specs API Alternative operation specs API Sep 5, 2020
@moxley
Copy link
Contributor Author

moxley commented Sep 5, 2020

@mbuhot Do you have any ideas for a better name for the module OperationDsl? The "DSL" part of the name seems unfitting because the syntax looks more like a data structure-based configuration rather than a true DSL (like PhoenixSwagger's DSL).

The new operation/2 and tags/1 macros could be moved to OpenApiSpex.Controller or to OpenApiSpex.Operation. Both those places have drawbacks, but I could be convinced otherwise.

Other names I considered for the module are OperationDefs, OperationHelpers, and ControllerOperations.

I came up with ControllerSpecs. I like that much better than the ones previously mentioned.

@moxley moxley merged commit 0aa8249 into master Sep 5, 2020
@moxley moxley deleted the dsl-1 branch September 5, 2020 16:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

@doc attributes stripped by mix releases
2 participants