NAR

NAR

Openapispex does not send data in POST body

I’m getting to know OpenApiSpex. I try to create a function on a REST API with two parameters and I’d like to pass the parameters in the body of the POST requests (I think this is how this stuff is usually used). So far I’ve came up with this:

  def login_operation() do
    %Operation{
      tags: ["users"],
      summary: "Logs in the user",
      description: "Log in the user",
      operationId: "LoginController.login",
      parameters: [
        Operation.parameter(:username, :path, :string, "User Name",
          example: "test",
          required: true
        ),
        Operation.parameter(:number, :path, :integer, "Some number",
          example: 12,
          required: true
        )
      ],
      responses: %{
        200 => Operation.response("User", "application/json", UserWeb.LoginResponse)
      }
    }
  end

My problem is that on the local Swagger UI (http://localhost:4000/swaggerui#/users/LoginController.login) if I press “Try it out”, then “execute”, the “username” and “number” are not added to the body. I tried to use “:query” for location (instead of “:path”), but in that case the parameters were included in the URI. How should I configure the parameters that the Swagger UI put them into the body?

Marked As Solved

NAR

NAR

I only found a workaround: instead of using parameters in the Operation map, I created a whole new schema(?) and use that in the requestBody field of the Operation map. So I have this new module:

defmodule MerlinWeb.LoginParameter do
require OpenApiSpex

  OpenApiSpex.schema(%{
    title: "LoginParameter",
    description: "Input for login",
    type: :object,
    properties: %{
      username: %OpenApiSpex.Schema{type: :string, description: "name of the user"},
      some_number: %OpenApiSpex.Schema{type: :integer, description: "some number"}
    },
    required: [:username, :some_number],
    example: %{
        "username" => "some user",
        "some_number" => "12"
    }
  })
end

and in the above example instead of the parameters: part I have this:

      parameters: [],
      requestBody: Operation.request_body("Login params description", "application/json",
          MerlinWeb.LoginParameter,
          required: true),

It means that on the Swagger UI I don’t have a separate text field for all parameters anymore (username, number in the above examples), but one single text area for the JSON input. Doesn’t look that good, but works.

Also Liked

mbuhot

mbuhot

Your solution is correct. Swagger 3 separates the requestBody from the parameters, which allows for defining multiple schemas for different content types.

To get a better editing experience, some customisation of swagger-ui might be required. I found this comment which mentions using react-jsonschema-form to edit the json body.

Where Next?

Popular in Questions Top

Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lists...
New
johnnyicon
Hi all, I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I’m trying to use Postgres...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
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
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New

Other popular topics Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
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
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
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

We're in Beta

About us Mission Statement