Skip to content

Commit e96cb52

Browse files
author
Chris Podmore
committed
Resolves #21 where stringified enum's are not serialized correctly
1 parent f0f4bb9 commit e96cb52

File tree

2 files changed

+43
-5
lines changed

2 files changed

+43
-5
lines changed

Tocsoft.GraphQLCodeGen.Cli/Templates/cs/CSharp.template

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,14 +75,51 @@ namespace {{Namespace}}
7575
{{!# ClientClass}}
7676
public sealed class {{ClassName}} : I{{ClassName}}
7777
{
78+
{{ifTemplate 'StringifyEnums' 'true'}}
79+
internal interface IStringifiedEnum
80+
{
81+
string Value { get; }
82+
}
83+
{{/ifTemplate}}
84+
7885
private static JsonSerializerSettings jsonSettings = new JsonSerializerSettings()
7986
{
8087
Converters =
8188
{
89+
{{ifTemplate 'StringifyEnums' 'true'}}
90+
new CustomJsonStringifiedEnumConverter(),
91+
{{/ifTemplate}}
8292
new Newtonsoft.Json.Converters.StringEnumConverter(camelCaseText: false),
8393
{{ > jsonConverters }}
8494
}
8595
};
96+
97+
{{ifTemplate 'StringifyEnums' 'true'}}
98+
internal class CustomJsonStringifiedEnumConverter : JsonConverter
99+
{
100+
public override bool CanConvert(Type objectType)
101+
{
102+
return typeof(IStringifiedEnum).IsAssignableFrom(objectType);
103+
}
104+
105+
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
106+
{
107+
return reader.Value == null ? null : reader.Value.ToString();
108+
}
109+
110+
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
111+
{
112+
if (value is IStringifiedEnum v)
113+
{
114+
writer.WriteValue(v.Value);
115+
}
116+
else
117+
{
118+
writer.WriteNull();
119+
}
120+
}
121+
}
122+
{{/ifTemplate}}
86123

87124
private string SerializeBody(GraphQLRequest request)
88125
{
@@ -153,7 +190,7 @@ namespace {{Namespace}}
153190
public sealed class {{pascalCase Name}} {{#each~ Interfaces}}{{~#if @first}}: {{else}}, {{/if~}}I{{pascalCase .}}{{~/each}}
154191
{
155192
{{#each Fields}}
156-
[JsonProperty("{{Name}}")]
193+
[JsonProperty("{{Name}}")]
157194
public {{> TypeReference Type}} {{pascalCase Name}} { get; set; }
158195
{{/each}}
159196
}
@@ -170,7 +207,7 @@ namespace {{Namespace}}
170207
{{!# Enum}}
171208

172209
{{ifTemplate 'StringifyEnums' 'true'}}
173-
public class {{pascalCase Name}}
210+
public class {{pascalCase Name}} : IStringifiedEnum
174211
{
175212
static Dictionary<string, {{pascalCase Name}}> lookup = new Dictionary<string, {{pascalCase Name}}>();
176213

Tocsoft.GraphQLCodeGen.Tests/StringifiedEnums.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public async Task SettingExplicitlyTrue()
3131

3232
var code = generator.GeneratedCode;
3333

34-
Assert.Contains(@"public class Episode", code);
34+
Assert.Contains(@"public class Episode : IStringifiedEnum", code);
3535
}
3636

3737
[Fact]
@@ -49,10 +49,9 @@ public async Task SettingImplicitlyTrue()
4949

5050
var code = generator.GeneratedCode;
5151

52-
Assert.Contains(@"public class Episode", code);
52+
Assert.Contains(@"public class Episode : IStringifiedEnum", code);
5353
}
5454

55-
5655
[Fact]
5756
public async Task SettingExplicitlyFalse()
5857
{
@@ -70,6 +69,8 @@ public async Task SettingExplicitlyFalse()
7069

7170
var code = generator.GeneratedCode;
7271

72+
Assert.DoesNotContain(@"IStringifiedEnum", code);
73+
7374
Assert.Contains(@"public enum Episode", code);
7475
}
7576
}

0 commit comments

Comments
 (0)