Skip to content
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 @@ -25,7 +25,7 @@ public static IServiceCollection AddSwaggerGenNewtonsoftSupport(this IServiceCol
{
var serializerSettings = s.GetRequiredService<IOptions<MvcNewtonsoftJsonOptions>>().Value?.SerializerSettings
?? new();
var generatorOptions = s.GetRequiredService<IOptions<SchemaGeneratorOptions>>().Value;
var generatorOptions = s.GetRequiredService<IOptions<SchemaGeneratorOptions>>();

return new NewtonsoftDataContractResolver(serializerSettings, generatorOptions);
}));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Reflection;
using Microsoft.Extensions.Options;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Serialization;
Expand All @@ -13,7 +14,7 @@ namespace Swashbuckle.AspNetCore.Newtonsoft;
public class NewtonsoftDataContractResolver : ISerializerDataContractResolver
{
private readonly JsonSerializerSettings _serializerSettings;
private readonly SchemaGeneratorOptions _generatorOptions;
private readonly IOptions<SchemaGeneratorOptions> _generatorOptions;
private readonly IContractResolver _contractResolver;

/// <summary>
Expand All @@ -31,9 +32,14 @@ public NewtonsoftDataContractResolver(JsonSerializerSettings serializerSettings)
/// <param name="serializerSettings">The <see cref="JsonSerializerSettings"/> to use.</param>
/// <param name="generatorOptions">The <see cref="SchemaGeneratorOptions"/> to use.</param>
public NewtonsoftDataContractResolver(JsonSerializerSettings serializerSettings, SchemaGeneratorOptions generatorOptions)
: this(serializerSettings, Options.Create(generatorOptions ?? new SchemaGeneratorOptions()))
{
}

internal NewtonsoftDataContractResolver(JsonSerializerSettings serializerSettings, IOptions<SchemaGeneratorOptions> generatorOptions)
{
_serializerSettings = serializerSettings;
_generatorOptions = generatorOptions ?? new SchemaGeneratorOptions();
_generatorOptions = generatorOptions;
_contractResolver = serializerSettings.ContractResolver ?? new DefaultContractResolver();
}

Expand Down Expand Up @@ -218,8 +224,9 @@ private List<DataProperty> GetDataPropertiesFor(JsonObjectContract jsonObjectCon
private bool IsNullable(MemberInfo memberInfo, Type propertyType)
{
var nullable = propertyType.IsReferenceOrNullableType();
var generatorOptions = _generatorOptions.Value;

if ((_generatorOptions.SupportNonNullableReferenceTypes || _generatorOptions.NonNullableReferenceTypesAsRequired)
if ((generatorOptions.SupportNonNullableReferenceTypes || generatorOptions.NonNullableReferenceTypesAsRequired)
&& memberInfo != null)
{
nullable &= !memberInfo.IsNonNullableReferenceType();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public static IServiceCollection AddSwaggerGen(
services.TryAddSingleton<ISerializerDataContractResolver>(s =>
{
var serializerOptions = s.GetRequiredService<JsonSerializerOptionsProvider>().Options;
var generatorOptions = s.GetRequiredService<IOptions<SchemaGeneratorOptions>>().Value;
var generatorOptions = s.GetRequiredService<IOptions<SchemaGeneratorOptions>>();
return new JsonSerializerDataContractResolver(serializerOptions, generatorOptions);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,30 @@
using System.Reflection;
using System.Text.Json;
using System.Text.Json.Serialization;
using Microsoft.Extensions.Options;
using Swashbuckle.AspNetCore.Annotations;

namespace Swashbuckle.AspNetCore.SwaggerGen;

public class JsonSerializerDataContractResolver : ISerializerDataContractResolver
{
private readonly JsonSerializerOptions _serializerOptions;
private readonly SchemaGeneratorOptions _generatorOptions;
private readonly IOptions<SchemaGeneratorOptions> _generatorOptions;

public JsonSerializerDataContractResolver(JsonSerializerOptions serializerOptions)
: this(serializerOptions, new SchemaGeneratorOptions())
{
}

public JsonSerializerDataContractResolver(JsonSerializerOptions serializerOptions, SchemaGeneratorOptions generatorOptions)
: this(serializerOptions, Options.Create(generatorOptions ?? new SchemaGeneratorOptions()))
{
}

internal JsonSerializerDataContractResolver(JsonSerializerOptions serializerOptions, IOptions<SchemaGeneratorOptions> generatorOptions)
{
_serializerOptions = serializerOptions;
_generatorOptions = generatorOptions ?? new SchemaGeneratorOptions();
_generatorOptions = generatorOptions;
}

public DataContract GetDataContractForType(Type type)
Expand Down Expand Up @@ -285,9 +291,10 @@ private List<DataProperty> GetDataPropertiesFor(Type objectType, out Type extens
private bool IsNullable(PropertyInfo propertyInfo, Type propertyType)
{
var nullable = propertyType.IsReferenceOrNullableType();
var generatorOptions = _generatorOptions.Value;

if (_generatorOptions.SupportNonNullableReferenceTypes ||
_generatorOptions.NonNullableReferenceTypesAsRequired)
if (generatorOptions.SupportNonNullableReferenceTypes ||
generatorOptions.NonNullableReferenceTypesAsRequired)
{
nullable &= !propertyInfo.IsNonNullableReferenceType();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Microsoft.OpenApi;
using Swashbuckle.AspNetCore.SwaggerGen;

namespace Swashbuckle.AspNetCore.Newtonsoft.Test;

public static class NewtonsoftServiceCollectionExtensionsTests
{
[Fact]
public static async Task AddSwaggerGenNewtonsoftSupport_CreatesSchemaFilter_WhenItDependsOnDataContractResolver()
{
var services = new ServiceCollection();
services.AddOptions();
services.AddSwaggerGen((options) => options.SchemaFilter<DataContractResolverSchemaFilter>());
services.AddSwaggerGenNewtonsoftSupport();

var provider = services.BuildServiceProvider();

// A dependency cycle through the options hangs instead of throwing, so resolve
// them on another thread to fail the test with a timeout.
var options = await Task.Run(() => provider.GetRequiredService<IOptions<SchemaGeneratorOptions>>().Value)
.WaitAsync(TimeSpan.FromSeconds(10), TestContext.Current.CancellationToken);

Assert.IsType<DataContractResolverSchemaFilter>(Assert.Single(options.SchemaFilters));
await provider.DisposeAsync();
}

private sealed class DataContractResolverSchemaFilter(ISerializerDataContractResolver resolver) : ISchemaFilter
{
public void Apply(IOpenApiSchema schema, SchemaFilterContext context) => _ = resolver;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Options;
using Microsoft.OpenApi;
using NSubstitute;

namespace Swashbuckle.AspNetCore.SwaggerGen.Test;
Expand Down Expand Up @@ -62,6 +64,23 @@ public static void AddSwaggerGen_UsesCamelCase_WhenNothingIsConfigured()
Assert.Equal("helloWorld", Assert.Single(contract.ObjectProperties).Name);
}

[Fact]
public static async Task AddSwaggerGen_CreatesSchemaFilter_WhenItDependsOnDataContractResolver()
{
var services = CreateServices();
services.AddSwaggerGen((options) => options.SchemaFilter<DataContractResolverSchemaFilter>());

var provider = services.BuildServiceProvider();

// A dependency cycle through the options hangs instead of throwing, so resolve
// them on another thread to fail the test with a timeout.
var options = await Task.Run(() => provider.GetRequiredService<IOptions<SchemaGeneratorOptions>>().Value)
.WaitAsync(TimeSpan.FromSeconds(10), TestContext.Current.CancellationToken);

Assert.IsType<DataContractResolverSchemaFilter>(Assert.Single(options.SchemaFilters));
await provider.DisposeAsync();
}

private static JsonNamingPolicy MinimalApiNamingPolicy { get; } = new SuffixNamingPolicy("-minimal-api");

private static JsonNamingPolicy MvcNamingPolicy { get; } = new SuffixNamingPolicy("-mvc");
Expand Down Expand Up @@ -96,4 +115,9 @@ private class Dto
{
public string HelloWorld { get; set; }
}

private sealed class DataContractResolverSchemaFilter(ISerializerDataContractResolver resolver) : ISchemaFilter
{
public void Apply(IOpenApiSchema schema, SchemaFilterContext context) => _ = resolver;
}
}
Loading