Valid value for TimeSpan property to post in web api? C#
Asked Answered
S

1

7

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

Soapwort answered 10/3, 2020 at 17:30 Comment(2)
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?Dorinedorion
What framework version do you use?Dietetic
O
17

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")
                });
Olivas answered 3/3, 2021 at 21:54 Comment(2)
Whole code is: services.AddSwaggerGen( c => c.MapType<TimeSpan?>(() => new OpenApiSchema { Type = "string", Example = new OpenApiString("00:00:00") }) ); That should be added in ConfigureServices, in Startup class.Curley
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 ?Painterly

© 2022 - 2024 — McMap. All rights reserved.