Skip to content

services.AddControllers().AddNewtonsoftJson() doesn't override System.Text.Json #13564

Closed
@scalablecory

Description

@scalablecory

From @Yannick183 on Wednesday, August 28, 2019 8:16:27 AM

Issue Title

services.AddControllers().AddNewtonsoftJson() doesn't seem to override System.Text.Json

General

I tried to upgrade a WebAPI project from dotnetcore 2.2 to version 3 but I'm facing some issues with Json serialization.

Using version 3.0.100-preview8-013656, I tried to override System.Text.Json by adding this to ConfigureServices

services.AddControllers().AddNewtonsoftJson(options =>
{
    options.SerializerSettings.Converters.Add(new StringEnumConverter(new CamelCaseNamingStrategy()));
    options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
});

Some of my controller endpoints fail with a HTTP500 error because of some circular references that should have been prevented by the [JsonIgnore] attribute. The error is the following :
System.Text.Json.JsonException: CurrentDepth (32) is equal to or larger than the maximum allowed depth of 32.

As you can see, the error comes from System.Text.Json.JsonException which I suspect means that Newtonsoft.Json isn't used by the controller to serialize the response.

I use ModelMetadataType to add [JsonIgnore] to some attributes like this. I even tried adding System.Text.Json.Serialization.JsonIgnore to the property but without any result.

    [ModelMetadataType(typeof(ISomeEntityClassMetadata))]
    public partial class SomeEntityClass : ISomeEntityClassMetadata
    {
    }

    public interface ISomeEntityClassMetadata
    {
        [System.Text.Json.Serialization.JsonIgnore]
        [JsonIgnore]
        SomeOtherEntityClass SomeOtherEntityNavigation { get; set; }
    }

Copied from original issue: dotnet/core#3269

Metadata

Metadata

Assignees

Labels

Needs: Author FeedbackThe author of this issue needs to respond in order for us to continue investigating this issue.area-mvcIncludes: MVC, Actions and Controllers, Localization, CORS, most templatesinvestigate

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions