Skip to content

Commit

Permalink
Porting test cases from domaindrivendev#2723 for #3 credits @dougclutter
Browse files Browse the repository at this point in the history
  • Loading branch information
Havunen committed Feb 18, 2024
1 parent 4e4bf2c commit ff74036
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,7 @@ public static PropertyInfo PropertyInfo(this ApiParameterDescription apiParamete
{
var modelMetadata = apiParameter.ModelMetadata;

return (modelMetadata?.ContainerType != null)
? modelMetadata.ContainerType.GetProperty(modelMetadata.PropertyName)
: null;
return modelMetadata?.ContainerType?.GetProperty(modelMetadata.PropertyName);
}

public static IEnumerable<object> CustomAttributes(this ApiParameterDescription apiParameter)
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.Text.Json.Serialization;
using DotSwashbuckle.AspNetCore.TestSupport;

namespace Swashbuckle.AspNetCore.SwaggerGen.Test.SchemaGenerator;

[JsonSerializable(typeof(IntEnum))]
[JsonSerializable(typeof(LongEnum))]
public partial class CustomJsonSerializerContext : JsonSerializerContext
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
using DotSwashbuckle.AspNetCore.TestSupport;
using Microsoft.OpenApi.Any;
using DotSwashbuckle.AspNetCore.TestSupport.Fixtures;
using Swashbuckle.AspNetCore.SwaggerGen.Test.SchemaGenerator;

namespace DotSwashbuckle.AspNetCore.SwaggerGen.Test
{
Expand Down Expand Up @@ -777,6 +778,34 @@ public void GenerateSchema_HonorsSerializerOption_PropertyNamingPolicy()
Assert.Equal(expectedDefaultAsJson, propertySchema.Default.ToJson());
}

[Theory]
[InlineData(typeof(IntEnum), "integer", "int32", false, "2", "4", "8")]
[InlineData(typeof(LongEnum), "integer", "int64", false, "2", "4", "8")]
[InlineData(typeof(IntEnum?), "integer", "int32", true, "2", "4", "8")]
[InlineData(typeof(LongEnum?), "integer", "int64", true, "2", "4", "8")]
public void GenerateSchema_GeneratesReferencedEnumSchema_IfEnumOrNullableEnumType_WorksWithJsonSerializerContext(
Type type,
string expectedSchemaType,
string expectedFormat,
bool expectedNullable,
params string[] expectedEnumAsJson
)
{
var schemaRepository = new SchemaRepository();

var referenceSchema = Subject(
configureSerializer: c => { c.TypeInfoResolver = CustomJsonSerializerContext.Default; }
).GenerateSchema(type, schemaRepository);

Assert.NotNull(referenceSchema.Reference);
var schema = schemaRepository.Schemas[referenceSchema.Reference.Id];
Assert.Equal(expectedSchemaType, schema.Type);
Assert.Equal(expectedFormat, schema.Format);
Assert.NotNull(schema.Enum);
Assert.Equal(expectedEnumAsJson, schema.Enum.Select(openApiAny => openApiAny.ToJson()));
Assert.Equal(expectedNullable, schema.Nullable);
}

[Fact]
public void GenerateSchema_HonorsSerializerAttribute_StringEnumConverter()
{
Expand Down

0 comments on commit ff74036

Please sign in to comment.