Skip to content

Commit 2b5adc3

Browse files
Allow reading numeric values in the JSON body for string properties like ProductPrice (Numeric(10,2)) in POST requests to the Product service. (#1112)
1 parent c6337a5 commit 2b5adc3

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

dotnet/src/dotnetcore/GxNetCoreStartup/Startup.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,13 @@ private void RegisterControllerAssemblies(IMvcBuilder mvcBuilder)
281281
{
282282
mvcBuilder.AddJsonOptions(options => options.JsonSerializerOptions.PropertyNameCaseInsensitive = false);
283283
}
284+
285+
mvcBuilder.AddJsonOptions(options =>
286+
{
287+
options.JsonSerializerOptions.NumberHandling = System.Text.Json.Serialization.JsonNumberHandling.AllowReadingFromString;
288+
options.JsonSerializerOptions.Converters.Add(new StringConverter());
289+
});
290+
284291
mvcBuilder.ConfigureApiBehaviorOptions(options =>
285292
{
286293
options.InvalidModelStateResponseFactory = context =>

dotnet/src/dotnetframework/GxClasses/Domain/GxCollections.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3119,5 +3119,26 @@ public override void Write(Utf8JsonWriter writer, bool value, JsonSerializerOpti
31193119
writer.WriteBooleanValue(value);
31203120
}
31213121
}
3122+
public class StringConverter : JsonConverter<string>
3123+
{
3124+
public override string Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
3125+
{
3126+
if (reader.TokenType == JsonTokenType.Number)
3127+
{
3128+
if (reader.TryGetInt32(out int l))
3129+
return l.ToString();
3130+
else if (reader.TryGetDecimal(out decimal d))
3131+
return d.ToString();
3132+
else
3133+
return reader.GetDouble().ToString();
3134+
}
3135+
return reader.GetString();
3136+
}
3137+
3138+
public override void Write(Utf8JsonWriter writer, string value, JsonSerializerOptions options)
3139+
{
3140+
writer.WriteStringValue(value);
3141+
}
3142+
}
31223143
#endif
31233144
}

0 commit comments

Comments
 (0)