Paragon_
Paragon_

Reputation: 95

C# Swagger response schema is empty when using class from referenced project

I'm creating a web API, with the response type being from another project, added in the .csproj file. However, the model response schema seems to be empty, even though I can see the correct object being returned when stepping through the code:

Empty model response

However, if I copy and paste the class that is being returned into my own project (see below), it functions as intended:

    public class Info
    {
        public DateTime Timestamp { get; set; }

        public Dictionary<string, string> LatestInfoPerClient { get; set; }
    }

The code I'm using to create the endpoint is below (with all the logic removed):

    [HttpGet]
    [Route("get_latest_info")]
    [ProducesResponseType(StatusCodes.Status200OK, Type = typeof(Info))]
    public IActionResult GetLatestInfo()
    {
        ...

        return Ok(new Info(result));
    }

I'm very confused as to why it functions correctly when the class is from my own project, but not when it's from a referenced project.

Upvotes: 0

Views: 4101

Answers (1)

Mephisto
Mephisto

Reputation: 141

You need to make your action return a typed result:

public ActionResult<Info> GetLatestInfo()
{
    ...
    return Ok(new Info(result));
}

Upvotes: 3

Related Questions