Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.Net Core Swagger How to ignore default parameter values

Is there a way to "suppress" the way that SwaggerUI pre-selects default values for optional parameters? We still want these parameters to be included in the generated documentation, just without a value selected. This is the way it worked within SwaggerUI 2.x, but it looks like this is no longer the case with 3.x

Edit - Adding example to clarify my question.

As an example - let's say that you have the following endpoint:

[HttpGet]
public dynamic GetSomething(
     string filter1, 
     string filter2, 
     bool shouldIFilter = false, 
     SomeEnum enumValue = SomeEnum.Whatever)

In swagger, when you click on "Try it out", the shouldIFilter and enumValue input fields will have the default values as specified in the method signature already selected. Instead, I want these fields to not have any value selected, so that they are not included in the query when you click on execute.

Basically, the request url should look like: http://some.url.com/v3/search?filter1=afilter&filter2=anotherfilter

(without the &shouldIFilter=false&enumValue=Whatever)

--- EDIT: 05/20/2019 ---

Taking Helder's suggestion below I tried injecting js to handle this. The first thing that I found is that the input fields do not exist until the "Try it out" button is clicked. And this button does not exist until you expand the specific Operation in the list!?! However, after wiring up an observer to set the values as I wanted when the elements were added to the DOM I am able to successfully change the selected value.

But when you click on Execute, these values refresh to match the original spec - and my js does not fire again to change the values that I want them to be.

I am starting to think that maybe this isn't possible. Though I am thinking that I may try and create a SwaggerUI plugin that might be able to do this - but I do not anticipate a lot of success there either.

Anyone else have any ideas?

like image 736
Justin Greywolf Avatar asked Dec 17 '25 20:12

Justin Greywolf


1 Answers

To close the loop on this I wanted to share what I ended up doing to resolve this issue (though we have not decided if we are going to actually move forward yet).

Trying to inject JS ended up being too much hassle, due to Swagger UI 3.x using react - I was able to change the selected values in the UI for the specific parameters, but once you clicked on "execute" they "reset" back to the default values.

Instead, I ended up adding an IParameterFilter to change the parameter defaults, and made sure that each parameter was otherwise documented correctly to display what the default value is

public void Apply(OpenApiParameter parameter, ParameterFilterContext context)
{
   if (parameter.In != ParameterLocation.Query) return;

   // readonly list to limit which parameters have this applied
   if (_parametersToUnSet.Contains(parameter.Name))
   {
       parameter.Description = $"*Default value*: {parameter.Schema.Default}";
       parameter.Schema.Default = null;
   }
 }
like image 68
Justin Greywolf Avatar answered Dec 19 '25 12:12

Justin Greywolf



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!