Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Jan 19, 2026

Fix ConfigurationValidator to properly check if CommerceConfiguration exists

Summary

Fixed the bug where the migration tool incorrectly throws a validation error about CommerceConfiguration when the section doesn't exist in appsettings.json. This resolves issue #578.

Changes:

  • Understand the issue: GetSection() returns non-null even when section doesn't exist
  • Review current code at line 164 in ConfigurationValidator.cs
  • Review existing pattern at line 55 using .Exists() method
  • Make the minimal code change: Update line 164 to use .Exists() check
  • Build the solution to ensure no compilation errors
  • Look for existing tests for ConfigurationValidator
  • Create tests to validate the fix (3 new tests added)
  • Run tests to ensure the fix works (all 20 tests pass)
  • Request code review (no issues found)
  • Run security scan with CodeQL (timed out, but changes are minimal and safe)
  • Fix code formatting issues (removed trailing spaces from test assertions)

Files Modified:

  1. Migration.Tool.CLI/ConfigurationValidator.cs (line 164): Changed if (commerceConfiguration is not null) to if (commerceConfiguration?.Exists() == true) - this is the core fix
  2. Migration.Tool.Tests/ConfigurationValidatorTests.cs (new file): Added 3 comprehensive tests, fixed formatting
  3. Migration.Tool.Tests/Migration.Tool.Tests.csproj: Added CLI project reference

Test Results:

✅ All tests pass (3 ConfigurationValidatorTests + 17 existing tests = 20 total)
✅ Build succeeds with no errors
✅ Code formatting passes dotnet format --verify-no-changes
✅ Code review completed with no issues

Original prompt

Problem

When running the migration tool with an appsettings.json file that does not contain a CommerceConfiguration section, the validation incorrectly throws an error:

Configuration error: 'CommerceConfiguration:CommerceSiteNames' must contain at least one site name when 'CommerceConfiguration' is specified.

This is reported in issue #578: #578

Root Cause

In Migration.Tool.CLI/ConfigurationValidator.cs at line 164, the code checks:

if (commerceConfiguration is not null)

However, IConfigurationSection.GetSection() always returns a non-null section object, even when the section doesn't exist in the configuration. This causes the validation logic inside the block to execute even when no CommerceConfiguration is present.

Solution

Change line 164 in Migration.Tool.CLI/ConfigurationValidator.cs from:

if (commerceConfiguration is not null)

to:

if (commerceConfiguration?.Exists() == true)

This properly checks whether the configuration section actually exists (i.e., has any values defined) before validating its contents.

File to Modify

  • File: Migration.Tool.CLI/ConfigurationValidator.cs
  • Line: 164
  • Current code: if (commerceConfiguration is not null)
  • New code: if (commerceConfiguration?.Exists() == true)

This follows the same pattern already used elsewhere in the file, such as line 55 where targetKxpApiSettings?.Exists() == true is checked.

This pull request was created from Copilot chat.


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

… exists and add tests

Co-authored-by: petrinecp <5637849+petrinecp@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix validation error for missing CommerceConfiguration section Fix ConfigurationValidator incorrectly validating non-existent CommerceConfiguration Jan 19, 2026
Copilot AI requested a review from petrinecp January 19, 2026 12:00
@petrinecp petrinecp marked this pull request as ready for review January 19, 2026 12:04
@petrinecp petrinecp requested a review from a team as a code owner January 19, 2026 12:04
Copilot AI review requested due to automatic review settings January 19, 2026 12:05
Copy link
Contributor

Copilot AI left a 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 PR fixes a bug where the ConfigurationValidator incorrectly validated non-existent CommerceConfiguration sections. The issue occurred because IConfigurationSection.GetSection() returns a non-null object even when a section doesn't exist in the configuration file.

Changes:

  • Fixed the null check for CommerceConfiguration to use .Exists() method instead of null comparison
  • Added comprehensive test coverage for the bug fix
  • Added CLI project reference to enable testing of ConfigurationValidator

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
Migration.Tool.CLI/ConfigurationValidator.cs Changed line 164 from is not null to ?.Exists() == true to properly detect section existence
Migration.Tool.Tests/ConfigurationValidatorTests.cs Added three test cases covering missing section, empty values, and valid configuration scenarios
Migration.Tool.Tests/Migration.Tool.Tests.csproj Added CLI project reference to enable testing of ConfigurationValidator class

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@petrinecp
Copy link
Contributor

@copilot build action is failing on dotnet format --exclude ./examples/** --verify-no-changes

Co-authored-by: petrinecp <5637849+petrinecp@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Jan 19, 2026

@copilot build action is failing on dotnet format --exclude ./examples/** --verify-no-changes

Fixed in commit 043af3f. The issue was trailing whitespace in multi-line Assert statements. Ran dotnet format to fix formatting, and dotnet format --verify-no-changes now passes.

@petrinecp petrinecp merged commit eaaa6e2 into develop Jan 19, 2026
2 checks passed
@petrinecp petrinecp deleted the copilot/fix-commerce-configuration-validation branch January 19, 2026 14:00
petrinecp added a commit that referenced this pull request Jan 22, 2026
#581)

* Fix ConfigurationValidator incorrectly validating non-existent CommerceConfiguration (#579)

* Initial plan

* Fix ConfigurationValidator to properly check if CommerceConfiguration exists and add tests

Co-authored-by: petrinecp <5637849+petrinecp@users.noreply.github.com>

* Fix code formatting to pass dotnet format verification

Co-authored-by: petrinecp <5637849+petrinecp@users.noreply.github.com>

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: petrinecp <5637849+petrinecp@users.noreply.github.com>

* Adjust version matrix

---------

Co-authored-by: Copilot <198982749+Copilot@users.noreply.github.com>
Co-authored-by: petrinecp <5637849+petrinecp@users.noreply.github.com>
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.

3 participants