Usage of Kemal's add_context_storage_type

Hello, I’m wondering if there are any Kemal users here that wouldn’t mind helping me understand why my usage of add_context_storage_type (link) is not working.

I have a method that configures my Kemal server with the following lines:

add_context_storage_type TenantModel
add_context_storage_type UserResponse
add_handler AuthMiddleware.new

Inside of AuthMiddleware, my goal is to have instances of the TenantModel and UserResponse that I can pass to routes using env.set "user", user for example.

Inside of my middleware, I get this error when trying to set a value with my custom type.

api    | in src/app/middleware/auth.cr:17: no overload matches 'HTTP::Server::Context#set' with types String, UserResponse
api    | Overloads are:
api    |  - HTTP::Server::Context#set(name : String, value : StoreTypes)
api    |
api    |     env.set "user", user

I am digging through Kemal source code with not much luck. I see what the macro for add_context_storage_type is doing, but I don’t see why the new type is not included in overloads for #set.

I did notice that there is a note in the Kemal guide on using add_context_storage_type that mentions I must define the type first, but I am fairly certain it is being defined before referenced in my middleware.

What’s your Crystal and Kemal version?

Also, could you please try calling add_context_storage_type inside your AuthMiddleware middleware?

Hi @sdogruyol, I’m using 0.27.2 of crystal and 0.25.2 of kemal.

Also, I tried moving my call to add_context_storage_type like you suggested, and the compiler error is gone.

If this seems like an issue with Kemal I can open an issue on GitHub.