earthtrip

earthtrip

OpenApiSpex - schema request validation support?

Hi all -

I’m getting back into Phoenix/Elixir after a few year hiatus and have started work on an OpenAPI based application that I would like to have schema request validation support for.
A caveat is that the schema is in YAML and due to restrictions in workflow I can’t just write it in line into the code like the OpenApiSpex README examples show. But there is support for reading the schema in from YAML at runtime which I have working.

The problem I’m running into is the docs don’t clearly describe how to leverage the OpenApiSpex struct that gets generated as if it was hand coded into the Elixir controllers/modules as it is in the documentation.

In order to rule out a potential issues the particular schema I’m using I took the venerable PetStore schema from the OpenAPI repo as a sample and created a phoenix project that uses that and reads it in and tries to use ControllerSpecs to validate it automatically.

From the documentation I created this module

defmodule PetstoreApiWeb.ApiSpec do
  @moduledoc false

  alias OpenApiSpex.{Components, Info, OpenApi, Paths, Server}
  alias PetstoreApiWeb.{Endpoint, Router}
  @behaviour OpenApi

  @impl OpenApi
  def spec do
    open_api_spec_from_yaml =
      "openapi/petstore.yaml"
      |> YamlElixir.read_all_from_file!()
      |> List.first()
      |> OpenApiSpex.OpenApi.Decode.decode()

    # open_api_spec_from_yaml
    # Discover request/response schemas from path specs
    open_api_spec_from_yaml |> OpenApiSpex.resolve_schema_modules()
  end
end

and then in my controller I have this stubbed in

defmodule PetstoreApiWeb.PetsController do
  use PetstoreApiWeb, :controller
  use OpenApiSpex.ControllerSpecs

  alias PetstoreApiWeb.Schemas.{
    Pet
  }

  plug(OpenApiSpex.Plug.CastAndValidate,
    json_render_error_v2: true
  )

  operation(:create, PetstoreApiWeb.ApiSpec.spec().paths["/pets"].post)

  def create(conn, _params) do
      text(conn, "GOT HERE")
  end

But when I run mix phx.server it fails to compile with this error

== Compilation error in file lib/petstore_api_web/controllers/pets_controller.ex ==
** (Protocol.UndefinedError) protocol Enumerable not implemented for %OpenApiSpex.Operation{tags: ["pets"], summary: "Create a pet", description: nil, externalDocs: nil, operationId: "createPets", parameters: [], requestBody: %OpenApiSpex.RequestBody{description: nil, content: %{"application/json" => %OpenApiSpex.MediaType{schema: %OpenApiSpex.Reference{"$ref": "#/components/schemas/Pet"}, example: nil, examples: nil, encoding: nil, extensions: nil}}, extensions: nil, required: true}, responses: %{"201" => %OpenApiSpex.Response{description: "Null response", headers: nil, content: nil, links: nil, extensions: nil}, "default" => %OpenApiSpex.Response{description: "unexpected error", headers: nil, content: %{"application/json" => %OpenApiSpex.MediaType{schema: %OpenApiSpex.Reference{"$ref": "#/components/schemas/Error"}, example: nil, examples: nil, encoding: nil, extensions: nil}}, links: nil, extensions: nil}}, callbacks: %{}, deprecated: false, security: nil, servers: nil, extensions: nil} of type OpenApiSpex.Operation (a struct)
    (elixir 1.15.7) lib/enum.ex:1: Enumerable.impl_for!/1
    (elixir 1.15.7) lib/enum.ex:166: Enumerable.reduce/3
    (elixir 1.15.7) lib/enum.ex:4387: Enum.reverse/1
    (elixir 1.15.7) lib/enum.ex:3702: Enum.to_list/1
    (elixir 1.15.7) lib/map.ex:224: Map.new_from_enum/1
    (open_api_spex 3.18.0) lib/open_api_spex/controller_specs.ex:378: OpenApiSpex.ControllerSpecs.operation_spec/3
    lib/petstore_api_web/controllers/pets_controller.ex:13: (module)

Which is the same error I get when I try and attempt with my API schema.

This is my router.ex as well and I’m able to successfully render the swagger docs if I remove the OpenApiSpex components from my Pets controller.

defmodule PetstoreApiWeb.Router do
  use PetstoreApiWeb, :router

  pipeline :browser do
    plug(:accepts, ["html"])
  end

  pipeline :api do
    plug(:accepts, ["json"])
    plug(OpenApiSpex.Plug.PutApiSpec, module: PetstoreApiWeb.ApiSpec)
  end

  scope "/" do
    # Use the default browser stack
    pipe_through(:browser)
    get("/swaggerui", OpenApiSpex.Plug.SwaggerUI, path: "/api/openapi")
  end

  scope "/api" do
    pipe_through(:api)
    get("/openapi", OpenApiSpex.Plug.RenderSpec, [])
    post("/pets", PetstoreApiWeb.PetsController, :create)
  end
end

If anyone can possibly steer me out of this I’d appreciate it. My use case requires that I have some form of schema validation and I just want to make sure this will work by parsing the YAML into the OpenApiSpex struct otherwise I’m going to have to go a different route. Comparing the output of the OpenApiSpex struct to what is written in the code examples I can’t see anything obviously different either so it seems like it “should work”.

Thanks for any help!

Where Next?

Popular in Questions Top

sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
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
mgjohns61585
Could someone help me? I’m making my first elixir program, number guessing game. I can’t figure out how to convert the user’s guess from ...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
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
earth10
Hi, I’m just starting to build a side-project with Elixir and Phoenix and doing some basic test with Elixir alone. What strikes me is th...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New

Other popular topics Top

malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
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 41539 114
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
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
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
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
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
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

We're in Beta

About us Mission Statement