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 @@ -10,31 +10,32 @@
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ApiExplorer;
using Microsoft.Extensions.Options;
using Microsoft.OpenApi.Any;
using Microsoft.OpenApi.Models;
using Microsoft.Extensions.Options;

namespace Swashbuckle.AspNetCore.SwaggerGen
{
public class SchemaGenerator : ISchemaGenerator
{
private readonly SchemaGeneratorOptions _generatorOptions;
private readonly ISerializerDataContractResolver _serializerDataContractResolver;
private readonly IOptions<MvcOptions> _mvcOptions;

public SchemaGenerator(SchemaGeneratorOptions generatorOptions, ISerializerDataContractResolver serializerDataContractResolver)
: this(generatorOptions, serializerDataContractResolver, null)
public SchemaGenerator(
SchemaGeneratorOptions generatorOptions,
ISerializerDataContractResolver serializerDataContractResolver)
{
_generatorOptions = generatorOptions;
_serializerDataContractResolver = serializerDataContractResolver;
}

[Obsolete($"{nameof(IOptions<MvcOptions>)} is no longer used. This constructor will be removed in a future major release.")]
public SchemaGenerator(
SchemaGeneratorOptions generatorOptions,
ISerializerDataContractResolver serializerDataContractResolver,
IOptions<MvcOptions> mvcOptions)
: this(generatorOptions, serializerDataContractResolver)
{
_generatorOptions = generatorOptions;
_serializerDataContractResolver = serializerDataContractResolver;
_mvcOptions = mvcOptions;
}

public OpenApiSchema GenerateSchema(
Expand Down Expand Up @@ -440,9 +441,6 @@ private OpenApiSchema CreateObjectSchema(DataContract dataContract, SchemaReposi
: GenerateSchemaForType(dataProperty.MemberType, schemaRepository);

var markNonNullableTypeAsRequired = _generatorOptions.NonNullableReferenceTypesAsRequired
#if !NETSTANDARD2_0
&& (!_mvcOptions?.Value.SuppressImplicitRequiredAttributeForNonNullableReferenceTypes ?? true)
#endif
&& (dataProperty.MemberInfo?.IsNonNullableReferenceType() ?? false);

if ((
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public void Apply_EnrichesResponseMetadata_IfActionDecoratedWithSwaggerResponseC
.GetMethod(nameof(FakeControllerWithSwaggerAnnotations.ActionWithSwaggerResponseContentTypesAttributes));
var filterContext = new OperationFilterContext(
apiDescription: null,
schemaRegistry: new SchemaGenerator(new SchemaGeneratorOptions(), new JsonSerializerDataContractResolver(new JsonSerializerOptions()), Options.Create<MvcOptions>(new MvcOptions())),
schemaRegistry: new SchemaGenerator(new SchemaGeneratorOptions(), new JsonSerializerDataContractResolver(new JsonSerializerOptions())),
schemaRepository: new SchemaRepository(),
methodInfo: methodInfo);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -925,7 +925,7 @@ private static SchemaGenerator Subject(
var serializerSettings = new JsonSerializerSettings();
configureSerializer?.Invoke(serializerSettings);

return new SchemaGenerator(generatorOptions, new NewtonsoftDataContractResolver(serializerSettings), Options.Create<MvcOptions>(new MvcOptions()));
return new SchemaGenerator(generatorOptions, new NewtonsoftDataContractResolver(serializerSettings));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -984,6 +984,7 @@ public void GenerateSchema_SupportsOption_NonNullableReferenceTypesAsRequired_Re
Assert.Equal(required, propertyIsRequired);
}

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

var propertyIsRequired = schemaRepository.Schemas[subType].Required.Contains(propertyName);
Assert.Equal(!suppress, propertyIsRequired);
Assert.True(propertyIsRequired);
}

[Theory]
Expand Down Expand Up @@ -1318,19 +1319,31 @@ public void GenerateSchema_GeneratesSchema_IfParameterHasTypeConstraints()

private static SchemaGenerator Subject(
Action<SchemaGeneratorOptions> configureGenerator = null,
Action<JsonSerializerOptions> configureSerializer = null,
Action<MvcOptions> configureMvcOptions = null)
Action<JsonSerializerOptions> configureSerializer = null)
{
var generatorOptions = new SchemaGeneratorOptions();
configureGenerator?.Invoke(generatorOptions);

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

return new SchemaGenerator(generatorOptions, new JsonSerializerDataContractResolver(serializerOptions));
}

[Obsolete($"{nameof(IOptions<MvcOptions>)} is not used.")]
private static SchemaGenerator Subject(
Action<SchemaGeneratorOptions> configureGenerator,
Action<MvcOptions> configureMvcOptions)
{
var generatorOptions = new SchemaGeneratorOptions();
configureGenerator?.Invoke(generatorOptions);

var serializerOptions = new JsonSerializerOptions();

var mvcOptions = new MvcOptions();
configureMvcOptions?.Invoke(mvcOptions);

return new SchemaGenerator(generatorOptions, new JsonSerializerDataContractResolver(serializerOptions), Options.Create<MvcOptions>(mvcOptions));
return new SchemaGenerator(generatorOptions, new JsonSerializerDataContractResolver(serializerOptions), Options.Create(mvcOptions));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2528,8 +2528,8 @@ private static SwaggerGenerator Subject(
return new SwaggerGenerator(
options ?? DefaultOptions,
new FakeApiDescriptionGroupCollectionProvider(apiDescriptions),
new SchemaGenerator(new SchemaGeneratorOptions() { SchemaFilters = schemaFilters ?? [] }, new JsonSerializerDataContractResolver(new JsonSerializerOptions()), Options.Create<MvcOptions>(new MvcOptions())),
new FakeAuthenticationSchemeProvider(authenticationSchemes ?? Enumerable.Empty<AuthenticationScheme>())
new SchemaGenerator(new SchemaGeneratorOptions { SchemaFilters = schemaFilters ?? [] }, new JsonSerializerDataContractResolver(new JsonSerializerOptions())),
new FakeAuthenticationSchemeProvider(authenticationSchemes ?? [])
);
}

Expand Down