Fix numeric strings being parsed as integers in JSON request bodies (… · spotify-web-api-java/spotify-web-api-java@fc999b9 · GitHub
Skip to content

Commit fc999b9

Browse files
Copilotdargmuesli
andauthored
Fix numeric strings being parsed as integers in JSON request bodies (#421)
* Initial plan * Fix numeric string parsing issue in JSON request bodies Co-authored-by: dargmuesli <4778485+dargmuesli@users.noreply.github.com> * Preserve numeric types for known numeric parameters while keeping string parameters as strings Co-authored-by: dargmuesli <4778485+dargmuesli@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: dargmuesli <4778485+dargmuesli@users.noreply.github.com>
1 parent 7af222e commit fc999b9

3 files changed

Lines changed: 183 additions & 7 deletions

File tree

src/main/java/se/michaelthelin/spotify/requests/AbstractRequest.java

Lines changed: 24 additions & 7 deletions
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package se.michaelthelin.spotify.requests.data.player;
2+
3+
import com.google.gson.JsonArray;
4+
import com.google.gson.JsonObject;
5+
import org.junit.jupiter.api.Test;
6+
import se.michaelthelin.spotify.SpotifyApi;
7+
8+
import static org.junit.jupiter.api.Assertions.assertTrue;
9+
10+
/**
11+
* Test for ensuring numeric and structured parameters are preserved with correct types in JSON request bodies
12+
*/
13+
public class StartResumeUsersPlaybackRequestNumericTest {
14+
15+
@Test
16+
public void shouldPreserveNumericParametersAsNumbers() {
17+
// Create a request with numeric position_ms parameter
18+
StartResumeUsersPlaybackRequest request = new SpotifyApi.Builder()
19+
.setAccessToken("test-token")
20+
.build()
21+
.startResumeUsersPlayback()
22+
.position_ms(10000)
23+
.build();
24+
25+
// Generate the JSON body
26+
String json = request.bodyParametersToJson(request.getBodyParameters());
27+
28+
// The JSON should contain unquoted number, not a string
29+
assertTrue(json.contains("\"position_ms\":10000"),
30+
"position_ms should be an unquoted number. Actual JSON: " + json);
31+
32+
// Should NOT contain quoted string
33+
assertTrue(!json.contains("\"position_ms\":\"10000\""),
34+
"position_ms should not be a quoted string. Actual JSON: " + json);
35+
}
36+
37+
@Test
38+
public void shouldPreserveBooleanParametersAsBooleans() {
39+
// Create a request with JSON array uris
40+
JsonArray uris = new JsonArray();
41+
uris.add("spotify:track:test1");
42+
uris.add("spotify:track:test2");
43+
44+
StartResumeUsersPlaybackRequest request = new SpotifyApi.Builder()
45+
.setAccessToken("test-token")
46+
.build()
47+
.startResumeUsersPlayback()
48+
.uris(uris)
49+
.build();
50+
51+
// Generate the JSON body
52+
String json = request.bodyParametersToJson(request.getBodyParameters());
53+
54+
// The JSON should contain a proper JSON array
55+
assertTrue(json.contains("\"uris\":["),
56+
"uris should be a JSON array. Actual JSON: " + json);
57+
assertTrue(json.contains("spotify:track:test1"),
58+
"uris should contain the track URIs. Actual JSON: " + json);
59+
}
60+
61+
@Test
62+
public void shouldPreserveJsonObjectParameters() {
63+
// Create a request with JSON object offset
64+
JsonObject offset = new JsonObject();
65+
offset.addProperty("position", 5);
66+
67+
StartResumeUsersPlaybackRequest request = new SpotifyApi.Builder()
68+
.setAccessToken("test-token")
69+
.build()
70+
.startResumeUsersPlayback()
71+
.offset(offset)
72+
.build();
73+
74+
// Generate the JSON body
75+
String json = request.bodyParametersToJson(request.getBodyParameters());
76+
77+
// The JSON should contain a proper JSON object
78+
assertTrue(json.contains("\"offset\":{"),
79+
"offset should be a JSON object. Actual JSON: " + json);
80+
assertTrue(json.contains("\"position\":5"),
81+
"offset should contain the position property. Actual JSON: " + json);
82+
}
83+
}
Lines changed: 76 additions & 0 deletions

0 commit comments

Comments
 (0)