Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for enums when a JsonSerializerContext is used #2723

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
image: Visual Studio 2019
image: Visual Studio 2022

install:
- ps: Invoke-WebRequest "https://dot.net/v1/dotnet-install.ps1" -OutFile "./dotnet-install.ps1"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public DataContract GetDataContractForType(Type type)
jsonConverter: JsonConverterFunc);
}

private string JsonConverterFunc(object value)
private string JsonConverterFunc(dynamic value)
{
return JsonSerializer.Serialize(value, _serializerOptions);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,17 @@ private OpenApiSchema GenerateSchemaForType(Type modelType, SchemaRepository sch

private DataContract GetDataContractFor(Type modelType)
{
var effectiveType = Nullable.GetUnderlyingType(modelType) ?? modelType;
return _serializerDataContractResolver.GetDataContractForType(effectiveType);
try
{
var effectiveType = Nullable.GetUnderlyingType(modelType) ?? modelType;
return _serializerDataContractResolver.GetDataContractForType(effectiveType);
}
catch (Exception ex)
{
throw new SwaggerGeneratorException(
message: $"Failed to generate data contract for type - {modelType}. See inner exception",
innerException: ex);
}
}

private bool IsBaseTypeWithKnownTypesDefined(DataContract dataContract, out IEnumerable<DataContract> knownTypesDataContracts)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using Swashbuckle.AspNetCore.TestSupport;
using System.Text.Json.Serialization;

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 @@ -15,6 +15,7 @@
using Xunit;
using Swashbuckle.AspNetCore.TestSupport;
using Microsoft.OpenApi.Any;
using Swashbuckle.AspNetCore.SwaggerGen.Test.SchemaGenerator;

namespace Swashbuckle.AspNetCore.SwaggerGen.Test
{
Expand Down Expand Up @@ -730,6 +731,32 @@ public void GenerateSchema_HonorsSerializerOption_PropertyNamingPolicy()
Assert.Equal(expectedDefaultAsJson, propertySchema.Default.ToJson());
}

[Theory]
[InlineData(typeof(IntEnum), "integer", "int32", "2", "4", "8")]
[InlineData(typeof(LongEnum), "integer", "int64", "2", "4", "8")]
[InlineData(typeof(IntEnum?), "integer", "int32", "2", "4", "8")]
[InlineData(typeof(LongEnum?), "integer", "int64", "2", "4", "8")]
public void GenerateSchema_GeneratesReferencedEnumSchema_IfEnumOrNullableEnumType_WorksWithJsonSerializerContext(
Type type,
string expectedSchemaType,
string expectedFormat,
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()));
}


[Fact]
public void GenerateSchema_HonorsSerializerAttribute_StringEnumConverter()
{
Expand Down Expand Up @@ -860,7 +887,7 @@ public void GenerateSchema_GeneratesSchema_IfParameterHasTypeConstraints()
Assert.Equal("integer", schema.Type);
}

private SchemaGenerator Subject(
private SwaggerGen.SchemaGenerator Subject(
Action<SchemaGeneratorOptions> configureGenerator = null,
Action<JsonSerializerOptions> configureSerializer = null)
{
Expand All @@ -870,7 +897,7 @@ public void GenerateSchema_GeneratesSchema_IfParameterHasTypeConstraints()
var serializerOptions = new JsonSerializerOptions();
configureSerializer?.Invoke(serializerOptions);

return new SchemaGenerator(generatorOptions, new JsonSerializerDataContractResolver(serializerOptions));
return new SwaggerGen.SchemaGenerator(generatorOptions, new JsonSerializerDataContractResolver(serializerOptions));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1285,7 +1285,7 @@ public void GetSwagger_SupportsOption_DocumentFilters()
return new SwaggerGenerator(
options ?? DefaultOptions,
new FakeApiDescriptionGroupCollectionProvider(apiDescriptions),
new SchemaGenerator(new SchemaGeneratorOptions(), new JsonSerializerDataContractResolver(new JsonSerializerOptions())),
new SwaggerGen.SchemaGenerator(new SchemaGeneratorOptions(), new JsonSerializerDataContractResolver(new JsonSerializerOptions())),
new FakeAuthenticationSchemeProvider(authenticationSchemes ?? Enumerable.Empty<AuthenticationScheme>())
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<DocumentationFile>Swashbuckle.AspNetCore.SwaggerGen.Test.xml</DocumentationFile>
<NoWarn>1701;1702;1591</NoWarn>
<TargetFrameworks>net6.0</TargetFrameworks>
<TargetFrameworks>net7.0</TargetFrameworks>
</PropertyGroup>

<ItemGroup>
Expand Down

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