C
C#2mo ago
Core

✅ .NET 8 enum to string serialization throws an exception

Hello, Following the docs, a custom JsonSerializerContext is applied on the controllers, yet an exception is thrown.
c#
public sealed record GetLinkMetricsRequest
{
[FromRoute]
public string Id { get; set; }

[FromQuery]
public TimePeriodUnit? TimeUnit { get; set; } = TimePeriodUnit.Day;

[FromQuery]
public int? Units { get; set; }
}

[JsonSourceGenerationOptions(UseStringEnumConverter = true)]
[JsonSerializable(typeof(GetLinkMetricsRequest))]
public partial class TimePeriodUnitJsonContext : JsonSerializerContext;


// applied to the Controllers
var options = new JsonSerializerOptions
{
TypeInfoResolver = JsonTypeInfoResolver.Combine(ContextA.Default, ContextB.Default, ContextC.Default);
};
c#
public sealed record GetLinkMetricsRequest
{
[FromRoute]
public string Id { get; set; }

[FromQuery]
public TimePeriodUnit? TimeUnit { get; set; } = TimePeriodUnit.Day;

[FromQuery]
public int? Units { get; set; }
}

[JsonSourceGenerationOptions(UseStringEnumConverter = true)]
[JsonSerializable(typeof(GetLinkMetricsRequest))]
public partial class TimePeriodUnitJsonContext : JsonSerializerContext;


// applied to the Controllers
var options = new JsonSerializerOptions
{
TypeInfoResolver = JsonTypeInfoResolver.Combine(ContextA.Default, ContextB.Default, ContextC.Default);
};
The exception:
An unexpected error occurred. See the inner exception for details. System.NotSupportedException: JsonTypeInfo metadata for type 'AnalyticsService.Api..' was not provided by TypeInfoResolver of type 'System.Text.Json.Serialization.Metadata.JsonTypeInfoResolverWithAddedModifiers'. If using source generation, ensure that all root types passed to the serializer have been annotated with 'JsonSerializableAttribute', along with any types that might be serialized polymorphically.
An unexpected error occurred. See the inner exception for details. System.NotSupportedException: JsonTypeInfo metadata for type 'AnalyticsService.Api..' was not provided by TypeInfoResolver of type 'System.Text.Json.Serialization.Metadata.JsonTypeInfoResolverWithAddedModifiers'. If using source generation, ensure that all root types passed to the serializer have been annotated with 'JsonSerializableAttribute', along with any types that might be serialized polymorphically.
I have followed the docs: https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json/source-generation#combine-source-generators https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json/source-generation#blanket-policy
How to use source generation in System.Text.Json - .NET
Learn how to use source generation in System.Text.Json.
9 Replies
Pobiega
Pobiega2mo ago
'AnalyticsService.Api..' doesn't strike me as a valid type identifier especially that double dot at the end is mighty suspicious whats the fully qualified name for GetLinkMetricsRequest?
Core
CoreOP2mo ago
sorry, I had no idea that's relevant
c#
'AnalyticsService.Api.Features.LinkMetrics.Models.GetLinkMetricsResult`1[AnalyticsService.Api.Features.LinkMetrics.Models.LinkClicksByDateResult]'
c#
'AnalyticsService.Api.Features.LinkMetrics.Models.GetLinkMetricsResult`1[AnalyticsService.Api.Features.LinkMetrics.Models.LinkClicksByDateResult]'
Pobiega
Pobiega2mo ago
ok so its in that namespace at least but that seems to be a generic version? GetLinkMetricsResult<T>
Core
CoreOP2mo ago
That's being returned, specifically GetLinkMetricsResult<LinkClicksByDateResult>
c#
public class GetLinkMetricsResult<T>
{
public IEnumerable<T> Metrics { get; set; } = null!;
}

public class LinkClicksByDateResult : ILinkMetricsResult
{
public int Clicks { get; set; }
public LocalDate Date { get; set; }
}
c#
public class GetLinkMetricsResult<T>
{
public IEnumerable<T> Metrics { get; set; } = null!;
}

public class LinkClicksByDateResult : ILinkMetricsResult
{
public int Clicks { get; set; }
public LocalDate Date { get; set; }
}
But there is no enum being used here and it is the response
Pobiega
Pobiega2mo ago
but that type is what the serializer cant serialize thats the error you are getting meaning it (probably) handles the request fine, its the result thats failing
Core
CoreOP2mo ago
Yes, the result is failing the request is serialized
Pobiega
Pobiega2mo ago
okay, so add the result type to your serializer context?
Core
CoreOP2mo ago
Now it works, thank you, I still don't understand what has the response to do with the serializer $close
MODiX
MODiX2mo ago
If you have no further questions, please use /close to mark the forum thread as answered
Want results from more Discord servers?
Add your server