Skip to content

OkObjectResult should be changed to support better type safety #32080

Closed
@AgentEnder

Description

@AgentEnder

Summary

OkObjectResult has an inner type of object?. This proposal is to make it generic, allowing better type safety when used in conjunction with ActionResult

Motivation and goals

Currently it is possible to break type safety in a method that returns an ActionResult. See the code sample below, found in a controller.

[HttpGet()]
public ActionResult<T1> MyControllerMethod {
    T2 myResult = repository.get();
    return Ok(myResult);
}

This code not only compiles, but creates API endpoints whose swagger documentation represents T1, when the returned type is actually T2. This is clearly unintended.

The goal here is to not change the signature of the helper functions like Ok(), but to use generics and type inference to ensure that types flow through the system properly.

Risks / unknowns

Some API's could theoretically build only because of this issue. While this would not be a hard thing for developers to fix, this would be a breaking change due to some projects not building after the types are locked down.

Metadata

Metadata

Assignees

No one assigned

    Labels

    ✔️ Resolution: DuplicateResolved as a duplicate of another issueStatus: Resolvedarea-mvcIncludes: MVC, Actions and Controllers, Localization, CORS, most templatesdesign-proposalThis issue represents a design proposal for a different issue, linked in the description

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions