Skip to content
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

Mismatch behavior between RegexRouteConstraint and produced OpenApi document #2626

Closed
StasPerekrestov opened this issue Apr 4, 2023 · 5 comments

Comments

@StasPerekrestov
Copy link

When a regex-based constraint is defined,
for instance

endpoints.MapGet("/{username:alpha}", async (string username, CancellationToken cancellationToken) =>
{

AlphaRouteConstraint is applied. The constraint inherits RegexRouteConstraint which is case insensitive.
The issue is that RegexRouteConstraint.Constraint & RegexOptions.IgnoreCase isn't taken into account.

"users/{username}": {
      "get": {
        "tags": [
          "demo"
        ],
        "parameters": [
          {
            "name": "username",
            "in": "path",
            "required": true,
            "schema": {
              "pattern": "^[a-z]*$",
              "type": "string"
            }
          }
        ],

In a nutshell,
the behavior of ^[a-z]*$ is different in the OpenApi document and in AlphaRouteConstraint because the first one is case sensitive and the other one is case insensitive.

public class AlphaRouteConstraint : RegexRouteConstraint
{
    /// <summary>
    /// Initializes a new instance of the <see cref="AlphaRouteConstraint" /> class.
    /// </summary>
    public AlphaRouteConstraint() : base(@"^[a-z]*$") <--------------------
    {
    }
}

This behavior can be observed in Swagger UI when it doesn't allow to execute an HTTP request due to a failed validation:
For instance, "Jack" cannot pass the provided regex constraint.

https://swagger.io/docs/specification/data-models/data-types/
The pattern keyword lets you define a regular expression template for the string value. Only the values that match this template will be accepted. The regular expression syntax used is from JavaScript (more specifically, ECMA 262). Regular expressions are case-sensitive, that is, [a-z] and [A-Z] are different expressions.

@SDOsmany
Copy link

I am having the same issue in my .Net6 Framework app
when using the alpha constraint it becomes case sensitive

gives the following error

Please correct the following validation errors and try again. Value must follow pattern ^[a-z]*$

@Havunen
Copy link

Havunen commented Feb 21, 2024

This seems to work in DotSwashbuckle

image

Copy link
Contributor

This issue is stale because it has been open for 60 days with no activity. It will be automatically closed in 14 days if no further updates are made.

@github-actions github-actions bot added the stale Stale issues or pull requests label Jun 23, 2024
@martincostello martincostello removed the stale Stale issues or pull requests label Jun 23, 2024
@jgarciadelanoceda
Copy link
Contributor

@StasPerekrestov or @SDOsmany. I have just tested using MyGet nuget and it seems to be working.
Can you please specify which version of Swashbuckle are you using?

@StasPerekrestov
Copy link
Author

@jgarciadelanoceda
I've checked the asp.net core code and found out that the issue is fixed in .NET 8
.NET 6
.NET 7
.NET 8

I suppose the issue can be closed.
Thank you for looking into it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants