C
C#2y ago
uselessxp

✅ RestAPI getting an error while testing (TimeSpan related)

I thought to have solved this, but it's not. As title, I'm testing some written RestAPI (still in development) for a service. The project is in .NET 7.0 There are few fields to type plus a field that will put in a DB hours and minutes, example 10:00 Well, in the model definition I have this:
public TimeSpan TestHour { get; set; }
public TimeSpan TestHour { get; set; }
Well, when I launch the debug, and it open Swaggler, I type all fields, and finally I have to write this TestHour. I tried with: - 10:00 - 10:00:00 - 360000000000 - 360000000000, 0, 0 None of those worked, and I get the following error from Swaggler: The JSON value could not be converted to System.TimeSpan Someone knows what could be? Am I doing something wrong, or maybe something doesn't works with this .NET ver?
32 Replies
uselessxp
uselessxp2y ago
Well, I tried changing definition to
public TimeOnly TestHour { get; set; }
public TimeOnly TestHour { get; set; }
Also if I not was asked to change models, I made it for try understand if my input was bad, then, now it clearly ask me for "Hour" and "Minutes", I put "10" and "0" and I get the same error with the difference that it's TimeOnly related 🤔
Anton
Anton2y ago
so the default format is "TestHour": "10:00:00"
Tvde1
Tvde12y ago
"10:00:00" should be working
MODiX
MODiX2y ago
tebeco#0205
REPL Result: Success
var foo = """
{
"bar": "10:00:00"
}
""";
record Foo(string Bar);
System.Text.Json.JsonSerializer.Deserialize<Foo>(foo)
var foo = """
{
"bar": "10:00:00"
}
""";
record Foo(string Bar);
System.Text.Json.JsonSerializer.Deserialize<Foo>(foo)
Result: Foo
{
"bar": null
}
{
"bar": null
}
Compile: 587.495ms | Execution: 74.442ms | React with ❌ to remove this embed.
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Anton
Anton2y ago
Bar -> bar
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
MODiX
MODiX2y ago
tebeco#0205
REPL Result: Success
var foo = """
{
"bar": "10:00:00"
}
""";
record Foo(string bar);
System.Text.Json.JsonSerializer.Deserialize<Foo>(foo)
var foo = """
{
"bar": "10:00:00"
}
""";
record Foo(string bar);
System.Text.Json.JsonSerializer.Deserialize<Foo>(foo)
Result: Foo
{
"bar": "10:00:00"
}
{
"bar": "10:00:00"
}
Compile: 555.397ms | Execution: 73.660ms | React with ❌ to remove this embed.
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Anton
Anton2y ago
ASP.NET Core adds some more configuration apparently
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Anton
Anton2y ago
It does make sense that it's ignored tho
MODiX
MODiX2y ago
tebeco#0205
REPL Result: Success
var foo = """
{
"bar": "10:00:00"
}
""";
record Foo(TimeOnly bar);
System.Text.Json.JsonSerializer.Deserialize<Foo>(foo)
var foo = """
{
"bar": "10:00:00"
}
""";
record Foo(TimeOnly bar);
System.Text.Json.JsonSerializer.Deserialize<Foo>(foo)
Result: Foo
{
"bar": "10:00:00"
}
{
"bar": "10:00:00"
}
Compile: 585.214ms | Execution: 79.647ms | React with ❌ to remove this embed.
MODiX
MODiX2y ago
Tvde1#0587
REPL Result: Success
var foo = """
{
"bar": "10:00:00"
}
""";
record Foo(TimeSpan bar);
System.Text.Json.JsonSerializer.Deserialize<Foo>(foo)
var foo = """
{
"bar": "10:00:00"
}
""";
record Foo(TimeSpan bar);
System.Text.Json.JsonSerializer.Deserialize<Foo>(foo)
Result: Foo
{
"bar": "10:00:00"
}
{
"bar": "10:00:00"
}
Compile: 587.900ms | Execution: 76.974ms | React with ❌ to remove this embed.
Tvde1
Tvde12y ago
what are you guys doing pfffft
MODiX
MODiX2y ago
tebeco#0205
REPL Result: Success
record Foo(TimeOnly Bar);
var foo = new Foo(TimeOnly.Parse("10:11:12"));
System.Text.Json.JsonSerializer.Serialize<Foo>(foo)
record Foo(TimeOnly Bar);
var foo = new Foo(TimeOnly.Parse("10:11:12"));
System.Text.Json.JsonSerializer.Serialize<Foo>(foo)
Result: string
{"Bar":"10:11:12"}
{"Bar":"10:11:12"}
Compile: 647.548ms | Execution: 90.767ms | React with ❌ to remove this embed.
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
MODiX
MODiX2y ago
AntonC#3545
REPL Result: Success
var foo = """
{
"bar": "10:00:00"
}
""";
record Foo(TimeSpan bar);
System.Text.Json.JsonSerializer.Deserialize<Foo>(foo).bar.Hours
var foo = """
{
"bar": "10:00:00"
}
""";
record Foo(TimeSpan bar);
System.Text.Json.JsonSerializer.Deserialize<Foo>(foo).bar.Hours
Result: int
10
10
Compile: 552.320ms | Execution: 76.993ms | React with ❌ to remove this embed.
Anton
Anton2y ago
@uselessxp
uselessxp
uselessxp2y ago
was eating, but I solved just in this moment, the problem was related to the sent body
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
uselessxp
uselessxp2y ago
swagger gave me automatically "ticks: 0" Then I replaced tons of value to the 0, but the problem i that I had to replace ALL removing "ticks:" also and the { } brackets
Anton
Anton2y ago
swagger*
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Anton
Anton2y ago
if you're after optimal performance, don't use json
uselessxp
uselessxp2y ago
"Testhour": { "ticks": 0 }, this was the generated body
Anton
Anton2y ago
that's TimeOnly
uselessxp
uselessxp2y ago
TimeSpan then I solved using "TestHour": 10:00:00,
Anton
Anton2y ago
quotes! around the time
uselessxp
uselessxp2y ago
yeah with quotes, I just written it manually now I'll make some additional tests, since I was asked to extend the model with lot of fields, then finally try to load data in a local DB table the requests now it's ok, so I'll have to try this at the end I lost like 2 hours searching solutions and I only had to remove "ticks" and brackets <:picard_facepalm:616692703685509130>
uselessxp
uselessxp2y ago
ops
uselessxp
uselessxp2y ago
if I'm not wrong, I think that the DB in which I should put the data "don't lik" strings for some columns hm I think he don't like how do I pass it the objects
Want results from more Discord servers?
Add your server
More Posts