Single module User for both Ecto and Open API Specs?
at hexdocs.pm/open_api_spex/3.2.0/readme.html there’s a definition of a module User
<span style="color:#323232;">defmodule MyApp.Schemas do
</span><span style="color:#323232;"> alias OpenApiSpex.Schema
</span><span style="color:#323232;">
</span><span style="color:#323232;"> defmodule User do
</span><span style="color:#323232;"> @behaviour OpenApiSpex.Schema
</span><span style="color:#323232;"> @derive [Jason.Encoder]
</span><span style="color:#323232;"> @schema %Schema{
</span><span style="color:#323232;"> title: "User",
</span><span style="color:#323232;"> description: "A user of the app",
</span><span style="color:#323232;"> type: :object,
</span><span style="color:#323232;"> properties: %{
</span><span style="color:#323232;"> id: %Schema{type: :integer, description: "User ID"},
</span><span style="color:#323232;"> name: %Schema{type: :string, description: "User name"},
</span><span style="color:#323232;"> email: %Schema{type: :string, description: "Email address", format: :email},
</span><span style="color:#323232;"> inserted_at: %Schema{type: :string, description: "Creation timestamp", format: :datetime},
</span><span style="color:#323232;"> updated_at: %Schema{type: :string, description: "Update timestamp", format: :datetime}
</span><span style="color:#323232;"> },
</span><span style="color:#323232;"> required: [:name, :email],
</span><span style="color:#323232;"> example: %{
</span><span style="color:#323232;"> "id" => 123,
</span><span style="color:#323232;"> "name" => "Joe",
</span><span style="color:#323232;"> "email" => "joe@gmail.com"
</span><span style="color:#323232;"> }
</span><span style="color:#323232;"> "x-struct": __MODULE__
</span><span style="color:#323232;"> }
</span><span style="color:#323232;"> def schema, do: @schema
</span><span style="color:#323232;"> defstruct Map.keys(@schema.properties)
</span><span style="color:#323232;"> end
</span>
In order to avoid re-describing it like that for Open API Specs whilst having already done so for Ecto, is it possible to combine the two definitions in a single module somehow? That is, my Ecto schema User would serve its purpose for Open API Specs too. With some required tweaks.
And if I do combine them, won’t this mess up with the methods of the 2 modules and other things?
Add comment