-
Notifications
You must be signed in to change notification settings - Fork 10.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(gatsby-source-contentful): Prevent null pointer exception #35244
fix(gatsby-source-contentful): Prevent null pointer exception #35244
Conversation
Any chance you can update a test? |
@wardpeet , is the solution "good enough"? TBH, I'm not sure creating "broken assets" makes sense. I needed this to (at least) make my build pass. If you think this is good enough, I can try to add a test. |
Yes, I think it does make sense. It is a situation that can be caused within Contentful, when using Preview API: When you create a new entry, the auto save can be triggered. That will save entities with required fields that are empty. Entries in current version of the plugin are not affected by this. This is something I have to address in #31385, as the new schema generation will finally mirror the fields from Contentful instead of guessing them. |
As @xavivars gave me write access to his repo, I'll update this PR with a test in a moment :) |
My test repo looks like the following. It should mirror all cases of published/unpublished, valid/invalid, translated/untranslated and has no locale fallback enabled. I'll add tests that emulate a build process using data from this space. One for Delivery API and one for Preview API. This should cover a lot of use cases, that are not covered by our E2E tests. (They rely on rendering the Delivery API only) |
My tests show that the fix is not enough. I changed the code to properly nullify all fields when the file is missing. (No matter if not uploaded or not translated) |
…and when using no locale fallback
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can't we ignore creating the asset if the file is null? That sounds like a better path forward, no?
@wardpeet i think you are right. There is no use in importing "broken" assets. If we simply ignore empty assets, the implementation on user/project level should be simplified as well. I'll change it! |
I pushed the change, assets without any field will now be skipped. The good part: This is a performance improvement! Less nodes! The bad part: This could be seen as a breaking change, as data that was existing is suddenly gone. On the other hand, everyone affected by this potentially breaking change is also affected by the bug and can't build (As long their content has assets with missing files) |
The CI now suffers of |
mimeType: file.contentType ?? null, | ||
filename: file.fileName ?? null, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These two props can't be null
mimeType: file.contentType ?? null, | |
filename: file.fileName ?? null, | |
mimeType: file.contentType, | |
filename: file.fileName, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@wardpeet Unfortunately they can in some cases when using the Preview API and a (big) asset is still processed by Contentful
I talked to the API team about it. So I added another check for file.url
to exist and more comments to this section.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we can't create an asset when these things are null cause Image CDN needs them.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
alright, then lets require them as well! :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done!
Any documentation we need to update with this change? You mentioned this would be a "somewhat" breaking change. |
The change/commit documentation would be:
You could argue that this is a breaking change, as it might require changes in the code, especially when handling assets without locale fallbacks. On the other hand, everyone benefitting from this fix, won't be able to build their site right now. This is likely to prevent multiple projects from upgrading to Gatsby v4. |
@axe312ger can you sketch when it would be breaking change? (Can this happen outside of preview?) |
The change requires to change code that handles non-existent assets. Before this, assets could exist, and users might checked if For users using a space without locale fallback, the page is likely not to build at all as it will fail due to incomplete assets in translation. The more I think about it, the less it is breaking. It is either working now for you, so you probably don't have to change anything, if its not working for you, this is the patch you need. I'd say lets publish it to canary/next and see? |
Co-authored-by: axe312ger <[email protected]> Co-authored-by: Lennart <[email protected]> Co-authored-by: Ward Peeters <[email protected]> (cherry picked from commit 7bc7cf6)
#35492) Co-authored-by: axe312ger <[email protected]> Co-authored-by: Lennart <[email protected]> Co-authored-by: Ward Peeters <[email protected]> (cherry picked from commit 7bc7cf6) Co-authored-by: Xavi Ivars <[email protected]>
Description
This MR prevents a null pointer when creating asset nodes that are not configured on some languages.
Related Issues
Fixes #35216