Property names doesn't use Json [PropertyName] values with [FromQuery]
See original GitHub issueWhen using Model as parameter for controller Actions with [FromQuery] attribute - parameter names in Swagger UI shown as model property names.
public class QueryModelRenameToJsonPropertyNameFilter : IOperationFilter
{
public void Apply(Operation operation, OperationFilterContext context)
{
foreach (var param in context.ApiDescription.ParameterDescriptions.Where(d => d.Source.Id == "Query"))
{
var toRename = ((DefaultModelMetadata)param.ModelMetadata)
.Attributes.PropertyAttributes?.Any(x => x is JsonPropertyAttribute);
if (toRename == true)
{
var opParam = operation.Parameters.SingleOrDefault(p => p.Name == param.Name);
if (opParam != null)
{
JsonPropertyAttribute attr = (JsonPropertyAttribute)((DefaultModelMetadata) param.ModelMetadata)
.Attributes.PropertyAttributes.First(x => x is JsonPropertyAttribute);
opParam.Name = attr.PropertyName;
}
}
};
}
}
If someone need this feel free to use
Issue Analytics
- State:
- Created 4 years ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
Generated UI doc not honoring JsonProperty value using ...
I have a .NET Core Web API service that will receive requests and take my object as input FromQuery . I wanted to...
Read more >How to customize property names and values with System. ...
Learn how to customize property names and values when serializing with System.Text.Json in .NET.
Read more >Model Binding
Mismatched Property Names. When the incoming JSON field name does not match with the request DTO property name, you can use the [JsonPropertyName]...
Read more >How to Pass Parameters With a GET Request in ASP.NET ...
We use the [FromRoute(Name = "manufacturer")] attribute to ensure that the value for the brand parameter is retrieved from the route segment.
Read more >Parameter Binding in ASP.NET Web API
Query string parameter name and action method parameter name must be the same (case-insensitive). If names do not match, then the values of...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
The issue title (and your filter implementation) seems to indicate that you’re expecting Swashbuckle to honor
JsonPropertyAttributes
for properties that are bound to query parameters. I’m just making the point thatJsonPropertyAttributes
don’t have any meaning in this context, independently of Swashbuckle, and therefore I don’t believe Swashbuckle should honor them in this way.If you’re using the same model for both query parameters and JSON bodies then I can see how your filter affords some convenience. However, to keep your query string serialization decoupled from JSON serialization, you could also do the following:
Swashbuckle wil honor this setup without the need for an operation filter.
And also. As you mention that I expect lib to honor
JsonPropertyAttributes
. I expect that because the lib do it in all other cases exceptFromQuery
, so why shouldn’t I, and others, expect it in that particular case.