-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for using SwaggerUI with native AoT. SwaggerGen still does not support native AoT, though it happens to work for simple cases. Resolves #2550.
- Loading branch information
1 parent
7b3ef3d
commit 6c462f3
Showing
14 changed files
with
312 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
src/Swashbuckle.AspNetCore.SwaggerUI/JavascriptStringEnumConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#if NET6_0_OR_GREATER | ||
using System; | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace Swashbuckle.AspNetCore.SwaggerUI; | ||
|
||
internal sealed class JavascriptStringEnumConverter<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields)] TEnum>() : | ||
#if NET8_0_OR_GREATER | ||
JsonStringEnumConverter<TEnum>(JsonNamingPolicy.CamelCase, false) | ||
#else | ||
JsonStringEnumConverter(JsonNamingPolicy.CamelCase, false) | ||
#endif | ||
where TEnum : struct, Enum | ||
{ | ||
} | ||
#endif |
67 changes: 67 additions & 0 deletions
67
src/Swashbuckle.AspNetCore.SwaggerUI/JavascriptStringEnumEnumerableConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
#if NET6_0_OR_GREATER | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace Swashbuckle.AspNetCore.SwaggerUI; | ||
|
||
internal sealed class JavascriptStringEnumEnumerableConverter<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields)] TEnum>() : | ||
JsonConverterFactory | ||
where TEnum : struct, Enum | ||
{ | ||
private readonly JavascriptStringEnumConverter<TEnum> _enumConverter = new(); | ||
|
||
public override bool CanConvert(Type typeToConvert) | ||
=> typeToConvert.IsAssignableFrom(typeof(IEnumerable<TEnum>)); | ||
|
||
public override JsonConverter CreateConverter(Type typeToConvert, JsonSerializerOptions options) | ||
{ | ||
if (!typeToConvert.IsAssignableFrom(typeof(IEnumerable<TEnum>))) | ||
{ | ||
throw new NotSupportedException($"The type {typeToConvert} is not supported."); | ||
} | ||
|
||
var valueConverter = (JsonConverter<TEnum>)_enumConverter.CreateConverter(typeof(TEnum), options); | ||
return new EnumEnumerableConverter(valueConverter); | ||
} | ||
|
||
private sealed class EnumEnumerableConverter(JsonConverter<TEnum> inner) : JsonConverter<IEnumerable<TEnum>> | ||
{ | ||
public override IEnumerable<TEnum> Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | ||
{ | ||
if (reader.TokenType != JsonTokenType.StartArray) | ||
{ | ||
throw new JsonException("Expected start of a JSON array."); | ||
} | ||
|
||
var result = new List<TEnum>(); | ||
|
||
while (reader.Read()) | ||
{ | ||
if (reader.TokenType == JsonTokenType.EndArray) | ||
{ | ||
return result; | ||
} | ||
|
||
result.Add(inner.Read(ref reader, typeof(TEnum), options)); | ||
} | ||
|
||
throw new JsonException("JSON array is malformed."); | ||
} | ||
|
||
public override void Write(Utf8JsonWriter writer, IEnumerable<TEnum> value, JsonSerializerOptions options) | ||
{ | ||
writer.WriteStartArray(); | ||
|
||
foreach (var item in value) | ||
{ | ||
inner.Write(writer, item, options); | ||
} | ||
|
||
writer.WriteEndArray(); | ||
} | ||
} | ||
} | ||
#endif |
13 changes: 13 additions & 0 deletions
13
src/Swashbuckle.AspNetCore.SwaggerUI/SwaggerUIJsonSerializerContext.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#if NET6_0_OR_GREATER | ||
using System.Text.Json.Serialization; | ||
|
||
namespace Swashbuckle.AspNetCore.SwaggerUI; | ||
|
||
[JsonSerializable(typeof(ConfigObject))] | ||
[JsonSerializable(typeof(InterceptorFunctions))] | ||
[JsonSerializable(typeof(OAuthConfigObject))] | ||
[JsonSourceGenerationOptions( | ||
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, | ||
PropertyNamingPolicy = JsonKnownNamingPolicy.CamelCase)] | ||
internal sealed partial class SwaggerUIOptionsJsonContext : JsonSerializerContext; | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.