From 58d2f47ebddcb88047d8a8bcc41be9be56c1687d Mon Sep 17 00:00:00 2001 From: HavenDV Date: Fri, 31 May 2024 09:53:22 +0400 Subject: [PATCH] fix: Fixed JsonSerializerContext array types. --- src/libs/Directory.Build.props | 2 +- .../Json/SystemTextJsonSerializer.cs | 7 +++++-- .../JsonTests.JsonSerializerContextTypes.cs | 17 +++++++++++++++++ 3 files changed, 23 insertions(+), 3 deletions(-) create mode 100644 src/tests/OpenApiGenerator.UnitTests/JsonTests.JsonSerializerContextTypes.cs diff --git a/src/libs/Directory.Build.props b/src/libs/Directory.Build.props index a71799a375..d21bd88fc2 100644 --- a/src/libs/Directory.Build.props +++ b/src/libs/Directory.Build.props @@ -39,7 +39,7 @@ - 0.9.4 + 0.9.5 0.1 v dev diff --git a/src/libs/OpenApiGenerator.Core/Json/SystemTextJsonSerializer.cs b/src/libs/OpenApiGenerator.Core/Json/SystemTextJsonSerializer.cs index 56e8635a7b..193fcaf841 100644 --- a/src/libs/OpenApiGenerator.Core/Json/SystemTextJsonSerializer.cs +++ b/src/libs/OpenApiGenerator.Core/Json/SystemTextJsonSerializer.cs @@ -29,10 +29,12 @@ public string GenerateConverterAttribute(string type) return $"[global::System.Text.Json.Serialization.JsonConverter(typeof(global::OpenApiGenerator.JsonConverters.{type}))]"; } - private static readonly char[] ContextTypeSeparators = [',', '<', '>']; + private static readonly char[] ContextTypeSeparators = [',', '<', '>', '[']; - private static string GetContextType(string type) + public static string GetContextType(string type) { + type = type ?? throw new ArgumentNullException(nameof(type)); + return string.Concat(type .Replace("global::", string.Empty) .TrimEnd('?') @@ -41,6 +43,7 @@ private static string GetContextType(string type) .Split(ContextTypeSeparators, StringSplitOptions.RemoveEmptyEntries) .Select(x => x.Trim() switch { + "]" => "Array", "bool" => "Boolean", "short" => "Int16", "int" => "Int32", diff --git a/src/tests/OpenApiGenerator.UnitTests/JsonTests.JsonSerializerContextTypes.cs b/src/tests/OpenApiGenerator.UnitTests/JsonTests.JsonSerializerContextTypes.cs new file mode 100644 index 0000000000..fc1f2f64f7 --- /dev/null +++ b/src/tests/OpenApiGenerator.UnitTests/JsonTests.JsonSerializerContextTypes.cs @@ -0,0 +1,17 @@ +using static OpenApiGenerator.Core.Json.SystemTextJsonSerializer; + +namespace OpenApiGenerator.UnitTests; + +public partial class JsonTests +{ + [TestMethod] + public void ContextTypes() + { + GetContextType("global::System.Collections.Generic.List").Should().Be("ListString"); + GetContextType("global::System.Collections.Generic.Dictionary").Should().Be("DictionaryStringInt32"); + GetContextType("byte[]").Should().Be("ByteArray"); + GetContextType("byte[][]").Should().Be("ByteArrayArray"); + GetContextType("global::System.Collections.Generic.List").Should().Be("ListByteArray"); + GetContextType("global::System.Collections.Generic.List").Should().Be("ListByteArrayArray"); + } +} \ No newline at end of file