Skip to content

Commit

Permalink
Refactor to always inject serviceprovider
Browse files Browse the repository at this point in the history
  • Loading branch information
mattfrear committed Sep 7, 2023
1 parent e69760e commit 0049f00
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 29 deletions.
12 changes: 0 additions & 12 deletions src/Swashbuckle.AspNetCore.Filters/Examples/MvcOutputFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,6 @@ private OutputFormatterSelector OutputFormatterSelector
}
}

public MvcOutputFormatter(IOptions<MvcOptions> options, ILoggerFactory loggerFactory)
: this(options,
GetDefaultServiceProvider(),
loggerFactory)
{
}

public MvcOutputFormatter(IOptions<MvcOptions> options, IServiceProvider serviceProvider, ILoggerFactory loggerFactory)
{
this.initializedOutputFormatterSelector = false;
Expand Down Expand Up @@ -169,11 +162,6 @@ private static HttpContext GetHttpContext(
return httpContext;
}

private static IServiceProvider GetDefaultServiceProvider()
=> new ServiceCollection()
.AddSingleton(Options.Create(new MvcOptions()))
.BuildServiceProvider();

internal class FormatterNotFoundException : Exception
{
public FormatterNotFoundException(MediaTypeHeaderValue contentType)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public ExamplesOperationFilterTests()
serviceProvider.GetService(typeof(DictionaryRequestExample)).Returns(new DictionaryRequestExample());
serviceProvider.GetService(typeof(PeopleResponseExample)).Returns(new PeopleResponseExample());

var mvcOutputFormatter = new MvcOutputFormatter(FormatterOptions.WithXmlAndNewtonsoftJsonAndCsvFormatters, new FakeLoggerFactory());
var mvcOutputFormatter = new MvcOutputFormatter(FormatterOptions.WithXmlAndNewtonsoftJsonAndCsvFormatters, serviceProvider, new FakeLoggerFactory());
var requestExample = new RequestExample(mvcOutputFormatter, Options.Create(swaggerOptions));
var responseExample = new ResponseExample(mvcOutputFormatter);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
using System;
using System.Xml.Linq;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Microsoft.Net.Http.Headers;
using NSubstitute;
using Shouldly;
using Xunit;
using Swashbuckle.AspNetCore.Filters.Test.TestFixtures.Fakes;
using Swashbuckle.AspNetCore.Filters.Test.TestFixtures.Fakes.Examples;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using NSubstitute;
using Castle.Core.Logging;
using System;
using System.Xml.Linq;
using Xunit;

namespace Swashbuckle.AspNetCore.Filters.Test.Examples
{
Expand All @@ -19,7 +17,7 @@ public class GivenAMvcFormatterWithNoOutputFormatters_WhenSerializingAnObject
private readonly MvcOutputFormatter sut;

public GivenAMvcFormatterWithNoOutputFormatters_WhenSerializingAnObject()
=> sut = new MvcOutputFormatter(FormatterOptions.WithoutFormatters, null);
=> sut = new MvcOutputFormatter(FormatterOptions.WithoutFormatters, null, null);

[Fact]
public void ThenAFormatNotFoundExceptionIsThrown()
Expand Down Expand Up @@ -50,7 +48,7 @@ public class GivenAMvcFormatterWithOutputFormattersButNoLoggerFactory_WhenSerial

public GivenAMvcFormatterWithOutputFormattersButNoLoggerFactory_WhenSerializingAnObject()
{
sut = new MvcOutputFormatter(FormatterOptions.WithXmlDataContractFormatter, null);
sut = new MvcOutputFormatter(FormatterOptions.WithXmlDataContractFormatter, null, null);
}

[Fact]
Expand All @@ -70,7 +68,7 @@ public class GivenAMvcFormatterWithOutputFormatters_WhenSerializingAnObjectForAC
private readonly MvcOutputFormatter sut;

public GivenAMvcFormatterWithOutputFormatters_WhenSerializingAnObjectForAContentTypeThatIsNotConfigured()
=> sut = new MvcOutputFormatter(FormatterOptions.WithXmlDataContractFormatter, new FakeLoggerFactory());
=> sut = new MvcOutputFormatter(FormatterOptions.WithXmlDataContractFormatter, null, new FakeLoggerFactory());

[Fact]
public void ThenAFormatNotFoundExceptionIsThrown()
Expand All @@ -89,7 +87,7 @@ public class GivenAMvcFormatterWitXmlOutputFormatter_WhenSerializingAnObjectAsXm
private readonly MvcOutputFormatter sut;

public GivenAMvcFormatterWitXmlOutputFormatter_WhenSerializingAnObjectAsXml()
=> sut = new MvcOutputFormatter(FormatterOptions.WithXmlDataContractFormatter, new FakeLoggerFactory());
=> sut = new MvcOutputFormatter(FormatterOptions.WithXmlDataContractFormatter, null, new FakeLoggerFactory());

[Fact]
public void ThenAnXmlStringIsReturned()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public ServiceProviderExamplesOperationFilterTests()
{
schemaGeneratorOptions = new SchemaGeneratorOptions();

var mvcOutputFormatter = new MvcOutputFormatter(FormatterOptions.WithXmlAndNewtonsoftJsonAndCsvFormatters, new FakeLoggerFactory());
var mvcOutputFormatter = new MvcOutputFormatter(FormatterOptions.WithXmlAndNewtonsoftJsonAndCsvFormatters, serviceProvider, new FakeLoggerFactory());

var requestExample = new RequestExample(mvcOutputFormatter, Options.Create(swaggerOptions));
var responseExample = new ResponseExample(mvcOutputFormatter);
Expand Down Expand Up @@ -483,7 +483,7 @@ public void SetsResponseExamples_CorrectlyFormatsCsvExample()
public void ShouldEmitSystemTextJsonPropertyName()
{
// Arrange
var mvcOutputFormatter = new MvcOutputFormatter(FormatterOptions.WithSystemTextJsonFormatter, new FakeLoggerFactory());
var mvcOutputFormatter = new MvcOutputFormatter(FormatterOptions.WithSystemTextJsonFormatter, serviceProvider, new FakeLoggerFactory());
var responseExample = new ResponseExample(mvcOutputFormatter);
var sut = new ServiceProviderExamplesOperationFilter(serviceProvider, null, responseExample);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public ServiceProviderExamplesOperationFilterWithXmlDataContractTests()
serviceProvider = Substitute.For<IServiceProvider>();
serviceProvider.GetService(typeof(IExamplesProvider<PersonResponse>)).Returns(new PersonResponseAutoExample());

var mvcOutputFormatter = new MvcOutputFormatter(FormatterOptions.WithXmlDataContractFormatter, new FakeLoggerFactory());
var mvcOutputFormatter = new MvcOutputFormatter(FormatterOptions.WithXmlDataContractFormatter, serviceProvider, new FakeLoggerFactory());

sut = new ServiceProviderExamplesOperationFilter(
serviceProvider,
Expand Down

0 comments on commit 0049f00

Please sign in to comment.