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

Fix issue #587: AddODataNewtonsoftJson should call AddODataNewtonsoftJson with null #589

Merged
merged 1 commit into from
May 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public static IMvcBuilder AddODataNewtonsoftJson(this IMvcBuilder builder,
/// <returns>The <see cref="IMvcCoreBuilder"/>.</returns>
public static IMvcCoreBuilder AddODataNewtonsoftJson(this IMvcCoreBuilder builder)
{
return builder.AddNewtonsoftJson(null);
return builder.AddODataNewtonsoftJson(null);
}

/// <summary>
Expand Down
10 changes: 10 additions & 0 deletions src/Microsoft.AspNetCore.OData/Microsoft.AspNetCore.OData.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14056,6 +14056,16 @@
<param name="parameterMappings">The parameter mappings.</param>
<returns>True/False.</returns>
</member>
<member name="M:Microsoft.AspNetCore.OData.Routing.Template.SegmentTemplateHelpers.GetNavigationSourceFromEdmOperation(Microsoft.OData.Edm.IEdmModel,Microsoft.OData.Edm.IEdmOperation)">
<summary>
Gets the navigation source from an Edm operation.
</summary>
<param name="model">The Edm model.</param>
<param name="operation">The Edm operation.</param>
<returns>
The navigation source or null if the annotation indicating the mapping from an Edm operation to an entity set is not found.
</returns>
</member>
<member name="T:Microsoft.AspNetCore.OData.Routing.Template.SingletonSegmentTemplate">
<summary>
Represents a template that could match an <see cref="T:Microsoft.OData.Edm.IEdmSingleton"/>.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
//-----------------------------------------------------------------------------
// <copyright file="ODataNewtonsoftJsonMvcBuilderExtensionsTests.cs" company=".NET Foundation">
// Copyright (c) .NET Foundation and Contributors. All rights reserved.
// See License.txt in the project root for license information.
// </copyright>
//------------------------------------------------------------------------------

using System;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ApplicationParts;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Xunit;

namespace Microsoft.AspNetCore.OData.NewtonsoftJson.Tests
{
public class ODataNewtonsoftJsonMvcBuilderExtensionsTests
{
[Fact]
public void AddODataNewtonsoftJson_OnIMvcBuilder_Throws_NullBuilder()
{
// Arrange
IMvcBuilder builder = null;

// Act & Assert
Assert.Throws<ArgumentNullException>("builder", () => builder.AddODataNewtonsoftJson());
}

[Fact]
public void AddODataNewtonsoftJson_OnIMvcCoreBuilder_Throws_NullBuilder()
{
// Arrange
IMvcCoreBuilder builder = null;

// Act & Assert
Assert.Throws<ArgumentNullException>("builder", () => builder.AddODataNewtonsoftJson());
}

[Fact]
public void AddODataNewtonsoftJson_OnMvcCoreBuilder_RegistersODataJsonConverts()
{
// Arrange
var services = new ServiceCollection();
services.AddLogging();
IMvcCoreBuilder builder = new MyMvcCoreBuilder
{
Services = services
};

// Act
builder.AddODataNewtonsoftJson();
IServiceProvider provider = services.BuildServiceProvider();

// Assert
IOptions<MvcNewtonsoftJsonOptions> options = provider.GetService<IOptions<MvcNewtonsoftJsonOptions>>();
Assert.NotNull(options);

MvcNewtonsoftJsonOptions jsonOptions = options.Value;
Assert.Contains(jsonOptions.SerializerSettings.Converters, c => c is JSelectExpandWrapperConverter);
Assert.Contains(jsonOptions.SerializerSettings.Converters, c => c is JDynamicTypeWrapperConverter);
Assert.Contains(jsonOptions.SerializerSettings.Converters, c => c is JPageResultValueConverter);
Assert.Contains(jsonOptions.SerializerSettings.Converters, c => c is JSingleResultValueConverter);
}
}

internal class MyMvcCoreBuilder : IMvcCoreBuilder
{
/// <inheritdoc />
public ApplicationPartManager PartManager { get; set; }

/// <inheritdoc />
public IServiceCollection Services { get; set; }
}
}