OData / OData/AspNetCoreOData

Incorrect output formatter selected to serialize response

Open
#462 7 comments 0 reactions 2 assignees View on GitHub

@KenitoInc is already working on this.

Since Feb 1, 2022.

investigating
Dominant language
C#
Stars
505
Forks
186
PR merge metrics
No merged PRs in 30d

Description

Hello,

I'm working through a POC with the new 8.x framework in order to update a bunch of microservices I maintain. My goal is to get all of these technologies (among others) working together:

Microsoft.AspNetCore.OData
AutoMapper.AspNetCore.OData.EFCore
Microsoft.AspNetCore.Identity.EntityFrameworkCore
Microsoft.AspNetCore.Mvc.Versioning.ApiExplorer

It looks like you've made things a lot simpler working more with the new AspNetCore routing framework instead of alongside it, so thanks for that!

I'm having a problem with how my responses are serialized. I can see in the log that the wrong output formatter is chosen to handle my response:

[17:06:30 DBG] Selected output formatter 'Microsoft.AspNetCore.Mvc.Formatters.SystemTextJsonOutputFormatter' and content type 'application/json' to write the response.

Per my understanding, this should be using the Microsoft.AspNetCore.OData.Formatter.ODataOutputFormatter and the content type should be application/json;odata.metadata=minimal;odata.streaming=true

So far, I've verified that the OdataOutputFormatter is definitely getting registered. I can see this in my log as well:

[17:06:30 DBG] List of registered output formatters, in the following order: ["Microsoft.AspNetCore.OData.Formatter.ODataOutputFormatter", "Microsoft.AspNetCore.OData.Formatter.ODataOutputFormatter", "Microsoft.AspNetCore.OData.Formatter.ODataOutputFormatter", "Microsoft.AspNetCore.Mvc.Formatters.HttpNoContentOutputFormatter", "Microsoft.AspNetCore.Mvc.Formatters.StringOutputFormatter", "Microsoft.AspNetCore.Mvc.Formatters.StreamOutputFormatter", "Microsoft.AspNetCore.Mvc.Formatters.SystemTextJsonOutputFormatter"]

I'm assuming that I'm doing something incorrect or unexpected. Here's what my controller looks like:

[ApiController]
[Route("sample/v{version:apiVersion}")]
public class UsersController : ControllerBase
{
    private ODataDbContext DbContext { get; }
    private IMapper Mapper { get; }

    public UsersController(ODataDbContext dbContext, IMapper mapper)
    {
        DbContext = dbContext;
        Mapper = mapper;
    }

    [ApiVersion("1")]
    [HttpGet("Users")]
    [ODataRouteComponent("v1")]
    [ProducesResponseType(typeof(IEnumerable<Model.V1.User>), Status200OK)]
    public async Task<IActionResult> GetV1(ODataQueryOptions<Model.V1.User> queryOptions) =>
        Ok(await DbContext.Users
            .AsNoTracking()
            .GetAsync(Mapper, queryOptions, (QuerySettings)null));

    [ApiVersion("2")]
    [HttpGet("Users")]
    [ODataRouteComponent("v2")]
    [ProducesResponseType(typeof(IEnumerable<Model.V2.User>), Status200OK)]
    public async Task<IActionResult> GetV2(ODataQueryOptions<Model.V2.User> queryOptions) =>
        Ok(await DbContext.Users
            .AsNoTracking()
            .GetAsync(Mapper, queryOptions, (QuerySettings)null));
}

Everything more or less works, but my resulting json is just an array of data instead of the standard object with @odata.{stuff} and value properties. I've been comparing my approach to those in the samples and the various blog posts, but not sure what I'm missing. If more source code or context is needed, it's no problem to publish a quick github repo for this. Any help would be appreciated, thanks!

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.