Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Oct 22, 2025

  • Understand the issue: Parameters with [DefaultValue(null)] are treated as required instead of optional
  • Identify the root cause: ParameterInfo.IsOptional and ParameterInfo.HasDefaultValue are false for [DefaultValue] attributes
  • Locate code changes needed
  • Implement helper methods to check for DefaultValueAttribute
  • Update schema generation to treat DefaultValueAttribute as optional
  • Update parameter marshaling to use DefaultValueAttribute values
  • Add comprehensive tests including precedence test
  • Build and test the changes
  • Address PR feedback:
    • Revert .gitignore changes
    • Fix formatting issues in test file
    • Consolidate duplicate helper methods
    • Use longer parameter name in test
    • Unify into single TryGetEffectiveDefaultValue method

Summary

Successfully implemented support for the [DefaultValue] attribute in AIFunctionFactory. Parameters decorated with [DefaultValue] are now treated as optional in the JSON schema and function invocation, matching the behavior of C# default values.

Changes Made:

  1. AIJsonUtilities.Schema.Create.cs: Added internal TryGetEffectiveDefaultValue helper method to check for and retrieve default values from both C# defaults and DefaultValueAttribute in a single call
  2. AIFunctionFactory.cs: Updated parameter marshaling to use the unified helper method from AIJsonUtilities
  3. Tests: Added comprehensive tests covering null, non-null, and int default values, plus precedence when both are present

Security Considerations:

  • No security vulnerabilities introduced
  • Default values from attributes are treated the same as C# defaults
  • All validation remains in place
Original prompt

With AIFunctionFactory, when a parameter is optional (e.g. string? s = null), it gets listed in the schema as being non-required. However, when that default value is instead supplied with [DefaultValue(null)] (e.g. [DefaultValue(null)] string? s), the DefaultValue attribute isn't currently being factored in. Please fix that, such that DefaultValue is treated the same as if a default value were supplied using the C# syntax.


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

Microsoft Reviewers: Open in CodeFlow

Copilot AI and others added 3 commits October 22, 2025 13:28
- Check for DefaultValueAttribute when determining parameter optionality
- Use DefaultValueAttribute value as default when parameter not provided
- Add helper methods HasEffectiveDefaultValue and GetEffectiveDefaultValue
- Add comprehensive tests for DefaultValue attribute support

Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
Verify that when both DefaultValue attribute and C# default are present,
the DefaultValue attribute takes precedence.

Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
- Add test verifying DefaultValue attribute takes precedence over C# default
- Add .nuget directory to gitignore to exclude build artifacts

Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix handling of DefaultValue attribute in optional parameters Support DefaultValueAttribute in AIFunctionFactory parameter handling Oct 22, 2025
Copilot AI requested a review from stephentoub October 22, 2025 13:41
Copilot finished work on behalf of stephentoub October 22, 2025 13:41
@stephentoub stephentoub marked this pull request as ready for review October 22, 2025 13:52
@stephentoub stephentoub requested a review from a team as a code owner October 22, 2025 13:52
Copilot AI review requested due to automatic review settings October 22, 2025 13:52
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 adds support for the [DefaultValue] attribute in AIFunctionFactory, ensuring parameters decorated with this attribute are treated as optional in the same way as parameters with C# default value syntax.

Key Changes:

  • Added helper methods to detect and retrieve default values from both [DefaultValue] attribute and C# default syntax
  • Updated schema generation to correctly mark parameters with [DefaultValue] as optional
  • Updated parameter marshaling to use default values from [DefaultValue] attribute

Reviewed Changes

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

File Description
AIJsonUtilities.Schema.Create.cs Added HasEffectiveDefaultValue() and GetEffectiveDefaultValue() helpers to check for both C# defaults and [DefaultValue] attribute; updated schema generation logic to use these helpers
AIFunctionFactory.cs Added duplicate helper methods and updated parameter marshaling to respect [DefaultValue] attribute
AIFunctionFactoryTest.cs Added comprehensive test coverage for [DefaultValue] attribute functionality including null values, non-null values, and precedence scenarios

Copy link
Member

