-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Preserve existing Examine FieldDefinitionCollection if it already exists #20931
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
base: main
Are you sure you want to change the base?
Preserve existing Examine FieldDefinitionCollection if it already exists #20931
Conversation
|
Hi there @callumbwhyte, thank you for this contribution! 👍 While we wait for one of the Core Collaborators team to have a look at your work, we wanted to let you know about that we have a checklist for some of the things we will consider during review:
Don't worry if you got something wrong. We like to think of a pull request as the start of a conversation, we're happy to provide guidance on improving your contribution. If you realize that you might want to make some changes then you can do that by adding new commits to the branch you created for this work and pushing new commits. They should then automatically show up as updates to this pull request. Thanks, from your friendly Umbraco GitHub bot 🤖 🙂 |
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.
Pull request overview
This pull request adds functionality to preserve existing field definitions when configuring Umbraco Examine indexes. Previously, the ConfigureIndexOptions class would always create a new UmbracoFieldDefinitionCollection from scratch, which would discard any custom field definitions that may have been registered by extension code before the configuration runs.
Key Changes:
- Added a new constructor to
UmbracoFieldDefinitionCollectionthat accepts an existingFieldDefinitionCollectionand merges its definitions with the base Umbraco field definitions - Updated
ConfigureIndexOptionsto use the new constructor, passing the existingoptions.FieldDefinitionsto preserve any pre-existing customizations
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| src/Umbraco.Infrastructure/Examine/UmbracoFieldDefinitionCollection.cs | Adds a new constructor that accepts and merges existing field definitions |
| src/Umbraco.Examine.Lucene/DependencyInjection/ConfigureIndexOptions.cs | Updates all three index configurations (Internal, External, Members) to preserve existing field definitions using the new constructor |
| foreach (var definition in definitions) | ||
| { | ||
| AddOrUpdate(definition); |
Copilot
AI
Nov 22, 2025
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.
The new constructor does not handle the case where definitions parameter is null. If options.FieldDefinitions is null when this constructor is called (from ConfigureIndexOptions.cs), it will throw a NullReferenceException when trying to iterate. Consider adding a null check:
public UmbracoFieldDefinitionCollection(FieldDefinitionCollection? definitions)
: base(UmbracoIndexFieldDefinitions)
{
if (definitions != null)
{
foreach (var definition in definitions)
{
AddOrUpdate(definition);
}
}
}| foreach (var definition in definitions) | |
| { | |
| AddOrUpdate(definition); | |
| if (definitions != null) | |
| { | |
| foreach (var definition in definitions) | |
| { | |
| AddOrUpdate(definition); | |
| } |
As discussed on #20267 v16 introduced a behavioural breaking change whereby Umbraco's own Examine index configuration now happens at an undetermined point in the startup lifecycle, compared with previously running as part of the core Umbraco boot steps.
This can cause issues when trying to customise the Examine index, e.g. setting field types, as you don't know whether your code is going to run before or after Umbraco's.
I explained a bit more about how this came to be here:
The solution is of course to add decorate your
IComposerwith[ComposeAfter(typeof(AddExamineComposer))]as per the docs - but this has been problematic for me in my Search Extensions package where I am multi-targeting 🙈 and can't reliably target this change to V16+ as V15 also uses .NET 9 whereAddExamineComposerdoes not exist...After digging into things I found the real issue is Umbraco assumes it's okay to set the
FieldDefinitionsproperty during configuration, without consideration for if this has already been set. The fix is simple: when Umbraco's configuration code runs, ensure any existing config is applied too. I have added an overload toUmbracoFieldDefinitionCollectionthat takes an existingFieldDefinitionCollectionand tries to add each existing definition to theUmbracoFieldDefinitionCollection. Examine sets this property to an empty collection on initialisation (here) anyway so it should always be set.I believe it would be good to get this fix into both V16 and V17 to prevent confusion as others have been experiencing!