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

JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
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
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
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

Other popular topics Top

Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 49266 226
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 42716 114
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

We're in Beta

About us Mission Statement