Skip to content
This repository has been archived by the owner on Dec 14, 2018. It is now read-only.

JsonInputFormatter doesn't log errors #3557

Closed
tugberkugurlu opened this issue Nov 14, 2015 · 4 comments
Closed

JsonInputFormatter doesn't log errors #3557

tugberkugurlu opened this issue Nov 14, 2015 · 4 comments
Assignees
Milestone

Comments

@tugberkugurlu
Copy link
Contributor

I am on MVC 6.0.0-beta8 and I had a JSON deserialization failure. It didn't log any errors but instead it just didn't bind anything and populated the model state. This is the response I had:

51
{"startedAt":["The input was not valid."],"endedAt":["The input was not valid."]}
0

When I tried the same deserialization in isolation, I got the below error from JsonConvert.DeserializeObject<T>:

An unhandled exception of type 'NodaTime.Utility.InvalidNodaDataException' occurred in Newtonsoft.Json.dll

Additional information: Unexpected token parsing Instant. Expected String, got Date.

I would expect to see this as a Debug level log so that I wouldn't need to dig deep to see what is going wrong. Would be also useful to log more info along with the error like what is the value, etc.

@tugberkugurlu
Copy link
Contributor Author

This is the code I tried to get the error:

using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using NodaTime;
using NodaTime.Serialization.JsonNet;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NodaTimeJson
{
    class Program
    {
        static void Main(string[] args)
        {
            var json = GetContent();
            var serializerSettings = new JsonSerializerSettings();
            serializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
            serializerSettings.Converters.Add(NodaConverters.InstantConverter);

            var trip = JsonConvert.DeserializeObject<TripCreationModel>(json, serializerSettings);
        }

        private static string GetContent()
        {
            using (var stream = File.OpenRead(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "file.json")))
            using (var reader = new StreamReader(stream))
            {
                return reader.ReadToEnd();
            }
        }

        public class TripCreationModel
        {
            public string Name { get; set; }
            public Instant StartedAt { get; set; }
            public Instant EndedAt { get; set; }
        }
    }
}

You need to install NodaTime.Serialization.JsonNet 1.3.1 NuGet package and file.json has the below content:

{
  "name": "Foo",
  "startedAt": "2015-11-14T12:27:17.389Z",
  "endedAt": "2015-11-14T12:27:45.206Z"
}

For people who are interested in the solution to this error: setting serializerSettings.DateParseHandling to DateParseHandling.None solves the problem.

@Eilon
Copy link
Member

Eilon commented Nov 17, 2015

@tugberkugurlu we added a ton of logging code to MVC in RC1. Could you try the latest builds and see if you get back something more reasonable?

@rynowak
Copy link
Member

rynowak commented Nov 17, 2015

I can confirm that we didn't do anything like what what @tugberkugurlu is asking for in RC1 😦

@Eilon Eilon added this to the 6.0.0-rc2 milestone Nov 17, 2015
@Eilon
Copy link
Member

Eilon commented Nov 17, 2015

A flipped my Ryan coin and it landed on @ryanbrandenburg .

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants