Single module User for both Ecto and Open API Specs?
Single module User for both Ecto and Open API Specs?
at https://hexdocs.pm/open_api_spex/3.2.0/readme.html there's a definition of a module User
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" => "joe@gmail.com"
}
"x-struct": __MODULE__
}
def schema, do: @schema
defstruct Map.keys(@schema.properties)
end
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?
0
comments