Bug in ServiceApi 7.1.1 (and earlier)

Vote:
 

There is a bug in ServiceApi when using System.Text.Json as default serializer with camelCase as formatting. Using Newtonsoft works as expected.

After some digging I the cause. JsonSerializerOptions in method GetSerializerOptions is not instanciated correctly while using camelCase. It is using the default constructor which will use PascalCase as default.

The options should set camelCase as PropertyNamingPolicy or use another constructor that takes default values.

private static object GetOptions()
	{
		if (ServiceProviderExtensions.TryGetExistingInstance<IActionResultExecutor<JsonResult>>(ServiceLocator.Current, out var instance) && instance.GetType().FullName.Contains("Newtonsoft"))
		{
			if (ServiceProviderExtensions.GetInstance<ServiceApiOptions>(ServiceLocator.Current).JsonPropertyNaming.Value == JsonPropertyNaming.PascalCase)
			{
				return new JsonSerializerSettings();
			}
			return new JsonSerializerSettings
			{
				ContractResolver = new DefaultContractResolver
				{
					NamingStrategy = new CamelCaseNamingStrategy()
				}
			};
		}
		return GetSerializerOptions();
	}

	private static JsonSerializerOptions GetSerializerOptions()
	{
		JsonSerializerOptions jsonSerializerOptions = new JsonSerializerOptions();
		if (ServiceProviderExtensions.GetInstance<ServiceApiOptions>(ServiceLocator.Current).JsonPropertyNaming.Value == JsonPropertyNaming.PascalCase)
		{
			jsonSerializerOptions.PropertyNamingPolicy = null;
		}
		return jsonSerializerOptions;
	}
#332133
Oct 29, 2024 10:18
Vote:
 

I will file a bug to Commerce team. Thanks for bringing this into our attention and apologies for the inconvenience.

#332142
Oct 29, 2024 16:57
Quan Mai - Oct 30, 2024 13:41
Bug is COM-18873 (not yet public)
Oskar Zetterberg - Oct 30, 2024 13:44
Thanks!
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.