5

I have a TimeSpan property

public TimeSpan Time { get; set; }

Currently using swagger to test API Trying to figure out what value value to pass here

"time": {},

Tried:

"time": {"01:01:01"},

"time": {"01:01"},

"time": "01:01:01",

"time": 01:01:01,

all returning 401 code

  • 1
    401 indicates the request lacks valid authentication credentials. Could you provide more details about what API you are testing, what kind of authentication it requires and how you’re creating the request? – NGambit Mar 10 '20 at 21:27
  • What framework version do you use? – Mohammad Barbast Jan 23 '21 at 11:32

1 Answers1

12

On this another page you will find the solution to the problem: .Net Core 3.0 TimeSpan deserialization error - Fixed in .Net 5.0.

In a complementary way, add this in the swagger

c.MapType<TimeSpan>(() => new OpenApiSchema
                {
                    Type = "string",
                    Example = new OpenApiString("00:00:00")
                });
SebastianJavier
  • 121
  • 1
  • 3
  • 2
    Whole code is: services.AddSwaggerGen( c => c.MapType(() => new OpenApiSchema { Type = "string", Example = new OpenApiString("00:00:00") }) ); That should be added in ConfigureServices, in Startup class. – Dušan Nov 06 '21 at 23:28
  • But why does swagger schema shows `{ "TotalDuration": { "Ticks": 0, "Days": 0, "Hours": 0, "Milliseconds": 0, "Minutes": 0, "Seconds": 0, "TotalDays": 0, "TotalHours": 0, "TotalMilliseconds": 0, "TotalMinutes": 0, "TotalSeconds": 0 } }` this ? Any idea how to fix this ? – Kumar Jun 27 '23 at 13:17