The API I am building is a dot.net web API project, and the swagger packages (swagger,swaggergen, and swaggerUI) are all the very latest off the nuget package registry. All settings are taken straight from instructional tutorials. For what it is worth, I don't think that a majority of all this configuration detail is relevant, as I suspect this problem is related to some serialization or deserialization of the swagger manifest that is generated. This might be a swagger issue or a swagger UI issue, not sure which. Starting here since it manifests in the UI.
services.AddSwaggerGen(options =>
options.SwaggerDoc("v1", new OpenApiInfo
Version = "v1",
Swagger-UI configuration options:
(from configure())
app.UseSwagger();
app.UseSwaggerUI();
// notes: https://learn.microsoft.com/en-us/aspnet/core/tutorials/getting-started-with-swashbuckle?view=aspnetcore-7.0&tabs=visual-studio
app.UseSwaggerUI(options =>
options.SwaggerEndpoint("/swagger/v1/swagger.json", "v1");
options.RoutePrefix = string.Empty;
Describe the bug you're encountering
When I set up an endpoint to accept a POST with form data and set a parameter to have a default int value, it renders this in the web UI properly, unless that value is 0.
To reproduce...
Steps to reproduce the behavior:
drop the following code into a dot.net API project with the latest swagger nuget packages installed:
[HttpPost("test/post/create")]
public void SamplePost(
[FromForm] int Int1 = 1,
[FromForm] int Int2 = 0,
[FromForm] int Int3 = -1,
[FromForm] int Int4 = int.MaxValue,
[FromForm] int Int5 = int.MinValue,
[FromForm] float Float1 = 1,
[FromForm] float Float2 = 0,
[FromForm] float Float3 = -1,
[FromForm] float Float4 = float.MaxValue,
[FromForm] float Float5 = float.MinValue,
[FromForm] float Float6 = float.Epsilon,
//[FromForm] float Float7 = float.PositiveInfinity,
//[FromForm] float Float8 = float.NegativeInfinity,
//[FromForm] float Float9 = float.NaN,
[FromForm] double Double1 = 1,
[FromForm] double Double2 = 0,
[FromForm] double Double3 = -1,
[FromForm] double Double4 = double.MaxValue,
[FromForm] double Double5 = double.MinValue,
[FromForm] decimal Decimal1 = 1,
[FromForm] decimal Decimal2 = 0,
[FromForm] decimal Decimal3 = -1,
[FromForm] decimal Decimal4 = decimal.MaxValue,
[FromForm] decimal Decimal5 = decimal.MinValue)
// foo?
This code snippet will allow you to quickly see the problem. I added a few extra tests of corner cases to validate that zero was the only value that was affected.
Load UI
the form field that is generated by swagger will have the defaults auto-populated in each form field, except if the value is zero.
Expected behavior
Any default numeric value should be displayed in a form field,
Screenshots
Additional context or thoughts
I suspect this might exist across multiple versions of swagger. It might be a result of how default numeric values are being serialized by your xml generator / serializer. (perhaps you need to change some serializer flags?). It is very interesting how all numeric values behave in the the same way.
I tested the values for positive Infinity, negative infinity, and NaN. Swagger blows up with a 500 error if you include them when you build and debug. This is a lot more fuzzy, as I am not sure if those are intended to be supported values. (Working as intended?) Epsilon looks ok, though. You might tweak swagger to generate a cleaner error if you are going to not support NaN, +infinity, and -Infinity.
The zero problem looks like a clear bug, these other values might be a bug or a secret feature request. You can decide