Skip to content

Add logging for OpenAPI schema default value type mismatches #63027

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

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Jul 31, 2025

Problem

The current ApplyDefaultValue method in OpenAPI schema generation fails when there are type mismatches between default values and their target types, particularly in F# scenarios. For example:

[<DefaultParameterValue(10)>] // int32 default
member this.GetData(id: uint64) = // uint64 parameter

This causes InvalidCastException during JsonSerializer.SerializeToNode() calls, preventing OpenAPI schema generation from completing successfully.

Solution

This PR implements a logging-based approach that:

  1. Attempts normal JSON serialization first - Maintains backward compatibility for all existing working scenarios
  2. Logs warnings on serialization exceptions - When type mismatches occur, logs a descriptive warning message using ILogger
  3. Omits default values for type mismatches - No default value is applied to the schema when types don't match, as recommended
  4. Remains trim-safe - No reflection-based operations are added

Implementation

The change adds logging infrastructure and modifies the ApplyDefaultValue method in JsonNodeSchemaExtensions.cs:

try
{
    schema[OpenApiSchemaKeywords.DefaultKeyword] = JsonSerializer.SerializeToNode(defaultValue, jsonTypeInfo);
}
catch (Exception ex) when (ex is InvalidCastException or NotSupportedException or InvalidOperationException)
{
    // Log warning when there's a type mismatch that prevents serialization
    logger?.DefaultValueTypeMismatch(defaultValue.GetType().Name, jsonTypeInfo.Type.Name);
    // Do not apply the default value when there's a type mismatch
}

Testing

Added comprehensive tests that verify:

  • Warning logs are emitted for type mismatches ([DefaultValue(10)] ulong id)
  • No default values are applied when types don't match
  • Existing scenarios continue to work unchanged

Compatibility

  • Backward compatible - Existing working scenarios are unchanged
  • Trim-safe - No new reflection or dynamic code generation
  • Handles F# scenarios - Resolves [<DefaultParameterValue>] type mismatches with proper logging
  • Follows recommendations - Logs warnings and omits mismatched default values instead of applying incorrect values

Fixes scenarios where OpenAPI generation would previously fail with InvalidCastException, now producing valid schemas while alerting developers to type mismatches through logging.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copy link
Contributor

Greetings human! You've submitted a PR that modifies code that is shared with https://github.com/dotnet/runtime . Please make sure you synchronize this code with the changes in that repo!

@Copilot Copilot AI changed the title [WIP] Implement fallback behavior for default value serialization with invariant culture string conversion Add trim-safe default value fallback for OpenAPI schema generation Jul 31, 2025
@Copilot Copilot AI requested a review from captainsafia July 31, 2025 22:42
Copilot finished work on behalf of captainsafia July 31, 2025 22:42
@captainsafia
Copy link
Member

@copilot It's recommended that the type of the default value match the type defined in the schema so this option is not viable. Update the PR to:

  • Handle serialization exceptions and log a warning to the user
  • Add new tests that verify that logs are emitted for cases where the default value type and the parameter type don't match

Copy link
Contributor Author

Copilot AI commented Jul 31, 2025

@copilot It's recommended that the type of the default value match the type defined in the schema so this option is not viable. Update the PR to:

  • Handle serialization exceptions and log a warning...

Updated the implementation to log warnings instead of using string fallback for default value type mismatches. The solution now:

  1. Handles serialization exceptions and logs warnings using ILogger
  2. Added tests that verify warning logs are emitted for type mismatches
  3. No longer applies default values when types don't match

Changes implemented in commit 4e363d3.

@Copilot Copilot AI changed the title Add trim-safe default value fallback for OpenAPI schema generation Add logging for OpenAPI schema default value type mismatches Jul 31, 2025
Copilot finished work on behalf of captainsafia July 31, 2025 23:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants