|
| 1 | +using System; |
1 | 2 | using System.Collections.Generic; |
2 | 3 | using System.Linq; |
3 | 4 | using System.Text.Json.Serialization; |
|
10 | 11 | using Microsoft.VisualStudio.TestTools.UnitTesting; |
11 | 12 | using Newtonsoft.Json; |
12 | 13 | using Newtonsoft.Json.Linq; |
| 14 | +using System.Reflection; |
| 15 | +using Bogus.DataSets; |
| 16 | +using System.Runtime.Serialization; |
13 | 17 |
|
14 | 18 | namespace FlurlGraphQL.Tests |
15 | 19 | { |
@@ -156,6 +160,55 @@ public void TestSimpleSystemTextJsonParsingOfPreFlattenedJsonWithStringEnumDirec |
156 | 160 | Assert.IsTrue(isEqual, "The Test Json and the Round Trip Serialized Json do not match!"); |
157 | 161 | } |
158 | 162 |
|
| 163 | + [TestMethod] |
| 164 | + [TestDataExecuteWithAllFlurlSerializerRequests] |
| 165 | + public void TestJsonParsingForAllStringEnumTestCases(IFlurlRequest flurlRequest) |
| 166 | + { |
| 167 | + //The DEFAULT options for FlurlGraphQL System.Text.Json Serializer should already have the String Enum Converter added... |
| 168 | + //var flurlGraphQLSystemTextJsonSerializer = new FlurlGraphQLSystemTextJsonSerializer(FlurlGraphQLSystemTextJsonSerializer.CreateDefaultSerializerOptions()); |
| 169 | + var graphqlApiRequest = flurlRequest.ToGraphQLRequest(); |
| 170 | + |
| 171 | + var graphqlJsonSerializer = graphqlApiRequest.GraphQLJsonSerializer; |
| 172 | + |
| 173 | + TestContext.WriteLine($"[{graphqlJsonSerializer.GetType().Name}] Enum Test Cases Parsing ..."); |
| 174 | + bool isUsingSystemTextJson = graphqlJsonSerializer is FlurlGraphQLSystemTextJsonSerializer; |
| 175 | + |
| 176 | + foreach (int enumIntValue in Enum.GetValues(typeof(EnumTestCase))) |
| 177 | + { |
| 178 | + var enumValue = (EnumTestCase)enumIntValue; |
| 179 | + var enumField = enumValue.GetType().GetField(enumValue.ToString()); |
| 180 | + |
| 181 | + var expectedEnumStringValue = |
| 182 | + enumField.GetCustomAttribute<EnumMemberAttribute>(true)?.Value |
| 183 | + //JsonPropertyName is ONLY supported by System.Text.Json; Newtonsoft did not support this attribute for Enums... |
| 184 | + ?? (isUsingSystemTextJson ? enumField.GetCustomAttribute<JsonPropertyNameAttribute>(true)?.Name : null) |
| 185 | + ?? enumValue.ToString().ToScreamingSnakeCase(); |
| 186 | + |
| 187 | + //Serialize the enum test case... |
| 188 | + var serializedEnumValue = graphqlJsonSerializer.Serialize(enumValue); |
| 189 | + |
| 190 | + //The serialized value should be a quoted string output from Json Serialization... |
| 191 | + Assert.AreEqual($"\"{expectedEnumStringValue}\"", serializedEnumValue, $"The Enum Value [{enumValue}] did not serialize to the expected string value [{expectedEnumStringValue}]!"); |
| 192 | + |
| 193 | + //Deserialize back to the Enum Value... |
| 194 | + var deserializedEnumValue = graphqlJsonSerializer.Deserialize<EnumTestCase>(serializedEnumValue); |
| 195 | + Assert.AreEqual(enumValue, deserializedEnumValue, $"The Enum Value [{enumValue}] did not deserialize back correctly from the serialized value [{serializedEnumValue}]!"); |
| 196 | + |
| 197 | + //Valdate actual values for the OVERRIDE cases using Attributes... |
| 198 | + switch (enumValue) |
| 199 | + { |
| 200 | + case EnumTestCase.TestPacalCaseEnumMember: |
| 201 | + Assert.AreEqual("TEST_PASCAL_CASE_ENUM_MEMBER_OVERRIDE", expectedEnumStringValue); |
| 202 | + break; |
| 203 | + case EnumTestCase.TestJsonPropertyNameSupportedBySystemTextJson when isUsingSystemTextJson: |
| 204 | + Assert.AreEqual("TEST.JsonPropertyName.Supported.By.System.Text.Json", expectedEnumStringValue); |
| 205 | + break; |
| 206 | + } |
| 207 | + |
| 208 | + TestContext.WriteLine($"Success: [{enumValue}] <==> [{serializedEnumValue}]"); |
| 209 | + } |
| 210 | + } |
| 211 | + |
159 | 212 | [TestMethod] |
160 | 213 | public void TestSystemTextJsonParsingOfNestedPaginatedStarWarsGraphQLResults() |
161 | 214 | { |
|
0 commit comments