-
-
Notifications
You must be signed in to change notification settings - Fork 196
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: newtonsoft could not handle enum values (#1731)
- Loading branch information
1 parent
8ca9004
commit 8787210
Showing
7 changed files
with
91 additions
and
25 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); |
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
35 changes: 35 additions & 0 deletions
35
test/runtime/runtime-csharp/runtime-csharp/test/models/newtonsoft/Address.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,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<string, dynamic>(); | ||
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)); | ||
} | ||
} | ||
|