-
Notifications
You must be signed in to change notification settings - Fork 150
Description
This was reproduced using the out-of-the-box WeatherForecast api, that Microsoft gives you as a template, when creating a new api in Visual Studio 2022. NLog successfully deserializes an object that is instantiated and initialized normally. It fails to deserialize, it seems, when the object is created through a Newtonsoft deserialization.
UseNlog is called off of the Host as specified in the wiki. The code
using APITestApp.ErrorResponses;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
namespace APITestApp.Controllers
{
[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
{
private static readonly string[] Summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};
private const string SampleAPIError = @" {
""apiVersion"": ""/prices/v3"",
""error"": {
""code"": ""400"",
""message"": ""OASValidation OpenAPI-Spec-Validation-Domestic-Prices with resource oas://domestic-prices-v3.yaml: failed with reason: [ERROR - Instance failed to match all required schemas (matched only 1 out of 2): [/allOf/0: Object has missing required properties ([height,length,width])]ERROR - Object has missing required properties ([height,length,width]): []]"",
""errors"": [
{
""title"": ""OASValidation OpenAPI-Spec-Validation-Domestic-Prices with resource 'oas: //domestic-prices-v3.yaml': failed with reason: '[ERROR - Instance failed to match all required schemas (matched only 1 out of 2): [/allOf/0: Object has missing required properties ([height,length,width])] ERROR - Object has missing required properties ([height,length,width]): []],detail:}] } }'""
}
]
}
}";
private readonly ILogger<WeatherForecastController> _logger;
public WeatherForecastController(ILogger<WeatherForecastController> logger)
{
_logger = logger;
}
[HttpGet]
public IEnumerable<WeatherForecast> Get()
{
var wf = Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
TemperatureC = Random.Shared.Next(-20, 55),
Summary = Summaries[Random.Shared.Next(Summaries.Length)]
})
.ToArray();
_logger.LogDebug("GET method {@wf}", wf);
//
var errorObject = JsonConvert.DeserializeObject<APIError>(SampleAPIError);
_logger.LogDebug("GET method with error: {@errorObject}", errorObject);
return wf;
}
[HttpPost]
public string ReturnApiError(APIError error)
{
return "ReturnApiError";
}
}
}then
outputs
2025-11-28 15:35:04.9641|0|DEBUG|APITestApp.Controllers.WeatherForecastController|GET method {"Date":"11/29/2025", "TemperatureC":25, "TemperatureF":76, "Summary":"Hot"} 2025-11-28 15:35:05.1951|0|DEBUG|APITestApp.Controllers.WeatherForecastController|GET method with error: "APITestApp.ErrorResponses.APIError"
ErrorResponses model attached.