Azure / azure-functions-host

The host/runtime that powers Azure Functions
https://functions.azure.com
MIT License
1.92k stars 442 forks source link

StringEnumConverter in Newtonsoft.Json 11.0.2 does not work as in v9.0.1 for Azure Functions #3228

Closed roberto-mardeni closed 5 years ago

roberto-mardeni commented 6 years ago

Using the StringEnumConverter does not result in enums serialized as strings in Azure Functions for the latest version of Newtonsoft.Json, but it does work for v9.0.1

Source/destination types

using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

namespace Data.Models
{
    [JsonConverter(typeof(StringEnumConverter))]
    public enum KnowledgeType
    {
        Web,
        People,
        Video,
        Document,
        Images
    }
}
namespace Data.Models
{
    public class SearchResult
    {
        public string Title { get; set; }
        public string Url { get; set; }
        public string ImageUrl { get; set; }
        public KnowledgeType Type { get; set; }
        public string Excerpt { get; set; }
        public string Author { get; set; }
        public int? Rating { get; set; }
        public int Shared { get; set; }
        public bool Featured { get; set; }
        public int Comments { get; set; }
    }
}

Source/destination JSON

[
    {
        "Title": "Unexpected Partners Join Forces for the Future",
        "Url": "url ...",
        "ImageUrl": "url ...",
        "Type": "Document",
        "Excerpt": "excerpt ...",
        "Author": "author",
        "Rating": 4,
        "Shared": 12,
        "Featured": true,
        "Comments": 1
    }
]

Expected behavior

Type should be serialized as string, in this example "Document"

Actual behavior

[
    {
        "Title": "Unexpected Partners Join Forces for the Future",
        "Url": "url ...",
        "ImageUrl": "url ...",
        "Type": 3,
        "Excerpt": "excerpt ...",
        "Author": "author",
        "Rating": 4,
        "Shared": 12,
        "Featured": true,
        "Comments": 1
    }
]

Steps to reproduce

// Azure Function Sample
using System;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using Data.Models;
using Models.Repository;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Azure.WebJobs.Host;

namespace Api
{
    public static class SearchKnowledge
    {
        [FunctionName("SearchKnowledge")]
        public static async Task<HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Function, "get", Route = "search")]HttpRequestMessage req, TraceWriter log)
        {
            var results = new List<SearchResult> {
                new SearchResult { 
                    Title = "Unexpected Partners Join Forces for the Future",
                    Url = "url ...",
                    ImageUrl = "url ...",
                    Type = 3,
                    Excerpt = "excerpt ...",
                    Author = "author",
                    Rating = 4,
                    Shared = 12,
                    Featured = true,
                    Comments = 1
                }
            };
            return req.CreateResponse(HttpStatusCode.OK, results);
        }
    }
}

Using version 9.0.1 works, upgrading to any version after that does not.

I tested similar code in Console Apps in both .Net Framework & .Net Core and the problem does not manifest there, only in Azure Functions.

Moved from https://github.com/JamesNK/Newtonsoft.Json/issues/1772

kevinwedwards commented 5 years ago

This is impacting us too.

alvaromongon commented 5 years ago

Is this still hapenning?

fabiocav commented 5 years ago

@roberto-mardeni @kevinwedwards is this still impacting you with the current production version of 2.0? That version doesn't use 9.0.1, so I wouldn't expect this to be an issue there. Can you confirm?

fabiocav commented 5 years ago

Closing as no-response. This should not impact 2.0, but do let us know if you continue to experience issues.

roberto-mardeni commented 5 years ago

Will have to try it again, haven't done so in a while.

abhiphirke commented 4 years ago

Hi... if we are still paying attention to this, I am facing exactly same issue when I ported my 2.2 Web Project to 3.0. Anyone has any pointer to fixing this, please share!