Skip to content

fix(hero): run result handlers when handler returns nil (#2534) - #2609

Closed
rifkir23 wants to merge 1 commit into
kataras:mainfrom
rifkir23:fix/hero-result-handler-nil
Closed

rifkir23 wants to merge 1 commit into
kataras:mainfrom
rifkir23:fix/hero-result-handler-nil

Conversation

@rifkir23

@rifkir23 rifkir23 commented Sep 6, 2026

Copy link
Copy Markdown

Description

A handler registered through the hero/MVC container that returns a nil value does not run the configured result handler chain. UseResultHandler handlers are silently skipped for nil returns:

app.ConfigureContainer().UseResultHandler(func(next iris.ResultHandler) iris.ResultHandler {
    return func(ctx iris.Context, v interface{}) error {
        // never called when the controller returns nil
        return next(ctx, v)
    }
})

func (c *CustomController) Get() any { return nil }

Fixes #2534

Root cause

In hero/func_result.go, dispatchCommon only invokes the result handler chain when the value is non-nil:

if v != nil {
    return handler(ctx, v)
}
// v == nil: flush headers/content, handler chain skipped

So when a handler returns nil, custom stays nil and the UseResultHandler chain never runs.

Fix

When the container has custom result handlers registered, run the handler chain even for a nil value — a nil return is a legitimate case a custom handler may want to observe or replace. Behavior without custom handlers is unchanged: the default handler is still not invoked on nil, so nil returns keep flushing headers/content exactly as before. The flag is threaded from the call site (len(c.resultHandlers) > 0); Response.Dispatch passes false.

Tests

Added TestContainerUseResultHandlerOnNil: a handler returning nil with a registered UseResultHandler now runs the handler (which replaces the nil result), and the response reflects the replacement. Verified it fails before the change ("result handler was not called for a nil return value") and passes after. Full hero suite passes; gofmt and go vet clean.

@rifkir23
rifkir23 requested a review from kataras as a code owner September 6, 2026 12:45
@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@kataras

kataras commented Nov 12, 2026

Copy link
Copy Markdown
Owner

Thanks for this, and for the test that came with it.

main is v14 now and the hero package is not part of it. That is why GitHub shows this branch as conflicting: hero/func_result.go and hero/handler.go no longer exist there.

I checked whether #2534 can still happen on main, and it cannot. v14 has no UseResultHandler chain, so there is nothing left to skip when a handler returns nil. A typed handler returns (Response, error) and the value always reaches the response encoder, nil included (controller/mvc/handler/handler.go). The migration guide has the section on what replaced hero: https://github.com/kataras/iris/blob/main/MIGRATION.md#hero-is-replaced-by-the-built-in-di-container

Your diagnosis is correct for v12. The v12 branch still has the if v != nil guard in dispatchCommon (hero/func_result.go:366), so the chain is skipped there exactly as you describe. I am not merging this into main because the code it patches is gone.

@kataras

kataras commented Nov 12, 2026

Copy link
Copy Markdown
Owner

@rifkir23 @haoval this changed in v14. hero and its UseResultHandler chain are gone, replaced by the built-in DI container and typed handlers that pass every return value to the response encoder, nil included. Please upgrade to v14:

go get github.com/kataras/iris/v14@latest

The Migration Guide takes a v12 application through it, and has a section on what replaced hero.

@kataras kataras closed this Nov 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] MVC UseResultHandler does not handle nil

4 participants