Skip to content

Commit

Permalink
fix: Fixed bug with missing settings inside AnyOf JsonConverter.
Browse files Browse the repository at this point in the history
  • Loading branch information
HavenDV committed Jun 4, 2024
1 parent 22910e1 commit c8b8b1f
Show file tree
Hide file tree
Showing 24 changed files with 309 additions and 237 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class {subType}JsonConverter{types} : global::System.Text.Json.Serializat
T{x}? value{x} = default;
try
{{
value{x} = global::System.Text.Json.JsonSerializer.Deserialize<T{x}>(ref readerCopy);
value{x} = global::System.Text.Json.JsonSerializer.Deserialize<T{x}>(ref readerCopy, options);
}}
catch (global::System.Text.Json.JsonException)
{{
Expand All @@ -57,7 +57,7 @@ public class {subType}JsonConverter{types} : global::System.Text.Json.Serializat
{Enumerable.Range(1, count).Select(x => $@"
{(x == 1 ? "" : "else ")}if (value{x} != null)
{{
_ = global::System.Text.Json.JsonSerializer.Deserialize<T{x}>(ref reader);
_ = global::System.Text.Json.JsonSerializer.Deserialize<T{x}>(ref reader, options);
}}
").Inject().TrimEnd(',')}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public override AnyOf<T1, T2> Read(
T1? value1 = default;
try
{
value1 = global::System.Text.Json.JsonSerializer.Deserialize<T1>(ref readerCopy);
value1 = global::System.Text.Json.JsonSerializer.Deserialize<T1>(ref readerCopy, options);
}
catch (global::System.Text.Json.JsonException)
{
Expand All @@ -26,7 +26,7 @@ public override AnyOf<T1, T2> Read(
T2? value2 = default;
try
{
value2 = global::System.Text.Json.JsonSerializer.Deserialize<T2>(ref readerCopy);
value2 = global::System.Text.Json.JsonSerializer.Deserialize<T2>(ref readerCopy, options);
}
catch (global::System.Text.Json.JsonException)
{
Expand All @@ -40,11 +40,11 @@ public override AnyOf<T1, T2> Read(

if (value1 != null)
{
_ = global::System.Text.Json.JsonSerializer.Deserialize<T1>(ref reader);
_ = global::System.Text.Json.JsonSerializer.Deserialize<T1>(ref reader, options);
}
else if (value2 != null)
{
_ = global::System.Text.Json.JsonSerializer.Deserialize<T2>(ref reader);
_ = global::System.Text.Json.JsonSerializer.Deserialize<T2>(ref reader, options);
}

return result;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace OpenApiGenerator.JsonConverters;

public enum TestEnum

Check warning on line 3 in src/libs/OpenApiGenerator.Helpers/JsonEnumConverter/TestEnum.cs

View workflow job for this annotation

GitHub Actions / Build, test and publish / Build, test and publish

Rename type name TestEnum so that it does not end in 'Enum' (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1711)

Check warning on line 3 in src/libs/OpenApiGenerator.Helpers/JsonEnumConverter/TestEnum.cs

View workflow job for this annotation

GitHub Actions / Build, test and publish / Build, test and publish

Rename type name TestEnum so that it does not end in 'Enum' (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1711)
{
PullingManifest,
Success,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace OpenApiGenerator.JsonConverters;

public sealed partial class TestEnumClass
{
[global::System.Text.Json.Serialization.JsonPropertyName("status")]
[global::System.Text.Json.Serialization.JsonConverter(typeof(TestEnumJsonStringEnumConverter))]
public TestEnum? Status { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;

namespace OpenApiGenerator.JsonConverters;

public static class TestEnumExtensions
{
public static string ToValueString(this TestEnum value)
{
return value switch
{
TestEnum.PullingManifest => "pulling manifest",
TestEnum.Success => "success",
_ => throw new ArgumentOutOfRangeException(nameof(value), value, null),
};
}

public static TestEnum ToEnum(string value)
{
return value switch
{
"pulling manifest" => TestEnum.PullingManifest,
"success" => TestEnum.Success,
_ => throw new ArgumentOutOfRangeException(nameof(value), value, null),
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
namespace OpenApiGenerator.JsonConverters;

public sealed class TestEnumJsonStringEnumConverter : global::System.Text.Json.Serialization.JsonConverter<TestEnum>
{
public override TestEnum Read(
ref global::System.Text.Json.Utf8JsonReader reader,
global::System.Type typeToConvert,
global::System.Text.Json.JsonSerializerOptions options)
{
switch (reader.TokenType)
{
case global::System.Text.Json.JsonTokenType.String:
{
var stringValue = reader.GetString();
if (stringValue != null)
{
return TestEnumExtensions.ToEnum(stringValue);
}

break;
}
case global::System.Text.Json.JsonTokenType.Number:
{
return (TestEnum)reader.GetInt32();
}
default:
throw new global::System.ArgumentOutOfRangeException();

Check warning on line 27 in src/libs/OpenApiGenerator.Helpers/JsonEnumConverter/TestEnumJsonStringEnumConverter.cs

View workflow job for this annotation

GitHub Actions / Build, test and publish / Build, test and publish

Call the ArgumentOutOfRangeException constructor that contains a message and/or paramName parameter (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2208)

Check warning on line 27 in src/libs/OpenApiGenerator.Helpers/JsonEnumConverter/TestEnumJsonStringEnumConverter.cs

View workflow job for this annotation

GitHub Actions / Build, test and publish / Build, test and publish

Call the ArgumentOutOfRangeException constructor that contains a message and/or paramName parameter (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2208)
}

return default;
}

public override void Write(
global::System.Text.Json.Utf8JsonWriter writer,
TestEnum value,
global::System.Text.Json.JsonSerializerOptions options)
{
writer.WriteStringValue(value.ToValueString());

Check warning on line 38 in src/libs/OpenApiGenerator.Helpers/JsonEnumConverter/TestEnumJsonStringEnumConverter.cs

View workflow job for this annotation

GitHub Actions / Build, test and publish / Build, test and publish

In externally visible method 'void TestEnumJsonStringEnumConverter.Write(Utf8JsonWriter writer, TestEnum value, JsonSerializerOptions options)', validate parameter 'writer' is non-null before using it. If appropriate, throw an 'ArgumentNullException' when the argument is 'null'. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1062)

Check warning on line 38 in src/libs/OpenApiGenerator.Helpers/JsonEnumConverter/TestEnumJsonStringEnumConverter.cs

View workflow job for this annotation

GitHub Actions / Build, test and publish / Build, test and publish

In externally visible method 'void TestEnumJsonStringEnumConverter.Write(Utf8JsonWriter writer, TestEnum value, JsonSerializerOptions options)', validate parameter 'writer' is non-null before using it. If appropriate, throw an 'ArgumentNullException' when the argument is 'null'. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1062)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using System;
using Newtonsoft.Json;

namespace OpenApiGenerator.JsonConverters;

public sealed class TestEnumNewtonsoftJsonStringEnumConverter : Newtonsoft.Json.JsonConverter<TestEnum>
{
public override void WriteJson(JsonWriter writer, TestEnum value, Newtonsoft.Json.JsonSerializer serializer)
{
writer.WriteValue(value.ToValueString());

Check warning on line 10 in src/libs/OpenApiGenerator.Helpers/JsonEnumConverter/TestEnumNewtonsoftJsonStringEnumConverter.cs

View workflow job for this annotation

GitHub Actions / Build, test and publish / Build, test and publish

In externally visible method 'void TestEnumNewtonsoftJsonStringEnumConverter.WriteJson(JsonWriter writer, TestEnum value, JsonSerializer serializer)', validate parameter 'writer' is non-null before using it. If appropriate, throw an 'ArgumentNullException' when the argument is 'null'. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1062)

Check warning on line 10 in src/libs/OpenApiGenerator.Helpers/JsonEnumConverter/TestEnumNewtonsoftJsonStringEnumConverter.cs

View workflow job for this annotation

GitHub Actions / Build, test and publish / Build, test and publish

In externally visible method 'void TestEnumNewtonsoftJsonStringEnumConverter.WriteJson(JsonWriter writer, TestEnum value, JsonSerializer serializer)', validate parameter 'writer' is non-null before using it. If appropriate, throw an 'ArgumentNullException' when the argument is 'null'. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1062)
}

public override TestEnum ReadJson(JsonReader reader, Type objectType, TestEnum existingValue, bool hasExistingValue,
Newtonsoft.Json.JsonSerializer serializer)
{
if (hasExistingValue)
{
return existingValue;
}

var type = reader.TokenType;

Check warning on line 21 in src/libs/OpenApiGenerator.Helpers/JsonEnumConverter/TestEnumNewtonsoftJsonStringEnumConverter.cs

View workflow job for this annotation

GitHub Actions / Build, test and publish / Build, test and publish

In externally visible method 'TestEnum TestEnumNewtonsoftJsonStringEnumConverter.ReadJson(JsonReader reader, Type objectType, TestEnum existingValue, bool hasExistingValue, JsonSerializer serializer)', validate parameter 'reader' is non-null before using it. If appropriate, throw an 'ArgumentNullException' when the argument is 'null'. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1062)

Check warning on line 21 in src/libs/OpenApiGenerator.Helpers/JsonEnumConverter/TestEnumNewtonsoftJsonStringEnumConverter.cs

View workflow job for this annotation

GitHub Actions / Build, test and publish / Build, test and publish

In externally visible method 'TestEnum TestEnumNewtonsoftJsonStringEnumConverter.ReadJson(JsonReader reader, Type objectType, TestEnum existingValue, bool hasExistingValue, JsonSerializer serializer)', validate parameter 'reader' is non-null before using it. If appropriate, throw an 'ArgumentNullException' when the argument is 'null'. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1062)

switch (type)
{
case JsonToken.String:
{
var stringValue = reader.ReadAsString();
if (stringValue != null)
{
return TestEnumExtensions.ToEnum(stringValue);
}
break;
}
case JsonToken.Integer:
{
var numValue = reader.ReadAsInt32();
if (numValue != null)
{
return (TestEnum)numValue.Value;
}

break;
}
}

return default;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class OneOfJsonConverter<T1, T2, T3> : global::System.Text.Json.Serializa
T1? value1 = default;
try
{
value1 = global::System.Text.Json.JsonSerializer.Deserialize<T1>(ref readerCopy);
value1 = global::System.Text.Json.JsonSerializer.Deserialize<T1>(ref readerCopy, options);
}
catch (global::System.Text.Json.JsonException)
{
Expand All @@ -27,7 +27,7 @@ public class OneOfJsonConverter<T1, T2, T3> : global::System.Text.Json.Serializa
T2? value2 = default;
try
{
value2 = global::System.Text.Json.JsonSerializer.Deserialize<T2>(ref readerCopy);
value2 = global::System.Text.Json.JsonSerializer.Deserialize<T2>(ref readerCopy, options);
}
catch (global::System.Text.Json.JsonException)
{
Expand All @@ -37,7 +37,7 @@ public class OneOfJsonConverter<T1, T2, T3> : global::System.Text.Json.Serializa
T3? value3 = default;
try
{
value3 = global::System.Text.Json.JsonSerializer.Deserialize<T3>(ref readerCopy);
value3 = global::System.Text.Json.JsonSerializer.Deserialize<T3>(ref readerCopy, options);
}
catch (global::System.Text.Json.JsonException)
{
Expand All @@ -56,17 +56,17 @@ public class OneOfJsonConverter<T1, T2, T3> : global::System.Text.Json.Serializa

if (value1 != null)
{
_ = global::System.Text.Json.JsonSerializer.Deserialize<T1>(ref reader);
_ = global::System.Text.Json.JsonSerializer.Deserialize<T1>(ref reader, options);
}

else if (value2 != null)
{
_ = global::System.Text.Json.JsonSerializer.Deserialize<T2>(ref reader);
_ = global::System.Text.Json.JsonSerializer.Deserialize<T2>(ref reader, options);
}

else if (value3 != null)
{
_ = global::System.Text.Json.JsonSerializer.Deserialize<T3>(ref reader);
_ = global::System.Text.Json.JsonSerializer.Deserialize<T3>(ref reader, options);
}
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class AnyOfJsonConverter<T1, T2> : global::System.Text.Json.Serialization
T1? value1 = default;
try
{
value1 = global::System.Text.Json.JsonSerializer.Deserialize<T1>(ref readerCopy);
value1 = global::System.Text.Json.JsonSerializer.Deserialize<T1>(ref readerCopy, options);
}
catch (global::System.Text.Json.JsonException)
{
Expand All @@ -27,7 +27,7 @@ public class AnyOfJsonConverter<T1, T2> : global::System.Text.Json.Serialization
T2? value2 = default;
try
{
value2 = global::System.Text.Json.JsonSerializer.Deserialize<T2>(ref readerCopy);
value2 = global::System.Text.Json.JsonSerializer.Deserialize<T2>(ref readerCopy, options);
}
catch (global::System.Text.Json.JsonException)
{
Expand All @@ -44,12 +44,12 @@ public class AnyOfJsonConverter<T1, T2> : global::System.Text.Json.Serialization

if (value1 != null)
{
_ = global::System.Text.Json.JsonSerializer.Deserialize<T1>(ref reader);
_ = global::System.Text.Json.JsonSerializer.Deserialize<T1>(ref reader, options);
}

else if (value2 != null)
{
_ = global::System.Text.Json.JsonSerializer.Deserialize<T2>(ref reader);
_ = global::System.Text.Json.JsonSerializer.Deserialize<T2>(ref reader, options);
}
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class AnyOfJsonConverter<T1, T2, T3> : global::System.Text.Json.Serializa
T1? value1 = default;
try
{
value1 = global::System.Text.Json.JsonSerializer.Deserialize<T1>(ref readerCopy);
value1 = global::System.Text.Json.JsonSerializer.Deserialize<T1>(ref readerCopy, options);
}
catch (global::System.Text.Json.JsonException)
{
Expand All @@ -27,7 +27,7 @@ public class AnyOfJsonConverter<T1, T2, T3> : global::System.Text.Json.Serializa
T2? value2 = default;
try
{
value2 = global::System.Text.Json.JsonSerializer.Deserialize<T2>(ref readerCopy);
value2 = global::System.Text.Json.JsonSerializer.Deserialize<T2>(ref readerCopy, options);
}
catch (global::System.Text.Json.JsonException)
{
Expand All @@ -37,7 +37,7 @@ public class AnyOfJsonConverter<T1, T2, T3> : global::System.Text.Json.Serializa
T3? value3 = default;
try
{
value3 = global::System.Text.Json.JsonSerializer.Deserialize<T3>(ref readerCopy);
value3 = global::System.Text.Json.JsonSerializer.Deserialize<T3>(ref readerCopy, options);
}
catch (global::System.Text.Json.JsonException)
{
Expand All @@ -56,17 +56,17 @@ public class AnyOfJsonConverter<T1, T2, T3> : global::System.Text.Json.Serializa

if (value1 != null)
{
_ = global::System.Text.Json.JsonSerializer.Deserialize<T1>(ref reader);
_ = global::System.Text.Json.JsonSerializer.Deserialize<T1>(ref reader, options);
}

else if (value2 != null)
{
_ = global::System.Text.Json.JsonSerializer.Deserialize<T2>(ref reader);
_ = global::System.Text.Json.JsonSerializer.Deserialize<T2>(ref reader, options);
}

else if (value3 != null)
{
_ = global::System.Text.Json.JsonSerializer.Deserialize<T3>(ref reader);
_ = global::System.Text.Json.JsonSerializer.Deserialize<T3>(ref reader, options);
}
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class AnyOfJsonConverter<T1, T2, T3, T4> : global::System.Text.Json.Seria
T1? value1 = default;
try
{
value1 = global::System.Text.Json.JsonSerializer.Deserialize<T1>(ref readerCopy);
value1 = global::System.Text.Json.JsonSerializer.Deserialize<T1>(ref readerCopy, options);
}
catch (global::System.Text.Json.JsonException)
{
Expand All @@ -27,7 +27,7 @@ public class AnyOfJsonConverter<T1, T2, T3, T4> : global::System.Text.Json.Seria
T2? value2 = default;
try
{
value2 = global::System.Text.Json.JsonSerializer.Deserialize<T2>(ref readerCopy);
value2 = global::System.Text.Json.JsonSerializer.Deserialize<T2>(ref readerCopy, options);
}
catch (global::System.Text.Json.JsonException)
{
Expand All @@ -37,7 +37,7 @@ public class AnyOfJsonConverter<T1, T2, T3, T4> : global::System.Text.Json.Seria
T3? value3 = default;
try
{
value3 = global::System.Text.Json.JsonSerializer.Deserialize<T3>(ref readerCopy);
value3 = global::System.Text.Json.JsonSerializer.Deserialize<T3>(ref readerCopy, options);
}
catch (global::System.Text.Json.JsonException)
{
Expand All @@ -47,7 +47,7 @@ public class AnyOfJsonConverter<T1, T2, T3, T4> : global::System.Text.Json.Seria
T4? value4 = default;
try
{
value4 = global::System.Text.Json.JsonSerializer.Deserialize<T4>(ref readerCopy);
value4 = global::System.Text.Json.JsonSerializer.Deserialize<T4>(ref readerCopy, options);
}
catch (global::System.Text.Json.JsonException)
{
Expand All @@ -68,22 +68,22 @@ public class AnyOfJsonConverter<T1, T2, T3, T4> : global::System.Text.Json.Seria

if (value1 != null)
{
_ = global::System.Text.Json.JsonSerializer.Deserialize<T1>(ref reader);
_ = global::System.Text.Json.JsonSerializer.Deserialize<T1>(ref reader, options);
}

else if (value2 != null)
{
_ = global::System.Text.Json.JsonSerializer.Deserialize<T2>(ref reader);
_ = global::System.Text.Json.JsonSerializer.Deserialize<T2>(ref reader, options);
}

else if (value3 != null)
{
_ = global::System.Text.Json.JsonSerializer.Deserialize<T3>(ref reader);
_ = global::System.Text.Json.JsonSerializer.Deserialize<T3>(ref reader, options);
}

else if (value4 != null)
{
_ = global::System.Text.Json.JsonSerializer.Deserialize<T4>(ref reader);
_ = global::System.Text.Json.JsonSerializer.Deserialize<T4>(ref reader, options);
}
return result;
}
Expand Down
Loading

0 comments on commit c8b8b1f

Please sign in to comment.