@stephentoub stephentoub left a comment

Choose a reason for hiding this comment

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

@copilot, please fix the formatting diagnostics:

Build FAILED.

/mnt/vss/_work/1/s/test/Libraries/Microsoft.Extensions.AI.Tests/Functions/AIFunctionFactoryTest.cs(69,1): error IDE0055: Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055) [/mnt/vss/_work/1/s/test/Libraries/Microsoft.Extensions.AI.Tests/Microsoft.Extensions.AI.Tests.csproj::TargetFramework=net9.0]
/mnt/vss/_work/1/s/test/Libraries/Microsoft.Extensions.AI.Tests/Functions/AIFunctionFactoryTest.cs(75,1): error IDE0055: Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055) [/mnt/vss/_work/1/s/test/Libraries/Microsoft.Extensions.AI.Tests/Microsoft.Extensions.AI.Tests.csproj::TargetFramework=net9.0]
/mnt/vss/_work/1/s/test/Libraries/Microsoft.Extensions.AI.Tests/Functions/AIFunctionFactoryTest.cs(78,1): error IDE0055: Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055) [/mnt/vss/_work/1/s/test/Libraries/Microsoft.Extensions.AI.Tests/Microsoft.Extensions.AI.Tests.csproj::TargetFramework=net9.0]
/mnt/vss/_work/1/s/test/Libraries/Microsoft.Extensions.AI.Tests/Functions/AIFunctionFactoryTest.cs(81,1): error IDE0055: Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055) [/mnt/vss/_work/1/s/test/Libraries/Microsoft.Extensions.AI.Tests/Microsoft.Extensions.AI.Tests.csproj::TargetFramework=net9.0]
/mnt/vss/_work/1/s/test/Libraries/Microsoft.Extensions.AI.Tests/Functions/AIFunctionFactoryTest.cs(87,1): error IDE0055: Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055) [/mnt/vss/_work/1/s/test/Libraries/Microsoft.Extensions.AI.Tests/Microsoft.Extensions.AI.Tests.csproj::TargetFramework=net9.0]
/mnt/vss/_work/1/s/test/Libraries/Microsoft.Extensions.AI.Tests/Functions/AIFunctionFactoryTest.cs(90,1): error IDE0055: Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055) [/mnt/vss/_work/1/s/test/Libraries/Microsoft.Extensions.AI.Tests/Microsoft.Extensions.AI.Tests.csproj::TargetFramework=net9.0]
/mnt/vss/_work/1/s/test/Libraries/Microsoft.Extensions.AI.Tests/Functions/AIFunctionFactoryTest.cs(96,1): error IDE0055: Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055) [/mnt/vss/_work/1/s/test/Libraries/Microsoft.Extensions.AI.Tests/Microsoft.Extensions.AI.Tests.csproj::TargetFramework=net9.0]
/mnt/vss/_work/1/s/test/Libraries/Microsoft.Extensions.AI.Tests/Functions/AIFunctionFactoryTest.cs(99,1): error IDE0055: Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055) [/mnt/vss/_work/1/s/test/Libraries/Microsoft.Extensions.AI.Tests/Microsoft.Extensions.AI.Tests.csproj::TargetFramework=net9.0]
/mnt/vss/_work/1/s/test/Libraries/Microsoft.Extensions.AI.Tests/Functions/AIFunctionFactoryTest.cs(105,1): error IDE0055: Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055) [/mnt/vss/_work/1/s/test/Libraries/Microsoft.Extensions.AI.Tests/Microsoft.Extensions.AI.Tests.csproj::TargetFramework=net9.0]
/mnt/vss/_work/1/s/test/Libraries/Microsoft.Extensions.AI.Tests/Functions/AIFunctionFactoryTest.cs(69,1): error IDE0055: Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055) [/mnt/vss/_work/1/s/test/Libraries/Microsoft.Extensions.AI.Tests/Microsoft.Extensions.AI.Tests.csproj::TargetFramework=net8.0]
/mnt/vss/_work/1/s/test/Libraries/Microsoft.Extensions.AI.Tests/Functions/AIFunctionFactoryTest.cs(75,1): error IDE0055: Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055) [/mnt/vss/_work/1/s/test/Libraries/Microsoft.Extensions.AI.Tests/Microsoft.Extensions.AI.Tests.csproj::TargetFramework=net8.0]
/mnt/vss/_work/1/s/test/Libraries/Microsoft.Extensions.AI.Tests/Functions/AIFunctionFactoryTest.cs(78,1): error IDE0055: Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055) [/mnt/vss/_work/1/s/test/Libraries/Microsoft.Extensions.AI.Tests/Microsoft.Extensions.AI.Tests.csproj::TargetFramework=net8.0]
/mnt/vss/_work/1/s/test/Libraries/Microsoft.Extensions.AI.Tests/Functions/AIFunctionFactoryTest.cs(81,1): error IDE0055: Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055) [/mnt/vss/_work/1/s/test/Libraries/Microsoft.Extensions.AI.Tests/Microsoft.Extensions.AI.Tests.csproj::TargetFramework=net8.0]
/mnt/vss/_work/1/s/test/Libraries/Microsoft.Extensions.AI.Tests/Functions/AIFunctionFactoryTest.cs(87,1): error IDE0055: Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055) [/mnt/vss/_work/1/s/test/Libraries/Microsoft.Extensions.AI.Tests/Microsoft.Extensions.AI.Tests.csproj::TargetFramework=net8.0]
/mnt/vss/_work/1/s/test/Libraries/Microsoft.Extensions.AI.Tests/Functions/AIFunctionFactoryTest.cs(90,1): error IDE0055: Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055) [/mnt/vss/_work/1/s/test/Libraries/Microsoft.Extensions.AI.Tests/Microsoft.Extensions.AI.Tests.csproj::TargetFramework=net8.0]
/mnt/vss/_work/1/s/test/Libraries/Microsoft.Extensions.AI.Tests/Functions/AIFunctionFactoryTest.cs(96,1): error IDE0055: Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055) [/mnt/vss/_work/1/s/test/Libraries/Microsoft.Extensions.AI.Tests/Microsoft.Extensions.AI.Tests.csproj::TargetFramework=net8.0]
/mnt/vss/_work/1/s/test/Libraries/Microsoft.Extensions.AI.Tests/Functions/AIFunctionFactoryTest.cs(99,1): error IDE0055: Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055) [/mnt/vss/_work/1/s/test/Libraries/Microsoft.Extensions.AI.Tests/Microsoft.Extensions.AI.Tests.csproj::TargetFramework=net8.0]
/mnt/vss/_work/1/s/test/Libraries/Microsoft.Extensions.AI.Tests/Functions/AIFunctionFactoryTest.cs(105,1): error IDE0055: Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055) [/mnt/vss/_work/1/s/test/Libraries/Microsoft.Extensions.AI.Tests/Microsoft.Extensions.AI.Tests.csproj::TargetFramework=net8.0]
    0 Warning(s)
    18 Error(s)

- Revert changes to .gitignore
- Fix formatting issues in test file (remove extra blank lines)
- Make HasEffectiveDefaultValue and GetEffectiveDefaultValue internal in AIJsonUtilities
- Remove duplicate helper methods from AIFunctionFactory and use the ones from AIJsonUtilities

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

Copilot AI commented Oct 22, 2025

@copilot, please fix the formatting diagnostics:

Build FAILED.

...

Fixed in c79b280. Removed extra blank lines that were causing IDE0055 formatting errors.

Copilot finished work on behalf of stephentoub October 22, 2025 14:03
Copilot AI requested a review from stephentoub October 22, 2025 14:03
Use a longer, more descriptive parameter name to avoid false positives
when searching for the parameter name in string assertions.

Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
Replace HasEffectiveDefaultValue and GetEffectiveDefaultValue with a single
TryGetEffectiveDefaultValue method to avoid resolving the DefaultValueAttribute
multiple times, improving performance.

Co-authored-by: eiriktsarpalis <2813363+eiriktsarpalis@users.noreply.github.com>
Copilot AI requested a review from eiriktsarpalis October 22, 2025 14:47
This was referenced Nov 12, 2025
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