-
Notifications
You must be signed in to change notification settings - Fork 10.3k
OpenApi convert to snackcase #60513
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Use |
Hi @martincostello, thank you for your response. Thanks |
You would configure the OpenAPI document to use snake case in the way I already described. builder.Services.ConfigureHttpJsonOptions(options =>
{
options.SerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower,
}); However, note that Microsoft.AspNetCore.OpenApi does not support Newtonsoft.Json. See #60458 for more details. |
Thank you 🙏 and noted |
builder.Services.AddControllers(options =>
{
options.ValueProviderFactories.Add(new SnakeCaseQueryValueProviderFactory());
}).AddJsonOptions(options =>
{
options.JsonSerializerOptions.PropertyNamingPolicy = new JsonSerializerSnakeCaseNamingPolicy();
options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter());
options.JsonSerializerOptions.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull;
options.JsonSerializerOptions.WriteIndented = false;
options.JsonSerializerOptions.ReferenceHandler = ReferenceHandler.IgnoreCycles;
});
builder.Services.ConfigureHttpJsonOptions(options =>
{
options.SerializerOptions.PropertyNamingPolicy = new JsonSerializerSnakeCaseNamingPolicy();
options.SerializerOptions.Converters.Add(new JsonStringEnumConverter());
options.SerializerOptions.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull;
options.SerializerOptions.WriteIndented = false;
options.SerializerOptions.ReferenceHandler = ReferenceHandler.IgnoreCycles;
}); public class JsonSerializerSnakeCaseNamingPolicy : JsonNamingPolicy
{
public override string ConvertName(string name)
{
return name.ToSnakeCase();
}
}
public static string ToSnakeCase(this string o) =>
SnakeCase().Replace(o, "$1_$2").ToLower();
[GeneratedRegex(@"(\w)([A-Z])")]
private static partial Regex SnakeCase(); This get to work, but this could be improve? |
Add a method to configure the options so you don't repeat it: builder.Services.AddControllers(options =>
{
options.ValueProviderFactories.Add(new SnakeCaseQueryValueProviderFactory());
}).AddJsonOptions(options => ConfigureJsonSerializer(options.JsonSerializerOptions));
builder.Services.ConfigureHttpJsonOptions(options => ConfigureJsonSerializer(options.SerializerOptions));
static void ConfigureJsonSerializer(JsonSerializerOptions options)
{
options.PropertyNamingPolicy = new JsonSerializerSnakeCaseNamingPolicy();
options.Converters.Add(new JsonStringEnumConverter());
options.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull;
options.WriteIndented = false;
options.ReferenceHandler = ReferenceHandler.IgnoreCycles;
} For the second snippet, use the built-in snake case options I already linked you to ( |
This issue has been resolved and has not had any activity for 1 day. It will be closed for housekeeping purposes. See our Issue Management Policies for more information. |
Is there an existing issue for this?
Is your feature request related to a problem? Please describe the problem.
Microsoft.AspNetCore.OpenApi 9.0.2
Describe the solution you'd like
I want request and response in snackcase.
Additional context
No response
The text was updated successfully, but these errors were encountered: