0

Is there a chance to tell swagger to take default value from init not from attribute [DefaultValue]?

public class MyClass
{
    public Guid RowId { get; set; } = Guid.NewGuid();
    public int? ExampleInt { get; set; } = 1;
    public string? ExampleText { get; set; } = string.Empty;
}

I don't want to type all default values twice.

Helen
  • 87,344
  • 17
  • 243
  • 314
TronComputers
  • 304
  • 2
  • 13
  • What library do you use - Swashbuckle, Swashbuckle.AspNetCore, NSwag, Swagger-Net, something else? – Helen Nov 10 '22 at 12:53
  • Swashbuckle.AspNetCore – TronComputers Nov 10 '22 at 21:38
  • What effect do you want? Is there anything you want in [this link](https://github.com/domaindrivendev/Swashbuckle.AspNetCore)? – Chen Nov 11 '22 at 09:30
  • The only way to autogenerate default value in swagger ui is to use attribute `[DefaultValue]`, but it has no efect on object so I need to use inline initialization, but then it has no effect on swagger.What I have to do is write both inline init for object and attribute for default value to show up in swagger ui. I don't want to do it twice. It is a waste of time. – TronComputers Nov 11 '22 at 21:48
  • Like all attributes, `[DefaultValue]` is meta data, you must manually set actual default values. Maybe you can only set it twice. Or refer to [this link](https://stackoverflow.com/a/17232853/18789859) to customize the method, but it is dangerous, we do not recommend that you use. – Chen Nov 15 '22 at 09:29
  • This is an interesting idea. You might want to share this as additional inference that can be added in [this issue on the ASP.NET repo](https://github.com/dotnet/aspnetcore/issues/44232). – Safia Abdalla Dec 01 '22 at 18:37

0 Answers0