JohnnyCrazy / SpotifyAPI-NET

:sound: A Client for the Spotify Web API, written in C#/.NET
http://johnnycrazy.github.io/SpotifyAPI-NET/
MIT License
1.5k stars 307 forks source link

Spotify API Inconsistency: Integer Values Returned as Floats Without Documentation Update #952

Closed Inzaniity closed 7 months ago

Inzaniity commented 7 months ago

We've observed a recent and undocumented change in Spotify's API behavior, specifically regarding the data type of certain values that were traditionally returned as integers. This alteration is gradually being implemented across various endpoints, with a notable example being the tracksendpoint. Previously, values such as "duration_ms" would be returned as integers (e.g., "duration_ms": 2345). However, we are now receiving these values as floats (e.g., "duration_ms": 2345.0), despite the official web API reference still indicating that integers are to be expected.

This discrepancy has been confirmed to affect multiple endpoints and is causing issues for applications with strict type requirements, where floats are not expected in places where integers were previously provided. As a result, typed applications that rely on these integer values are experiencing breaks in functionality.

A discussion thread exists on the Spotify Developer Forums highlighting this issue, but responses and clarifications from Spotify have been minimal: Spotify Developer Forums Thread.

As a temporary workaround, I have modified all applicable properties in my application from type int to float and performed a cast back to int where necessary (I'm still on a very old version of SpotifyAPI-NET but I guess this affects the latest version too). Checking the latest code it seems like the models for the API response are still typed and thus this issue will also arise in the current release. Models. While this approach has resolved the issue for my application and benefitted my users, it is not an ideal or sustainable solution.

The staggered rollout of these API changes has further complicated diagnosis and troubleshooting, especially since the effects were not immediately evident across all user bases.

I am raising this issue here so others know what's up, if they encounter Newtonsoft.Json.JsonReaderException.

Discussed in https://github.com/JohnnyCrazy/SpotifyAPI-NET/discussions/925

Originally posted by **rogamaral** January 5, 2024 Since yesterday and suddenly, **without any changes to the code**, I started to have the following exception: Newtonsoft.Json.JsonReaderException: 'Input string '359120.0' is not a valid integer. Path 'followers.total', line 1, position 127.' `var artist = await _spotify.Artists.Get(artistId);` Does anyone else also have this problem? Thanks.
mx2rel commented 7 months ago

This issue seems to be happening to multiple developers, including myself, and has been discussed previously in #925, #926 and #928. For me, it randomly stopped happening after few days.

Inzaniity commented 7 months ago

This issue seems to be happening to multiple developers, including myself, and has been discussed previously in #925, #926 and #928. For me, it randomly stopped happening after few days.

I'm aware that this was discussed. I raised the issue out of #925 to further spread awareness for it.

The Newtonsoft.JSON Deserializer should convert int to double if int is returned from the API and double is expected. Thats why the approach of changing the types might work long term.

Inzaniity commented 7 months ago

Well I should read all the discussions beforehand. It seems the Serializer has been modified to convert.

My bad!

lwhuybrechts commented 7 months ago

Hi @Inzaniity, I'm also experiencing the JsonReaderException on some properties, for instance album.total_tracks. Updating to the latest SpotifyAPI.Web (7.1.1) did not fix them all.

If I add the DoubleToIntConverter attribute to the properties that still cause the exceptions it's ok again.

So it seems to me we should add the DoubleToIntConverter attribute to all int properties so it doesn't break when Spotify decides to return doubles/floats somewhere. What do you think?

Inzaniity commented 7 months ago

Hi @Inzaniity, I'm also experiencing the JsonReaderException on some properties, for instance album.total_tracks. Updating to the latest SpotifyAPI.Web (7.1.1) did not fix them all.

If I add the DoubleToIntConverter attribute to the properties that still cause the exceptions it's ok again.

So it seems to me we should add the DoubleToIntConverter attribute to all int properties so it doesn't break when Spotify decides to return doubles/floats somewhere. What do you think?

I mean that is what I did but on an older code base. So yeah, converting all expected ints seems to be the solution here.

Inzaniity commented 7 months ago

A Spotify developer confirmed a fix for this in the discussion on the Spotify developer forum

An optimization in the deserialization of JSON in upstream was causing the serializer to default to double types for integer values. The optimization was not applied to all queries, and several tools were sanitizing the response which was the reason we did not uncover this from the start.