Ecto 和 Open API Specs 的单模块用户?

Single module User for both Ecto and Open API Specs?

提问人:mandiromi 提问时间:11/14/2023 更新时间:11/14/2023 访问量:15

问:

https://hexdocs.pm/open_api_spex/3.2.0/readme.html 有一个模块的定义 用户

defmodule MyApp.Schemas do
  alias OpenApiSpex.Schema

  defmodule User do
    @behaviour OpenApiSpex.Schema
    @derive [Jason.Encoder]
    @schema %Schema{
      title: "User",
      description: "A user of the app",
      type: :object,
      properties: %{
        id: %Schema{type: :integer, description: "User ID"},
        name:  %Schema{type: :string, description: "User name"},
        email: %Schema{type: :string, description: "Email address", format: :email},
        inserted_at: %Schema{type: :string, description: "Creation timestamp", format: :datetime},
        updated_at: %Schema{type: :string, description: "Update timestamp", format: :datetime}
      },
      required: [:name, :email],
      example: %{
        "id" => 123,
        "name" => "Joe",
        "email" => "[email protected]"
      }
      "x-struct": __MODULE__
    }
    def schema, do: @schema
    defstruct Map.keys(@schema.properties)
  end

为了避免像 Open API Specs 那样重新描述它,而已经为 Ecto 这样做了,是否有可能以某种方式将这两个定义组合在一个模块中?也就是说,我的 Ecto 模式用户也将用于 Open API Specs。进行一些必要的调整。

如果我将它们组合在一起,这不会弄乱 2 个模块的方法和其他事情吗?

Swagger 长生不老药 Swagger-UI OpenAPI 凤凰框架

评论


答: 暂无答案