question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Property names doesn't use Json [PropertyName] values with [FromQuery]

See original GitHub issue

When 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:closed
  • Created 4 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

5reactions
domaindrivendevcommented, Sep 13, 2019

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 that JsonPropertyAttributes 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:

public class PagingParameters
{
   [FromQuery(Name = "pn")]
   [JsonProperty(Name = "pageNo")]
    public int PageNo { get; set; }

   [FromQuery(Name = "ps")]
   [JsonProperty(Name = "pageNo")]
    public int PageSize { get; set; }
}

Swashbuckle wil honor this setup without the need for an operation filter.

1reaction
CaCTuCaTu4ECKuucommented, Sep 13, 2019

And also. As you mention that I expect lib to honor JsonPropertyAttributes. I expect that because the lib do it in all other cases except FromQuery, so why shouldn’t I, and others, expect it in that particular case.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found