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.

How to customise the name of property?

See original GitHub issue

I have a WebApi controller action that takes a string authToken parameter, which it reads from the Auth-Token header. swagger-ui code-gens this as authToken which means requests via swagger-ui don’t write the header correctly.

How can I customise the name of this parameter so that swagger-ui does the right thing?

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
domaindrivendevcommented, Nov 26, 2014

The exact implementation for this depends on how you’ve implemented the header-to-parameter binding in WebApi. I did it by creating two custom classes - FromHeaderAttribute (derived from ParameterBindingAttribute) and HeaderParameterBinding (derived from HttpParameteBinding). I can share the details if needed but the net result gave me a re-usable attribute (similar to FromUri and FromBody) for binding string parameters to header values. For example …

public int Create([FromHeader(Name = "Auth-Token")]string authToken, Customer customer)

Then, to describe these parameters correctly in the resulting Swagger, I implemented and wired up a custom IOperationFilter (see readme) that updates the descriptions accordingly:

// HandleFromHeaderParams.cs
public class HandleFromHeaderParams : IOperationFilter
{
    public void Apply(Operation operation, SchemaRegistry schemaRegistry, ApiDescription apiDescription)
    {
        var fromHeaderParams = apiDescription.ActionDescriptor.GetParameters()
            .Where(param => param.GetCustomAttributes<FromHeaderAttribute>().Any())
            .ToArray();

        foreach (var param in fromHeaderParams)
        {
            var fromHeaderAttribute = param.GetCustomAttributes<FromHeaderAttribute>().First();
            var operationParam = operation.parameters.First(p => p.name == param.ParameterName);

            operationParam.name = fromHeaderAttribute.Name;
            operationParam.@in = "header";
        }
    }
}

// SwaggerConfig.cs
c.OperationFilter<HandleFromHeaderParams>();

This worked first go … the swagger description and hence swagger-ui described the parameters correctly and the correct headers get sent when a request is made through the UI.

Try it out and let me know how it goes

Richie

0reactions
DwarfPortercommented, Oct 8, 2019

It’s very helpful for Swashbuckle version 4, however, in version 5.0.0-rc4 the interface is changed to interface IOperationFilter { void Apply(OpenApiOperation operation, OperationFilterContext context);

and not clearly how to make the parameters

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Change the Name on Property Title Deeds in Texas
If the current owner's name has been legally changed, you may want to file a deed showing the name change. A Warranty Deed...
Read more >
How to Change the Title of Your Home
It's possible to get a title change on your home's deed, though this is different from changing the name on the mortgage. The...
Read more >
How to Change the Name of the Owner on a House Title
You will then need to get the deed notarized. Choose the Right Deed. You must use a deed to do make changes to...
Read more >
Reasons to Change the Name on a House Deed and (How ...
Give everyone a heads-up on your name change. · Be prepared to show alternate forms of identification. · To file a quitclaim, you...
Read more >
How to Change the Name on a Property Title | Real Estate Law
How to Change the name on a Property Title | Real Estate Law | LegalShield Are you the new owner of real estate...
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