Fix validation to accept lowercase status code ranges (4xx, 5xx) in OpenAPI responses#2415
Merged
Fix validation to accept lowercase status code ranges (4xx, 5xx) in OpenAPI responses#2415
Conversation
Co-authored-by: baywet <7905502+baywet@users.noreply.github.com>
…eneratedRegex support Co-authored-by: baywet <7905502+baywet@users.noreply.github.com>
…lution works Co-authored-by: baywet <7905502+baywet@users.noreply.github.com>
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
baywet
reviewed
Jun 25, 2025
src/Microsoft.OpenApi/Validations/Rules/OpenApiResponsesRules.cs
Outdated
Show resolved
Hide resolved
baywet
approved these changes
Jun 25, 2025
|
adrian05-ms
approved these changes
Jun 25, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



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:
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
^[1-5](?>[0-9]{2}|XX)$to^[1-5](?>[0-9]{2}|[xX]{2})$to accept both cases[GeneratedRegex]attribute for .NET 8+ targets for improved performanceChanges Made
partialto support GeneratedRegexOpenApiResponsesValidationTests.cswith test cases for:string.Splitcalls in BaseOpenApiReference.cs, OpenApiDocument.cs, and OpenApiWorkspace.csTest Results
All tests pass, confirming the fix works correctly:
Now both formats work seamlessly:
Fixes #2414.
💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.