Skip to content

Conversation

@crazytonyli
Copy link
Contributor

This PR should fix the crash in #24320

WordPress/WPAccount+RestApi.swift:15: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value

The crash happens at authToken.isEmpty. authToken can be nil, but the Objective-C API didn't have nullabilty annotation which makes it an IUO property.

To test:

Regression Notes

  1. Potential unintended areas of impact

  2. What I did to test those areas of impact (or what existing automated tests I relied on)

  3. What automated tests I added (or what prevented me from doing so)

PR submission checklist:

  • I have completed the Regression Notes.
  • I have considered adding unit tests for my changes.
  • I have considered adding accessibility improvements for my changes.
  • I have considered if this change warrants user-facing release notes and have added them to RELEASE-NOTES.txt if necessary.

Testing checklist:

  • WordPress.com sites and self-hosted Jetpack sites.
  • Portrait and landscape orientations.
  • Light and dark modes.
  • Fonts: Larger, smaller and bold text.
  • High contrast.
  • VoiceOver.
  • Languages with large words or with letters/accents not frequently used in English.
  • Right-to-left languages. (Even if translation isn’t complete, formatting should still respect the right-to-left layout)
  • iPhone and iPad.
  • Multi-tasking: Split view and Slide over. (iPad)

@crazytonyli crazytonyli added this to the 25.9 milestone Mar 26, 2025
@dangermattic
Copy link
Collaborator

2 Warnings
⚠️ View files have been modified, but no screenshot or video is included in the pull request. Consider adding some for clarity.
⚠️ This PR is assigned to the milestone 25.9. The due date for this milestone has already passed.
Please assign it to a milestone with a later deadline or check whether the release for this milestone has already been finished.

Generated by 🚫 Danger


@property (nonatomic, strong, nullable) NSNumber *userID;
@property (nonatomic, strong, nullable) NSString *avatarURL;
@property (nonatomic, copy, nonnull) NSString *username;
Copy link
Contributor

Choose a reason for hiding this comment

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

(nit) nonnull is redundant.

Copy link
Contributor

Choose a reason for hiding this comment

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

Are we sure username is nonnull?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, according to the Core Data schema.

Screenshot 2025-04-01 at 1 59 16 PM

get {
guard let api = objc_getAssociatedObject(self, &apiAssociatedKey) as? WordPressComRestApi else {
guard authToken.isEmpty else {
if let authToken, !authToken.isEmpty {
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm also changing these lines here #24327 👀

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I just saw your PR. I can resolve the conflicts once yours is merged. 😄

Copy link
Contributor

Choose a reason for hiding this comment

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

My PR is missing the nullability check, so this change will be needed.

@objc
var hasBlogs: Bool {
return !blogs.isEmpty
return blogs?.isEmpty == false
Copy link
Contributor

Choose a reason for hiding this comment

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

(nit) Is there any way to make blogs in WPAccount nonnull for convenience?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

At runtime, it probably won't be null. Core Data should return an empty list if there is no Blog instance associated with this account. However, considering an "Optional" property in the relationship configuration, I feel it is safer to declare it nullable.

@wpmobilebot
Copy link
Contributor

WordPress Alpha📲 You can test the changes from this Pull Request in WordPress Alpha by scanning the QR code below to install the corresponding build.
App NameWordPress Alpha WordPress Alpha
ConfigurationRelease-Alpha
Build Numberpr24328-d9f9d89
Version25.8
Bundle IDorg.wordpress.alpha
Commitd9f9d89
App Center BuildWPiOS - One-Offs #11860
Automatticians: You can use our internal self-serve MC tool to give yourself access to App Center if needed.

@wpmobilebot
Copy link
Contributor

Jetpack Alpha📲 You can test the changes from this Pull Request in Jetpack Alpha by scanning the QR code below to install the corresponding build.
App NameJetpack Alpha Jetpack Alpha
ConfigurationRelease-Alpha
Build Numberpr24328-d9f9d89
Version25.8
Bundle IDcom.jetpack.alpha
Commitd9f9d89
App Center Buildjetpack-installable-builds #10883
Automatticians: You can use our internal self-serve MC tool to give yourself access to App Center if needed.

@crazytonyli crazytonyli requested a review from kean March 30, 2025 22:49
@wpmobilebot
Copy link
Contributor

wpmobilebot commented Mar 30, 2025

App Icon📲 You can test the changes from this Pull Request in Jetpack by scanning the QR code below to install the corresponding build.
App NameJetpack
ConfigurationRelease-Alpha
Build Number27020
VersionPR #24328
Bundle IDcom.jetpack.alpha
Commitddc2105
Installation URL3nr1nu8s78mbg
Automatticians: You can use our internal self-serve MC tool to give yourself access to those builds if needed.

@wpmobilebot
Copy link
Contributor

wpmobilebot commented Mar 30, 2025

App Icon📲 You can test the changes from this Pull Request in WordPress by scanning the QR code below to install the corresponding build.
App NameWordPress
ConfigurationRelease-Alpha
Build Number27020
VersionPR #24328
Bundle IDorg.wordpress.alpha
Commitddc2105
Installation URL4s4g51ihlfoo8
Automatticians: You can use our internal self-serve MC tool to give yourself access to those builds if needed.

} else if let account = try WPAccount.lookupDefaultWordPressComAccount(in: context) {
siteID = account.primaryBlogID
} else if let account = try WPAccount.lookupDefaultWordPressComAccount(in: context), let primaryBlogID = account.primaryBlogID {
return nil
Copy link
Contributor

Choose a reason for hiding this comment

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

Looks like a function change. Previous:

siteID = account.primaryBlogID
```

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good catch! Fixed in a185c04


@property (nonatomic, strong, nullable) NSNumber *userID;
@property (nonatomic, strong, nullable) NSString *avatarURL;
@property (nonatomic, copy, nonnull) NSString *username;
Copy link
Contributor

Choose a reason for hiding this comment

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

Are we sure username is nonnull?

if let account = blog.account {
self.init(baseUrl: url, type: .dotCom(authToken: account.authToken))
if let account = blog.account, let authToken = account.authToken {
self.init(baseUrl: url, type: .dotCom(authToken: authToken))
Copy link
Contributor

Choose a reason for hiding this comment

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

As long as a blog has an associated WP.com, it should be using a .dotCom client.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

All WPAccount instances should have an authToken because they are created after signing into WP.com. We can change the WordPressClient to make it support anonymous API requests, but I don't think that's necessary because it won't be used to make anonymous API calls?

Copy link
Contributor

Choose a reason for hiding this comment

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

The issue is that if account.authToken is nil, it will now fall into the else with .selfHosted, which is the logic change. Should it throw an error instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah of course. I have addressed the issue in 8af7561.

@kean
Copy link
Contributor

kean commented Apr 1, 2025

I'm going to merge it if you don't mind – need some of this code in another branch.

@kean kean enabled auto-merge April 1, 2025 19:51
@crazytonyli
Copy link
Contributor Author

@kean 👍 Thanks!

@kean kean added this pull request to the merge queue Apr 1, 2025
Merged via the queue into trunk with commit c9b4af3 Apr 1, 2025
25 checks passed
@kean kean deleted the fix-wp-account-nullablity branch April 1, 2025 20:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants