diff --git a/src/generators/csharp/presets/NewtonsoftSerializerPreset.ts b/src/generators/csharp/presets/NewtonsoftSerializerPreset.ts index 648e948f4c..8c803ae484 100644 --- a/src/generators/csharp/presets/NewtonsoftSerializerPreset.ts +++ b/src/generators/csharp/presets/NewtonsoftSerializerPreset.ts @@ -29,8 +29,7 @@ function renderSerialize({ model }: { model: ConstrainedObjectModel }): string { var stringEnumValue = enumValue.ToString(); // C# converts booleans to uppercase True and False, which newtonsoft cannot understand var jsonStringCompliant = stringEnumValue == "True" || stringEnumValue == "False" ? stringEnumValue.ToLower() : stringEnumValue; -var jsonToken = JToken.Parse(jsonStringCompliant); -jo.Add("${prop.unconstrainedPropertyName}", jsonToken);`; +jo.Add("${prop.unconstrainedPropertyName}", JToken.FromObject(jsonStringCompliant, serializer));`; } return `if (value.${propertyAccessor} != null) { @@ -93,7 +92,7 @@ function renderDeserialize({ prop.property instanceof ConstrainedReferenceModel && prop.property.ref instanceof ConstrainedEnumModel ) { - toValue = `${prop.property.name}Extensions.To${prop.property.name}(jo["${prop.unconstrainedPropertyName}"])`; + toValue = `${prop.property.name}Extensions.To${prop.property.name}(jo["${prop.unconstrainedPropertyName}"].ToString())`; } return `if(jo["${prop.unconstrainedPropertyName}"] != null) { value.${propertyAccessor} = ${toValue}; diff --git a/test/generators/csharp/presets/__snapshots__/NewtonsoftSerializerPreset.spec.ts.snap b/test/generators/csharp/presets/__snapshots__/NewtonsoftSerializerPreset.spec.ts.snap index 3ea6419f36..698c143006 100644 --- a/test/generators/csharp/presets/__snapshots__/NewtonsoftSerializerPreset.spec.ts.snap +++ b/test/generators/csharp/presets/__snapshots__/NewtonsoftSerializerPreset.spec.ts.snap @@ -55,7 +55,7 @@ if(jo[\\"numberProp\\"] != null) { value.NumberProp = jo[\\"numberProp\\"].ToObject(serializer); } if(jo[\\"enumProp\\"] != null) { - value.EnumProp = EnumTestExtensions.ToEnumTest(jo[\\"enumProp\\"]); + value.EnumProp = EnumTestExtensions.ToEnumTest(jo[\\"enumProp\\"].ToString()); } if(jo[\\"objectProp\\"] != null) { value.ObjectProp = jo[\\"objectProp\\"].ToObject(serializer); @@ -88,8 +88,7 @@ if (value.EnumProp != null) var stringEnumValue = enumValue.ToString(); // C# converts booleans to uppercase True and False, which newtonsoft cannot understand var jsonStringCompliant = stringEnumValue == \\"True\\" || stringEnumValue == \\"False\\" ? stringEnumValue.ToLower() : stringEnumValue; -var jsonToken = JToken.Parse(jsonStringCompliant); -jo.Add(\\"enumProp\\", jsonToken); +jo.Add(\\"enumProp\\", JToken.FromObject(jsonStringCompliant, serializer)); } if (value.ObjectProp != null) { diff --git a/test/runtime/generic-input.json b/test/runtime/generic-input.json index 3d153ba97b..afe3656468 100644 --- a/test/runtime/generic-input.json +++ b/test/runtime/generic-input.json @@ -44,6 +44,9 @@ "type": "string" } } + }, + "enumTest": { + "enum": ["test", "test2"] } }, "patternProperties": { @@ -52,7 +55,6 @@ } }, "required": [ - "street_name", "house_number", "array_type" ] diff --git a/test/runtime/runtime-csharp.ts b/test/runtime/runtime-csharp.ts index 97f5c1c441..12598805c9 100644 --- a/test/runtime/runtime-csharp.ts +++ b/test/runtime/runtime-csharp.ts @@ -1,17 +1,43 @@ -import { CSHARP_JSON_SERIALIZER_PRESET, CSharpFileGenerator } from '../../'; +import { CSHARP_JSON_SERIALIZER_PRESET, CSHARP_NEWTONSOFT_SERIALIZER_PRESET, CSharpFileGenerator } from '../../'; import path from 'path'; import input from './generic-input.json'; -const generator = new CSharpFileGenerator({ - presets: [CSHARP_JSON_SERIALIZER_PRESET] -}); +async function generateJsonSerializer() { + const generator = new CSharpFileGenerator({ + presets: [CSHARP_JSON_SERIALIZER_PRESET] + }); + + await generator.generateToFiles( + input, + path.resolve( + // eslint-disable-next-line no-undef + __dirname, + './runtime-csharp/runtime-csharp/src/models/json_serializer' + ), + { namespace: 'com.mycompany.app.json_serializer' } + ); +} +async function generateNewtonsoft() { + const generator = new CSharpFileGenerator({ + presets: [CSHARP_NEWTONSOFT_SERIALIZER_PRESET] + }); + await generator.generateToFiles( + input, + path.resolve( + // eslint-disable-next-line no-undef + __dirname, + './runtime-csharp/runtime-csharp/src/models/newtonsoft' + ), + { namespace: 'com.mycompany.app.newtonsoft' } + ); +} -generator.generateToFiles( - input, - path.resolve( - // eslint-disable-next-line no-undef - __dirname, - './runtime-csharp/runtime-csharp/src/models/json_serializer' - ), - { namespace: 'com.mycompany.app.generic' } -); +async function generateEverything() { + await Promise.all( + [ + generateJsonSerializer(), + generateNewtonsoft() + ] + ) +} +generateEverything(); \ No newline at end of file diff --git a/test/runtime/runtime-csharp/runtime-csharp/runtime-csharp.csproj b/test/runtime/runtime-csharp/runtime-csharp/runtime-csharp.csproj index 7cb79cdc30..a16ebd4679 100644 --- a/test/runtime/runtime-csharp/runtime-csharp/runtime-csharp.csproj +++ b/test/runtime/runtime-csharp/runtime-csharp/runtime-csharp.csproj @@ -15,6 +15,7 @@ + @@ -24,6 +25,7 @@ + diff --git a/test/runtime/runtime-csharp/runtime-csharp/test/models/json_serializer/Address.cs b/test/runtime/runtime-csharp/runtime-csharp/test/models/json_serializer/Address.cs index 4275e8d7d7..bb50cce423 100644 --- a/test/runtime/runtime-csharp/runtime-csharp/test/models/json_serializer/Address.cs +++ b/test/runtime/runtime-csharp/runtime-csharp/test/models/json_serializer/Address.cs @@ -1,7 +1,7 @@ -using System.Text.Json; -using com.mycompany.app.generic; +using System.Text.Json; +using com.mycompany.app.json_serializer; -namespace runtime_csharp; +namespace runtime_csharp.json_serializer; public class AddressTests { @@ -18,13 +18,16 @@ public void TestSerializingFullModel() NestedObject nestedObject = new NestedObject(); nestedObject.Test = "test"; address.NestedObject = nestedObject; - address.StreetName = "test"; address.Marriage = true; address.Members = 2; address.HouseNumber = 1; address.ArrayType = new dynamic[] { 1, "test" }; + address.EnumTest = EnumTest.TEST; + address.AdditionalProperties = new Dictionary(); + address.AdditionalProperties.Add("test_not_used", 2); + string actualJsonString = JsonSerializer.Serialize(address); - string expectedJsonString = "{\"street_name\":\"test\",\"house_number\":1,\"marriage\":true,\"members\":2,\"array_type\":[1,\"test\"],\"nestedObject\":{\"test\":\"test\"}}"; + string expectedJsonString = "{\"house_number\":1,\"marriage\":true,\"members\":2,\"array_type\":[1,\"test\"],\"nestedObject\":{\"test\":\"test\"},\"enumTest\":\"test\",\"test_not_used\":2}"; Assert.That(actualJsonString, Is.EqualTo(expectedJsonString)); } } diff --git a/test/runtime/runtime-csharp/runtime-csharp/test/models/newtonsoft/Address.cs b/test/runtime/runtime-csharp/runtime-csharp/test/models/newtonsoft/Address.cs new file mode 100644 index 0000000000..896823abbb --- /dev/null +++ b/test/runtime/runtime-csharp/runtime-csharp/test/models/newtonsoft/Address.cs @@ -0,0 +1,35 @@ +using System.Text.Json; +using com.mycompany.app.newtonsoft; +using Newtonsoft.Json; + +namespace runtime_csharp.newtonsoft; + +public class AddressTests +{ + + [SetUp] + public void Setup() + { + } + + [Test] + public void TestSerializingFullModel() + { + Address address = new Address(); + NestedObject nestedObject = new NestedObject(); + nestedObject.Test = "test"; + address.NestedObject = nestedObject; + address.Marriage = true; + address.Members = 2; + address.HouseNumber = 1; + address.ArrayType = new dynamic[] { 1, "test" }; + address.EnumTest = EnumTest.TEST; + address.AdditionalProperties = new Dictionary(); + address.AdditionalProperties.Add("test_not_used", 2); + + string actualJsonString = JsonConvert.SerializeObject(address); + string expectedJsonString = "{\"house_number\":1.0,\"marriage\":true,\"members\":2,\"array_type\":[1,\"test\"],\"nestedObject\":{\"test\":\"test\"},\"enumTest\":\"test\",\"test_not_used\":2}"; + Assert.That(actualJsonString, Is.EqualTo(expectedJsonString)); + } +} +