Skip to content

Commit

Permalink
revert breaking change, mark ctor as Obsolete
Browse files Browse the repository at this point in the history
bring back tests
  • Loading branch information
EvgeniyZ committed Jan 31, 2025
1 parent 972e8fb commit 1d65d2e
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System.Reflection;
using System.Xml.XPath;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ApiExplorer;
using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.SwaggerGen;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +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> _) -> 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,6 +10,7 @@
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;

Expand All @@ -28,6 +29,15 @@ public SchemaGenerator(
_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> _)
: this(generatorOptions, serializerDataContractResolver)
{
}

public OpenApiSchema GenerateSchema(
Type modelType,
SchemaRepository schemaRepository,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -984,6 +984,30 @@ 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)]
[InlineData(typeof(TypeWithNullableContextNotAnnotated), nameof(TypeWithNullableContextNotAnnotated.SubTypeWithOneNonNullableContent), nameof(TypeWithNullableContextNotAnnotated.NonNullableString), false)]
[InlineData(typeof(TypeWithNullableContextNotAnnotated), nameof(TypeWithNullableContextNotAnnotated.SubTypeWithOneNonNullableContent), nameof(TypeWithNullableContextNotAnnotated.NonNullableString), true)]
public void GenerateSchema_SupportsOption_SuppressImplicitRequiredAttributeForNonNullableReferenceTypes(
Type declaringType,
string subType,
string propertyName,
bool suppress)
{
var subject = Subject(
configureGenerator: c => c.NonNullableReferenceTypesAsRequired = true,
configureMvcOptions: o => o.SuppressImplicitRequiredAttributeForNonNullableReferenceTypes = suppress
);
var schemaRepository = new SchemaRepository();

subject.GenerateSchema(declaringType, schemaRepository);

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

[Theory]
[InlineData(typeof(TypeWithNullableContextAnnotated), nameof(TypeWithNullableContextAnnotated.SubTypeWithNestedSubType.Nested), nameof(TypeWithNullableContextAnnotated.SubTypeWithNestedSubType.Nested.NullableString), true)]
[InlineData(typeof(TypeWithNullableContextAnnotated), nameof(TypeWithNullableContextAnnotated.SubTypeWithNestedSubType.Nested), nameof(TypeWithNullableContextAnnotated.SubTypeWithNestedSubType.Nested.NonNullableString), false)]
Expand All @@ -1006,6 +1030,29 @@ public void GenerateSchema_SupportsOption_SupportNonNullableReferenceTypes_Neste
Assert.Equal(expectedNullable, propertySchema.Nullable);
}

[Theory]
[InlineData(typeof(TypeWithNullableContextAnnotated))]
[InlineData(typeof(TypeWithNullableContextNotAnnotated))]
public void GenerateSchema_Works_IfNotProvidingMvcOptions(Type type)
{
var generatorOptions = new SchemaGeneratorOptions
{
NonNullableReferenceTypesAsRequired = true
};

var serializerOptions = new JsonSerializerOptions();

var subject = new SchemaGenerator(generatorOptions, new JsonSerializerDataContractResolver(serializerOptions));
var schemaRepository = new SchemaRepository();

subject.GenerateSchema(type, schemaRepository);

var subType = nameof(TypeWithNullableContextAnnotated.SubTypeWithOneNonNullableContent);
var propertyName = nameof(TypeWithNullableContextAnnotated.NonNullableString);
var propertyIsRequired = schemaRepository.Schemas[subType].Required.Contains(propertyName);
Assert.True(propertyIsRequired);
}

[Fact]
public void GenerateSchema_HandlesTypesWithNestedTypes()
{
Expand Down Expand Up @@ -1270,6 +1317,7 @@ public void GenerateSchema_GeneratesSchema_IfParameterHasTypeConstraints()
Assert.Equal("integer", schema.Type);
}


private static SchemaGenerator Subject(
Action<SchemaGeneratorOptions> configureGenerator = null,
Action<JsonSerializerOptions> configureSerializer = null)
Expand All @@ -1282,5 +1330,21 @@ private static SchemaGenerator Subject(

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));
}
}
}

0 comments on commit 1d65d2e

Please sign in to comment.