Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -202,7 +202,7 @@ Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext.Type.get -> System.Type
Swashbuckle.AspNetCore.SwaggerGen.SchemaGenerator
Swashbuckle.AspNetCore.SwaggerGen.SchemaGenerator.GenerateSchema(System.Type modelType, Swashbuckle.AspNetCore.SwaggerGen.SchemaRepository schemaRepository, System.Reflection.MemberInfo memberInfo = null, System.Reflection.ParameterInfo parameterInfo = null, Microsoft.AspNetCore.Mvc.ApiExplorer.ApiParameterRouteInfo routeInfo = null) -> Microsoft.OpenApi.Models.OpenApiSchema
Swashbuckle.AspNetCore.SwaggerGen.SchemaGenerator.SchemaGenerator(Swashbuckle.AspNetCore.SwaggerGen.SchemaGeneratorOptions generatorOptions, Swashbuckle.AspNetCore.SwaggerGen.ISerializerDataContractResolver serializerDataContractResolver) -> void
Swashbuckle.AspNetCore.SwaggerGen.SchemaGenerator.SchemaGenerator(Swashbuckle.AspNetCore.SwaggerGen.SchemaGeneratorOptions generatorOptions, Swashbuckle.AspNetCore.SwaggerGen.ISerializerDataContractResolver serializerDataContractResolver, Microsoft.Extensions.Options.IOptions<Microsoft.AspNetCore.Mvc.MvcOptions> mvcOptions) -> void
Swashbuckle.AspNetCore.SwaggerGen.SchemaGenerator.SchemaGenerator(Swashbuckle.AspNetCore.SwaggerGen.SchemaGeneratorOptions generatorOptions, Swashbuckle.AspNetCore.SwaggerGen.ISerializerDataContractResolver serializerDataContractResolver, Microsoft.Extensions.Options.IOptions<Microsoft.AspNetCore.Mvc.MvcOptions> _) -> void
Swashbuckle.AspNetCore.SwaggerGen.SchemaGeneratorOptions
Swashbuckle.AspNetCore.SwaggerGen.SchemaGeneratorOptions.CustomTypeMappings.get -> System.Collections.Generic.IDictionary<System.Type, System.Func<Microsoft.OpenApi.Models.OpenApiSchema>>
Swashbuckle.AspNetCore.SwaggerGen.SchemaGeneratorOptions.CustomTypeMappings.set -> void
Expand Down
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 not used. This constructor will be removed in future versions")]
public SchemaGenerator(
SchemaGeneratorOptions generatorOptions,
ISerializerDataContractResolver serializerDataContractResolver,
IOptions<MvcOptions> mvcOptions)
IOptions<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. Test method must be removed when ctor will be removed")]
[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 @@ -1316,21 +1317,34 @@ public void GenerateSchema_GeneratesSchema_IfParameterHasTypeConstraints()
Assert.Equal("integer", schema.Type);
}


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. Test method must be removed when ctor will be removed")]
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