Replies: 4 comments 2 replies
-
Facing this exact issue today. |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
I actually realized that my issue was the query string working differently than the global serialization options. As that uses model binding vs json serialization options. When I changed everything to snake case the document changed the request parameter to snake case as well. But it doesn’t deserialize correctly. |
Beta Was this translation helpful? Give feedback.
-
I have a minimal API that receives webhook events from GitHub. If I use the configuration that @davidfowl suggested, I'm able to deserialize the body successfully into a C# POCO type: builder.Services.ConfigureHttpJsonOptions(options =>
{
options.SerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower;
options.SerializerOptions.PropertyNameCaseInsensitive = true;
}); app.MapPost(
"/event-handler",
async (
[FromServices] IHandlerService handlerService,
[FromBody] Payload payload
) => await handlerService.HandleAsync(payload)); My payload is just simple C# record types sealed record Payload
{
public required string Action { get; init; }
public CheckSuite? CheckSuite { get; init; }
public PullRequest? PullRequest { get; init; }
}
sealed record CheckSuite
{
public required long Id { get; init; }
}
internal sealed record PullRequest
{
public required long Id { get; init; }
} Ideally, I want to use a [JsonSourceGenerationOptions(
PropertyNamingPolicy = JsonKnownNamingPolicy.SnakeCaseLower,
PropertyNameCaseInsensitive = true,
UseStringEnumConverter = true)]
[JsonSerializable(typeof(Payload))]
internal sealed partial class GitHubJsonSerializerContext : JsonSerializerContext; // Is invalid due to the property being read-only
builder.Services.ConfigureHttpJsonOptions(
options => options.SerializerOptions = GitHubJsonSerializerContext.Default.Options
);
builder.Services.Configure<JsonSerializerOptions>(
static o => o.TypeInfoResolver = GitHubJsonSerializerContext.Default
); Am I forgetting something obvious? The builder.Services.ConfigureHttpJsonOptions(
static options => options.SerializerOptions.TypeInfoResolver = GitHubJsonSerializerContext.Default
); Unfortunately this leaves the naming convention to |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi!
I'm writing an application that requires both request paramenters and response body to be in snake_case
With the following code I've managed to serialize the response, but it doesn't affect the request deserialization.
Worth say that what I mean by request isn't the body, but the query parameters. At the endpoint I'm getting the query string with the
AsParameters
I'm running 8.0.100-preview.6.23330.14
Beta Was this translation helpful? Give feedback.
All reactions