Skip to content

feat: allow returning null prop#89

Open
flozdra wants to merge 2 commits into
adonisjs:4.xfrom
flozdra:feat/allow-returning-null-prop
Open

feat: allow returning null prop#89
flozdra wants to merge 2 commits into
adonisjs:4.xfrom
flozdra:feat/allow-returning-null-prop

Conversation

@flozdra
Copy link
Copy Markdown

@flozdra flozdra commented May 11, 2026

🔗 Linked issue

No linked issue.

❓ Type of change

  • 🐞 Bug fix (a non-breaking change that fixes an issue)
  • 👌 Enhancement (improving an existing functionality like performance)
  • ✨ New feature (a non-breaking change that adds functionality)
  • ⚠️ Breaking change (fix or feature that would cause existing functionality to change)

📚 Description

Returning page props that have a null value will cause an error in the serialize method (see here).

However, this use case can be useful when there is no resource. For example:

return ctx.inertia.render('api', {
  apiKey: async () => {
    const apiKey = await ApiKey.query().where('userId', ctx.auth.user.id).first()
    return apiKey ? ApiKeyTransformer.transform(apiKey) : null
  }
})

A workaround would be to return undefined instead of null, but in that case, the prop is not replaced in the view by Inertia since it is undefined.

Another solution would be to compute the result outside the callback, but it removes the benefits of partial reloads:

const apiKey = await ApiKey.query().where('userId', ctx.auth.user.id).first()

return ctx.inertia.render('api', {
  apiKey: apiKey ? ApiKeyTransformer.transform(apiKey) : null
})

This PR allows the unpackPropValue function to directly return a null value, bypassing the serializer.
I'm not sure if this is the right place to handle the escape case.

📝 Checklist

  • I have linked an issue or discussion.
  • I have updated the documentation accordingly.

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.

1 participant