Skip to content
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: handle nil CompositeIDProperty value during database insert #628

Merged
merged 1 commit into from
Feb 7, 2025

Conversation

bwdmr
Copy link
Contributor

@bwdmr bwdmr commented Feb 6, 2025

These changes are now available in 1.50.2

The CompositeIDProperty was force-unwrapping its value during database input, which would crash if the value was nil. This could happen during initial inserts where the composite ID is meant to be set by a database trigger. Now checks if value exists before attempting to input it.

The CompositeIDProperty was force-unwrapping its value during database input,
which would crash if the value was nil. This could happen during initial
inserts where the composite ID is meant to be set by a database trigger.
Now checks if value exists before attempting to input it.
@bwdmr bwdmr requested a review from gwynne as a code owner February 6, 2025 17:17
@gwynne
Copy link
Member

gwynne commented Feb 6, 2025

How are you encountering this scenario? The composite ID value is default-initialized to avoid exactly this failure mode.

@bwdmr
Copy link
Contributor Author

bwdmr commented Feb 7, 2025

idkwym, i have a db function and a trigger on insert.
i need the composite id to be able to be nil on init so i can make use of the trigger.

@gwynne
Copy link
Member

gwynne commented Feb 7, 2025

The auto-initialized composite ID contains no values, so no values are (or can be) generated or sent by Fluent. In short, its presence should not prevent your trigger from filling them in itself. If it is, something else is wrong.

Copy link

codecov bot commented Feb 7, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 24.28%. Comparing base (260eff9) to head (8d90aa0).
Report is 3 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #628   +/-   ##
=======================================
  Coverage   24.28%   24.28%           
=======================================
  Files         149      149           
  Lines        9349     9350    +1     
=======================================
+ Hits         2270     2271    +1     
  Misses       7079     7079           
Files with missing lines Coverage Δ
Sources/FluentKit/Properties/CompositeID.swift 88.46% <100.00%> (+0.46%) ⬆️

Copy link
Member

@gwynne gwynne left a comment

Choose a reason for hiding this comment

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

Upon further examination, this is actually a valid fix, and avoids a crash in cases where the id of a model with composite ID has been explicitly set to nil (which was not previously meaningful, but with this change now becomes equivalent to setting it to .init()). Thanks for the fix and for getting me to take another look!

@gwynne gwynne added the semver-patch Internal changes only label Feb 7, 2025
@gwynne gwynne merged commit dd5a92d into vapor:main Feb 7, 2025
15 checks passed
bwdmr added a commit to bwdmr/fluent-kit that referenced this pull request Feb 7, 2025
…or#628)

The CompositeIDProperty was force-unwrapping its value during database input,
which would crash if the value was nil. This could happen during initial
inserts where the composite ID is meant to be set by a database trigger.
Now checks if value exists before attempting to input it.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
semver-patch Internal changes only
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants