Reputation: 47
I'm trying to modify the sample json shown to test in swagger if a POST works or doesn't work. From where should I modify this? That is, I would have to modify the json that is displayed when we press "Try out".
To be more precise, the json to test the post is the following:
{
"Cliente": 0,
"CantidadRegistros": 0,
"TotalesPrimerVencimiento": 0,
"TotalesSegundoVencimiento": 0,
"Detalle": [
{
"Consorcio": 0,
"UnidadFuncional": 0,
"Periodo": "string",
"Propietario": "string",
"Ubicacion": "string",
"Email": "string",
"FechaPrimerVencimiento": "2021-12-15",
"ImportePrimerVencimiento": 0,
"FechaSegundoVencimiento": "2021-12-15",
"ImporteSegundoVencimiento": 0,
"CodigoDePagoElectronico": "string",
"CodigoDeBarras": "string"
}
]
}
What I would like to modify is the format in which the date is displayed. Currently it is dd-mm-yyyy and I want to modify it to dd/mm/yyyy
Tried modifying this with the following DisplayFormat code but it didn't work for me:
[JsonPropertyName("FechaSegundoVencimiento")]
[FromQuery(Name = "FechaSegundoVencimiento")]
[ModelBinder(BinderType = typeof(DateTimeModelBinder))]
[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
[DataType(DataType.Date)]
public DateTime? DueDate2 { get; set; }
I hope your help! Thanks!
Upvotes: 0
Views: 2456
Reputation: 151674
The supported and out of the box way to do this, is by using XML comments, generating documentation files on build and having Swashbuckle read these comments:
Model:
public class Product
{
/// <summary>
/// The name of the product
/// </summary>
/// <example>Men's basketball shoes</example>
public string Name { get; set; }
// ...
Startup:
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", ...);
var filePath = Path.Combine(System.AppContext.BaseDirectory, "Your.App.xml");
c.IncludeXmlComments(filePath);
}
If memory serves me right, this generates one example per model property. So if you reuse models between different APIs and want different examples, you'll need to branch out to other libraries.
I have used mattfrear/Swashbuckle.AspNetCore.Filters, where you can annotate your methods with [SwaggerRequestExample(typeof(...)]
and [SwaggerResponseExample(typeof(...))]
, where ...
can provide an example object that will be serialized to JSON, but note that in various cases they recommend to use Swashbuckle's built-in way.
However, you say:
Currently it is dd-mm-yyyy and I want to modify it to dd/mm/yyyy
Don't. There's no predefined date format for JSON, but most APIs accept ISO-8601(ish) formats by default. Don't go make that culture-specific.
Upvotes: 1