Skip to content

Commit 515f9ad

Browse files
authored
Remove MvcOptions from SchemaGenerator (#3242)
Remove any usages of `MvcOptions` in `SchemaGenerator`. It should not be a breaking change since bool expression is `true` by definition if `SuppressImplicitRequiredAttributeForNonNullableReferenceTypes = false`. Resolves #3203.
1 parent 1e16163 commit 515f9ad

5 files changed

Lines changed: 29 additions & 18 deletions

File tree

src/Swashbuckle.AspNetCore.SwaggerGen/SchemaGenerator/SchemaGenerator.cs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,31 +10,32 @@
1010
using Microsoft.AspNetCore.Http;
1111
using Microsoft.AspNetCore.Mvc;
1212
using Microsoft.AspNetCore.Mvc.ApiExplorer;
13+
using Microsoft.Extensions.Options;
1314
using Microsoft.OpenApi.Any;
1415
using Microsoft.OpenApi.Models;
15-
using Microsoft.Extensions.Options;
1616

1717
namespace Swashbuckle.AspNetCore.SwaggerGen
1818
{
1919
public class SchemaGenerator : ISchemaGenerator
2020
{
2121
private readonly SchemaGeneratorOptions _generatorOptions;
2222
private readonly ISerializerDataContractResolver _serializerDataContractResolver;
23-
private readonly IOptions<MvcOptions> _mvcOptions;
2423

25-
public SchemaGenerator(SchemaGeneratorOptions generatorOptions, ISerializerDataContractResolver serializerDataContractResolver)
26-
: this(generatorOptions, serializerDataContractResolver, null)
24+
public SchemaGenerator(
25+
SchemaGeneratorOptions generatorOptions,
26+
ISerializerDataContractResolver serializerDataContractResolver)
2727
{
28+
_generatorOptions = generatorOptions;
29+
_serializerDataContractResolver = serializerDataContractResolver;
2830
}
2931

32+
[Obsolete($"{nameof(IOptions<MvcOptions>)} is no longer used. This constructor will be removed in a future major release.")]
3033
public SchemaGenerator(
3134
SchemaGeneratorOptions generatorOptions,
3235
ISerializerDataContractResolver serializerDataContractResolver,
3336
IOptions<MvcOptions> mvcOptions)
37+
: this(generatorOptions, serializerDataContractResolver)
3438
{
35-
_generatorOptions = generatorOptions;
36-
_serializerDataContractResolver = serializerDataContractResolver;
37-
_mvcOptions = mvcOptions;
3839
}
3940

4041
public OpenApiSchema GenerateSchema(
@@ -438,9 +439,6 @@ private OpenApiSchema CreateObjectSchema(DataContract dataContract, SchemaReposi
438439
: GenerateSchemaForType(dataProperty.MemberType, schemaRepository);
439440

440441
var markNonNullableTypeAsRequired = _generatorOptions.NonNullableReferenceTypesAsRequired
441-
#if !NETSTANDARD2_0
442-
&& (!_mvcOptions?.Value.SuppressImplicitRequiredAttributeForNonNullableReferenceTypes ?? true)
443-
#endif
444442
&& (dataProperty.MemberInfo?.IsNonNullableReferenceType() ?? false);
445443

446444
if ((

test/Swashbuckle.AspNetCore.Annotations.Test/AnnotationsOperationFilterTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public void Apply_EnrichesResponseMetadata_IfActionDecoratedWithSwaggerResponseC
7777
.GetMethod(nameof(FakeControllerWithSwaggerAnnotations.ActionWithSwaggerResponseContentTypesAttributes));
7878
var filterContext = new OperationFilterContext(
7979
apiDescription: null,
80-
schemaRegistry: new SchemaGenerator(new SchemaGeneratorOptions(), new JsonSerializerDataContractResolver(new JsonSerializerOptions()), Options.Create<MvcOptions>(new MvcOptions())),
80+
schemaRegistry: new SchemaGenerator(new SchemaGeneratorOptions(), new JsonSerializerDataContractResolver(new JsonSerializerOptions())),
8181
schemaRepository: new SchemaRepository(),
8282
methodInfo: methodInfo);
8383

test/Swashbuckle.AspNetCore.Newtonsoft.Test/SchemaGenerator/NewtonsoftSchemaGeneratorTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -937,7 +937,7 @@ private static SchemaGenerator Subject(
937937
var serializerSettings = new JsonSerializerSettings();
938938
configureSerializer?.Invoke(serializerSettings);
939939

940-
return new SchemaGenerator(generatorOptions, new NewtonsoftDataContractResolver(serializerSettings), Options.Create<MvcOptions>(new MvcOptions()));
940+
return new SchemaGenerator(generatorOptions, new NewtonsoftDataContractResolver(serializerSettings));
941941
}
942942
}
943943
}

test/Swashbuckle.AspNetCore.SwaggerGen.Test/SchemaGenerator/JsonSerializerSchemaGeneratorTests.cs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -984,6 +984,7 @@ public void GenerateSchema_SupportsOption_NonNullableReferenceTypesAsRequired_Re
984984
Assert.Equal(required, propertyIsRequired);
985985
}
986986

987+
[Obsolete($"{nameof(IOptions<MvcOptions>)} is not used.")]
987988
[Theory]
988989
[InlineData(typeof(TypeWithNullableContextAnnotated), nameof(TypeWithNullableContextAnnotated.SubTypeWithOneNonNullableContent), nameof(TypeWithNullableContextAnnotated.NonNullableString), false)]
989990
[InlineData(typeof(TypeWithNullableContextAnnotated), nameof(TypeWithNullableContextAnnotated.SubTypeWithOneNonNullableContent), nameof(TypeWithNullableContextAnnotated.NonNullableString), true)]
@@ -1004,7 +1005,7 @@ public void GenerateSchema_SupportsOption_SuppressImplicitRequiredAttributeForNo
10041005
subject.GenerateSchema(declaringType, schemaRepository);
10051006

10061007
var propertyIsRequired = schemaRepository.Schemas[subType].Required.Contains(propertyName);
1007-
Assert.Equal(!suppress, propertyIsRequired);
1008+
Assert.True(propertyIsRequired);
10081009
}
10091010

10101011
[Theory]
@@ -1330,19 +1331,31 @@ public void GenerateSchema_GeneratesSchema_IfParameterHasTypeConstraints()
13301331

13311332
private static SchemaGenerator Subject(
13321333
Action<SchemaGeneratorOptions> configureGenerator = null,
1333-
Action<JsonSerializerOptions> configureSerializer = null,
1334-
Action<MvcOptions> configureMvcOptions = null)
1334+
Action<JsonSerializerOptions> configureSerializer = null)
13351335
{
13361336
var generatorOptions = new SchemaGeneratorOptions();
13371337
configureGenerator?.Invoke(generatorOptions);
13381338

13391339
var serializerOptions = new JsonSerializerOptions();
13401340
configureSerializer?.Invoke(serializerOptions);
13411341

1342+
return new SchemaGenerator(generatorOptions, new JsonSerializerDataContractResolver(serializerOptions));
1343+
}
1344+
1345+
[Obsolete($"{nameof(IOptions<MvcOptions>)} is not used.")]
1346+
private static SchemaGenerator Subject(
1347+
Action<SchemaGeneratorOptions> configureGenerator,
1348+
Action<MvcOptions> configureMvcOptions)
1349+
{
1350+
var generatorOptions = new SchemaGeneratorOptions();
1351+
configureGenerator?.Invoke(generatorOptions);
1352+
1353+
var serializerOptions = new JsonSerializerOptions();
1354+
13421355
var mvcOptions = new MvcOptions();
13431356
configureMvcOptions?.Invoke(mvcOptions);
13441357

1345-
return new SchemaGenerator(generatorOptions, new JsonSerializerDataContractResolver(serializerOptions), Options.Create<MvcOptions>(mvcOptions));
1358+
return new SchemaGenerator(generatorOptions, new JsonSerializerDataContractResolver(serializerOptions), Options.Create(mvcOptions));
13461359
}
13471360
}
13481361
}

test/Swashbuckle.AspNetCore.SwaggerGen.Test/SwaggerGenerator/SwaggerGeneratorTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2528,8 +2528,8 @@ private static SwaggerGenerator Subject(
25282528
return new SwaggerGenerator(
25292529
options ?? DefaultOptions,
25302530
new FakeApiDescriptionGroupCollectionProvider(apiDescriptions),
2531-
new SchemaGenerator(new SchemaGeneratorOptions() { SchemaFilters = schemaFilters ?? [] }, new JsonSerializerDataContractResolver(new JsonSerializerOptions()), Options.Create<MvcOptions>(new MvcOptions())),
2532-
new FakeAuthenticationSchemeProvider(authenticationSchemes ?? Enumerable.Empty<AuthenticationScheme>())
2531+
new SchemaGenerator(new SchemaGeneratorOptions { SchemaFilters = schemaFilters ?? [] }, new JsonSerializerDataContractResolver(new JsonSerializerOptions())),
2532+
new FakeAuthenticationSchemeProvider(authenticationSchemes ?? [])
25332533
);
25342534
}
25352535

0 commit comments

Comments
 (0)