Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TimeSpan Mismatch between Schema(string) vs Serialization(object graph) #2668

Closed
abbotware opened this issue Feb 3, 2020 · 6 comments
Closed

Comments

@abbotware
Copy link

abbotware commented Feb 3, 2020

Can't seem to deserialize a class with TimeSpan from the swagger2csclient generated clients.

The error i get on the client is

Newtonsoft.Json.JsonSerializationException: Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.TimeSpan' because the type requires a JSON primitive value (e.g. string, number, boolean, null) to deserialize correctly.
To fix this error either change the JSON to a JSON primitive value (e.g. string, number, boolean, null) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List<T>) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.
Path '[0].offlineTimeSpan.ticks', line 1, position 225.

Not sure if this is related, but I noticed the data returned during serialization doesn't seem to match the schema (see image below)

image

I thought TimeSpan was supported.. but I guess I have to add it?

how would one go about adding TimeSpan support to the RestAPI and Nswag the CSharp generated clients?

On the server / Rest side: Do i have to use this:
https://github.com/RicoSuter/NJsonSchema/wiki/Type-Mappers
Or do I use Netwonsoft settings?

       services.AddSwaggerDocument(x =>
            {
                x.FlattenInheritanceHierarchy = true;
                x.SchemaGenerator.Settings.TypeMappers.Add(???));
                x.ActualSerializerSettings.Converters.Add(???);
            });

On the client,
I see there are options to expose the JsonSerializerSettings but the CreateSerializerSettings is not virtual.

@RicoSuter
Copy link
Owner

Somehow timespan is not converted to a string, eg “00:05:00”. So you probably just need a global json converter which does that as expected, no?

@abbotware
Copy link
Author

Somehow timespan is not converted to a string, eg “00:05:00”

Are you saying in my instance its broken? (just trying to determine if what I am seeing is expected behavior or not)

So you probably just need a global json converter which does that as expected, no?

where would I go about doing this on the server?
And I also need to register this on the swagger2csclient generated clients?

@abbotware
Copy link
Author

OK - I know why I am confused - I am using NetCoreApp3.1 (upgraded)... and it seems that the json serialization is no longer based on Newtonsoft...

There is no converter for TimeSpan!

https://github.com/dotnet/runtime/tree/81bf79fd9aa75305e55abe2f7e9ef3f60624a3a1/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters

@RicoSuter
Copy link
Owner

Ah, yeah on 3.1 with System.Json.Text there are mismatches expected because the default newtonsoft serializer (nswag) might not match the new serializer behavior.

@RicoSuter
Copy link
Owner

See: #2243

@abbotware
Copy link
Author

@RicoSuter - makes sense - closing this - thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants