Skip to content

Fix validation to accept lowercase status code ranges (4xx, 5xx) in OpenAPI responses #2415

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

Merged
merged 6 commits into from
Jun 25, 2025

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Jun 25, 2025

The OpenAPI validation was incorrectly rejecting lowercase status code ranges like "4xx" and "5xx" in response definitions. The regex pattern only accepted uppercase "XX" variants.

Problem

When defining OpenAPI responses with lowercase status code ranges, validation would fail:

var responses = new OpenApiResponses
{
    ["4xx"] = new OpenApiResponse { Description = "Client error" },  // ❌ Failed validation
    ["5xx"] = new OpenApiResponse { Description = "Server error" }   // ❌ Failed validation
};

Error: Responses key must be 'default', an HTTP status code, or one of the following strings representing a range of HTTP status codes: '1XX', '2XX', '3XX', '4XX', '5XX'

Solution

  • Fixed regex pattern: Changed from ^[1-5](?>[0-9]{2}|XX)$ to ^[1-5](?>[0-9]{2}|[xX]{2})$ to accept both cases
  • Added GeneratedRegex support: Implemented [GeneratedRegex] attribute for .NET 8+ targets for improved performance
  • Maintained backward compatibility: Used conditional compilation to support netstandard2.0
  • Updated error message: Now mentions "(case insensitive)" to clarify accepted formats

Changes Made

  1. OpenApiResponsesRules.cs:
    • Made class partial to support GeneratedRegex
    • Added case-insensitive regex pattern with conditional compilation
    • Updated validation error message
  2. Added comprehensive tests: Created OpenApiResponsesValidationTests.cs with test cases for:
    • All valid status code formats (uppercase, lowercase, numeric)
    • Mixed case scenarios
    • Backward compatibility verification
  3. Fixed compilation issues: Resolved ambiguous string.Split calls in BaseOpenApiReference.cs, OpenApiDocument.cs, and OpenApiWorkspace.cs

Test Results

All tests pass, confirming the fix works correctly:

  • ✅ Lowercase "4xx", "5xx" now accepted
  • ✅ Uppercase "4XX", "5XX" continue to work
  • ✅ Mixed case scenarios work as expected
  • ✅ Existing functionality preserved

Now both formats work seamlessly:

var responses = new OpenApiResponses
{
    ["4xx"] = new OpenApiResponse { Description = "Client error" },  // ✅ Now works
    ["4XX"] = new OpenApiResponse { Description = "Client error" },  // ✅ Still works
    ["200"] = new OpenApiResponse { Description = "Success" }        // ✅ Still works
};

Fixes #2414.


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

@Copilot Copilot AI changed the title [WIP] Parsing a description with 4xx or 5xx (lowercase) leads to an error Fix validation to accept lowercase status code ranges (4xx, 5xx) in OpenAPI responses Jun 25, 2025
Copilot finished work on behalf of baywet June 25, 2025 17:50
@Copilot Copilot AI requested a review from baywet June 25, 2025 17:50
@Copilot Copilot AI requested a review from baywet June 25, 2025 18:13
Copilot finished work on behalf of baywet June 25, 2025 18:13
@baywet baywet marked this pull request as ready for review June 25, 2025 18:13
@baywet baywet requested a review from a team as a code owner June 25, 2025 18:13
@baywet baywet enabled auto-merge (squash) June 25, 2025 18:13
Copy link

@baywet baywet merged commit 09f661f into main Jun 25, 2025
15 of 17 checks passed
@baywet baywet deleted the copilot/fix-2414 branch June 25, 2025 18:22
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.

Parsing a description with 4xx or 5xx (lowercase) leads to an error
3 participants