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.

mix.exs

  defp deps do
    [
      ...
      {:cors_plug, "~> 2.0"}
    ]
  end

router.ex

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

  scope "/api", AnalyticsApiWeb do
    pipe_through :api

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

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
    endpoint.static_url

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

    # lib/analytics_api_web/router.ex:1
    is_atom(endpoint)

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

    # lib/analytics_api_web/router.ex:1
    endpoint.static_url

HINT: "var.field" (without parentheses) implies "var" is a map() while "var.fun()" (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.

1 Like

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

1 Like

Thanks - I’ll give it a try!

Any idea why mix phx.new --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.

Updating for the version 1.5.4 worked for me, was 1.5.1. What I made:

  • In mix.exs under function deps change the version of phoenix
  • Run mix deps.get phoenix to download the new version
  • Run mix deps.compile phoenix to compile the new version

Thanks for the help