Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
roberto-mardeni opened this issue Aug 6, 2018 · 6 comments
Assignees

Comments

@roberto-mardeni
Copy link

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 JamesNK/Newtonsoft.Json#1772

@kevinwedwards
Copy link

This is impacting us too.

@alvaromongon
Copy link

Is this still hapenning?

@fabiocav
Copy link
Member

@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 fabiocav added this to the Active Questions milestone Mar 20, 2019
@fabiocav fabiocav self-assigned this Mar 20, 2019
@fabiocav
Copy link
Member

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

@roberto-mardeni
Copy link
Author

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

@abhiphirke
Copy link

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!

@ghost ghost locked as resolved and limited conversation to collaborators Jan 1, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants