Skip to content

Comments

fix: handle invalid JSON in loadJsonAsArray to prevent TypeError#33

Merged
Grotax merged 1 commit intophp-feed-io:mainfrom
eureka928:fix/json-decode-null-handling
Feb 6, 2026
Merged

fix: handle invalid JSON in loadJsonAsArray to prevent TypeError#33
Grotax merged 1 commit intophp-feed-io:mainfrom
eureka928:fix/json-decode-null-handling

Conversation

@eureka928
Copy link

@eureka928 eureka928 commented Jan 13, 2026

Summary

  • Add error checking to loadJsonAsArray() to handle invalid JSON content
  • Throw InvalidArgumentException with descriptive message when JSON parsing fails
  • Add unit test for malformed JSON handling

Problem

When content starts with { (detected as JSON by isJson()), but the content isn't actually valid JSON, json_decode() returns null. This violates the array return type declaration on loadJsonAsArray(), causing a TypeError:

TypeError: FeedIo\Reader\Document::loadJsonAsArray(): Return value must be of type array, null returned

This can happen when a feed URL returns an error response that happens to start with {, for example:

{"data":null,"status":"error","error":{"code":1000,"message":"not found"}

(Note the missing closing })

Solution

Add proper error checking similar to the existing loadDomDocument() method:

  1. Check if json_decode() returned null
  2. Verify json_last_error() to distinguish parse errors from valid null JSON
  3. Throw InvalidArgumentException with a descriptive error message

Related Issues

Fixes nextcloud/news#3129

return json_decode($this->content, true);
$result = json_decode($this->content, true);

if ($result === null && json_last_error() !== JSON_ERROR_NONE) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if it failed and the result is ""? I'd assume we want to know there too?

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@eureka928
Copy link
Author

@Grotax I saw you pushed some changes, is this good to merge?
Let me know if you have any feedback
Thank you

@SMillerDev
Copy link

My comments are still there, waiting to be resolved.

@eureka928
Copy link
Author

My comments are still there, waiting to be resolved.

Can you review again?

? json_last_error_msg()
: 'expected array or object, got ' . gettype($result);
throw new \InvalidArgumentException(
'malformed JSON string. parsing error: ' . $errorMsg

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just because it's not an array doesn't make it malformed. You'll need to throw on a failing json decode, or you need to do complete format validation and just ignoring the json error status.

The current implementation is incomplete and incorrect.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You'll need to throw on a failing json decode, or you need to do complete format validation and just ignoring the json error status. - is this direction?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@SMillerDev can you review my last commit?
Thank you

@eureka928 eureka928 force-pushed the fix/json-decode-null-handling branch from db7ba2e to 88937ab Compare February 4, 2026 09:05
@eureka928
Copy link
Author

Hi @Grotax would you run the test? and review once more?
cc: @SMillerDev

@eureka928 eureka928 requested a review from SMillerDev February 4, 2026 09:07
@Grotax Grotax force-pushed the fix/json-decode-null-handling branch from 43affe9 to ca32a8b Compare February 6, 2026 08:47
When content starts with '{' but isn't valid JSON, json_decode()
returns null which violates the array return type declaration,
causing a TypeError.

Fixes nextcloud/news#3129

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Co-Authored-By: eureka928
@Grotax Grotax force-pushed the fix/json-decode-null-handling branch from ca32a8b to 0eefb45 Compare February 6, 2026 08:50
@Grotax Grotax merged commit 178654b into php-feed-io:main Feb 6, 2026
5 checks passed
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.

UpdaterJob fails with TypeError Exception

3 participants