kylelw23

kylelw23

Phoenix and Swagger

Hi Elixir community,
My phoenix application already set up the swagger and it’s up and running.
I specified swagger specification for /get and it works well

 swagger_path :index do
    get("/api/campaigns")
    description("List of campaigns")
    response(code(:ok), "Success")
  end

but when I tried specify swagger for /post, I got stuck at its parameter() although I followed the documents, I installed latest PhoenixSwagger from its github branch
I still can’t specify the parameter right
I tried this

swagger_path :create do
    post("/api/campaigns")
    description("Create a campaign")
    PhoenixSwagger.Path.Parameter(:name, :start_date, :end_date, :budget, :hashtags, :team_id, :description)
    response(code(:ok), "Success")
  end

with parenthesis and not with parenthesis
or this

swagger_path :create do
    post("/api/campaigns")
    description("Create a campaign")
    parameter(:name, :start_date, :end_date, :budget, :hashtags, :team_id, :description)
    response(code(:ok), "Success")
  end

and I got this error for the two above code:

lib/kernel/parallel_compiler.ex:346: anonymous fn/5 in Kernel.ParallelCompiler.spawn_workers/7

How can I get the parameter right ? Thank you

Marked As Solved

daskycodes

daskycodes

With phoenix_swagger you probably want to refer to the Swagger 2.0 Documentation. You have to define a request body as described here: Describing Request Body | Swagger Docs.

Did you check out the example they provide in the repository?

https://github.com/xerions/phoenix_swagger/blob/250d7c79459227095c0eb5c12237afb1b21b6477/examples/simple/lib/simple_web/controllers/user_controller.ex#L93

This could look like the following example

  swagger_path :create do
    post("/api/campaigns")
    description("Create a campaign")

    parameter(:campaign, :body, Schema.ref(:campaign), "Campaign Attributes")

    response(200, "Success")
  end

Additionally you have to define the :campaign schema. Here you can make use of the swagger_definitions/0 function, which you can also add into the same controller:

  def swagger_definitions do
    %{
      campaign:
        swagger_schema do
          title("Campaign")
          description("A campaign.")

          properties do
            data(
              Schema.new do
                properties do
                  name(:string, "Campaign name", required: true)

                  start_time(:string, "Start time of the campaign.",
                    required: true,
                    format: "date-time"
                  )

                  id(:string, "Auto generated ID of type binary_id", required: false)
                end
              end
            )
          end

          example(%{
            campaign: %{
              name: "Marketing 101",
              start_time: "2030-07-01T21:00:00Z"
            }
          })
        end
    }
  end

Where Next?

Popular in Discussions Top

andre1sk
A big advantage to Elixir is all the distributed goodness but for many applications running on multiple nodes having integrated Etcd, Zoo...
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New
Fl4m3Ph03n1x
Background This question comes mainly from my ignorance. Today is Black Friday, one of my favorite days of the year to buy books. One boo...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 39297 209
New
Ankhers
Just a little information upfront. Generally speaking, if I feel like I need to either break a pipe chain or use an anonymous function in...
New
tmbb
This is a post to discuss the new Phoenix LiveView functionality. From Chris’s talk, it appears that they generate all HTML on the serve...
342 18146 126
New
und0ck3d
Hello everyone! A few days ago I’ve created a topic here about how people were creating CMSs with Elixir and Phoenix. I’ve been studying...
New
Owens
Hello all, I am developing a new mobile app with Flutter frontend and Phoenix backend. The mobile app has real-time task management and c...
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
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

Other popular topics Top

danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 29377 241
New
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
Nvim
Anybody knows a comprehensive comparison of Django and Phoenix, thanks for the help. Where are they similar? Where do they differ the m...
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
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 30877 112
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
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

We're in Beta

About us Mission Statement