CORS Error - warning: incompatible types

I created a Phoenix API (no ecto, no webpack, no html). Works great.

I needed to add CORS functionality to the API so that another URL can access the API.


  defp deps do
      {:cors_plug, "~> 2.0"}


  pipeline :api do
    plug CORSPlug, origin: "*"
    plug CORSPlug, send_preflight_response?: false
    plug :accepts, ["json"]

  scope "/api", AnalyticsApiWeb do
    pipe_through :api

    get "/chart/*path", AnalyticsController, :get_chart
    options "/chart/*path", AnalyticsController, :options

CORS is working fine, but when the application is compiling/building, I get this error which only appeared when I added the above CORS code:

warning: incompatible types:

    map() !~ atom()

in expression:

    # lib/analytics_api_web/router.ex:1

where "endpoint" was given the type atom() in:

    # lib/analytics_api_web/router.ex:1

where "endpoint" was given the type map() (due to calling var.field) in:

    # lib/analytics_api_web/router.ex:1

HINT: "var.field" (without parentheses) implies "var" is a map() while "" (with parentheses) implies "var" is an atom()

Conflict found at
  lib/analytics_api_web/router.ex:1: AnalyticsApiWeb.Router.Helpers.static_url/2

What did I do wrong?

I was getting the same warning. But, Without cors_plug. Did you get any solutions.

I got the solution for this. You have to update phoenix version 1.5.4 or more

Thanks - I’ll give it a try!

Any idea why mix --version reports Phoenix v1.4.12 and mix phx reports Phoenix v1.4.17?

It looks like origin should be a list, i.e.

 plug CORSPlug, origin: ["*"]

Thanks Serge, I’ll give it a try.