Fix numeric strings being parsed as integers in JSON request bodies by Copilot · Pull Request #421 · spotify-web-api-java/spotify-web-api-java · GitHub
Skip to content

Fix numeric strings being parsed as integers in JSON request bodies#421

Merged
dargmuesli merged 3 commits into
masterfrom
copilot/fix-420
Jul 23, 2025
Merged

Fix numeric strings being parsed as integers in JSON request bodies#421
dargmuesli merged 3 commits into
masterfrom
copilot/fix-420

Conversation

Copilot AI commented Jul 23, 2025

Copy link
Copy Markdown
Contributor

Problem

String parameters containing numeric values were incorrectly converted to JSON numbers instead of remaining as strings when creating API requests. This caused playlist creation to fail when using numeric strings for names or descriptions.

Example issue:

api.createPlaylist(userId, "2025")
    .description("2025")
    .build()
    .execute()

Before (broken): {"name":2025,"description":2025}
After (fixed): {"name":"2025","description":"2025"}

Root Cause

In AbstractRequest.bodyParametersToJson(), the code attempted to parse all parameter values as JSON using JsonParser.parseString(). This converted:

  • "2025"2025 (number)
  • "123.45"123.45 (number)
  • "true"true (boolean)
  • "null"null (JSON null)

Solution

Modified bodyParametersToJson() to always treat parameter values as strings using jsonObject.addProperty(name, value) instead of attempting JSON parsing. This preserves the caller's intent when they explicitly pass string values.

Key changes:

  • Removed JsonParser.parseString() attempt for parameter values
  • Use addProperty() to ensure values remain as string primitives
  • Added comprehensive test coverage for edge cases

Testing

  • ✅ All existing tests pass (335 tests, 0 failures)
  • ✅ New test cases verify numeric strings stay as strings
  • ✅ Edge cases covered: decimals, booleans, null-like strings
  • ✅ Manual verification of exact issue scenario

This change ensures string parameters always serialize as JSON strings, preventing API validation failures while maintaining backward compatibility.

Fixes #420.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Co-authored-by: dargmuesli <4778485+dargmuesli@users.noreply.github.com>
Copilot AI changed the title [WIP] [9.2.0] Strings of integers are incorrectly parsed as integers Fix numeric strings being parsed as integers in JSON request bodies Jul 23, 2025
Copilot AI requested a review from dargmuesli July 23, 2025 07:14
@dargmuesli dargmuesli marked this pull request as ready for review July 23, 2025 07:16
@codecov

codecov Bot commented Jul 23, 2025

Copy link
Copy Markdown

@dargmuesli

Copy link
Copy Markdown
Member

@copilot ensure that other numeric body parameters like position_ms for StartResumeUsersPlaybackRequest are kept numeric

…ing parameters as strings

Co-authored-by: dargmuesli <4778485+dargmuesli@users.noreply.github.com>

Copilot AI commented Jul 23, 2025

Copy link
Copy Markdown
Contributor Author

@dargmuesli dargmuesli merged commit fc999b9 into master Jul 23, 2025
10 of 11 checks passed
@dargmuesli dargmuesli deleted the copilot/fix-420 branch July 23, 2025 07:53
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

Successfully merging this pull request may close these issues.

[9.2.0] Strings of integers are incorrectly parsed as integers

2 participants