klo

klo

OpenApiSpex schema - are there any naming conventions on handling show and index routes?

Hi all,

I was wondering if there is any naming convention on handling show and index routes. Would they just fall under Request and Response?

If i have a show route for getting a car by id

would it be something like

show naming:


defmodule CarRequest do
  require OpenApiSpex
  alias OpenApiSpex.Schema
  
  OpenApiSpex.schema(%{
    title: "CarRequest",
    description: "Gets a single car by id.",
    type: :object,
    required: [:id],
    properties: %{
      id: %Schema{type: :integer, example: 12}
    }
  })
end

index naming:

defmodule CarsRequest do
  require OpenApiSpex
  alias OpenApiSpex.Schema
  
  OpenApiSpex.schema(%{
    title: "CarsRequest",
    description: "Gets all cars.",
    type: :object
  })
end

then inside of the response would it be something like

defmodule CarResponse do
  require OpenApiSpex
  alias OpenApiSpex.Schema
  
  OpenApiSpex.schema(%{
    title: "CarResponse",
    description: "Gets a single car.",
    type: :object,
    required: [:data],
    properties: %{
      data: Car.schema()
    }
  })
end

index response

defmodule CarsResponse do
  require OpenApiSpex
  alias OpenApiSpex.Schema
  
  OpenApiSpex.schema(%{
    title: "CarsResponse",
    description: "Gets all cars.",
    type: :object,
    required: [:data],
    properties: %{
      data: %Schema{type: :array, data: Car.schema()}
    }
  })
end

shared Car response:

defmodule Car do
  require OpenApiSpex
  alias OpenApiSpex.Schema
  
  OpenApiSpex.schema(%{
    title: "Car",
    description: "A single car.",
    type: :object,
    required: [:id, :make, :model],
    properties: %{
      id: %Schema{type: :integer, example: 12},
      make: %Schema{type: :string, example: "toyota"},
      model: %Schema{type: :string, example: "corolla"}
    }
  })
end

Where Next?

Popular in Questions Top

beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> somethi...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

Other popular topics Top

albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
klo
Got a question about when to concat vs. prepending items to list then reversing to achieve appending. So i know lists boil down to [1 | ...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

Latest on Elixir Forum

We're in Beta

About us Mission Statement