Skip to content

[.NET 10] Runtime issue when adding a MaxLengthAttribute to a Guid: "The field of type System.Guid must be a string..." #62762

@erwinkramer

Description

@erwinkramer

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

I'm setting up validation but it doesn't seem to accept a MaxLengthAttribute construction when the type is a Guid.

Now you might ask, why does it make sense to add a maxLength validation to a Guid, when it's already a uuid in the OpenAPI definition, and that can only be 36-characters? Well, it's mainly because this spectral rule requests it: https://github.com/stoplightio/spectral-owasp-ruleset/blob/624d67f3a124de36502ad3d06c35917da19dff85/src/ruleset.ts#L533. Really that might not be the best excuse, but it is one. A related and interesting discussion regarding length constraints and formats can be found here: stoplightio/spectral-owasp-ruleset#41. Per OpenAPI spec (https://spec.openapis.org/oas/v3.1.1.html#data-type-format): "Tools that do not recognize a specific format MAY default back to the type alone, as if the format is not specified", so that might be another excuse to just have the maxLength in.

The validation message particularly seems strange. It might be better to give a build-time error, since it's just a broken setup that cannot accept any requests.

Expected Behavior

No response

Steps To Reproduce

Add this attribute to a Guid type:

public class IdAttribute : MaxLengthAttribute
{
    public IdAttribute() : base(36) { }
}

Response:

{
  "type": "https://tools.ietf.org/html/rfc9110#section-15.5.1",
  "title": "One or more validation errors occurred.",
  "status": 400,
  "instance": "POST /banks",
  "errors": {
    "Id": [
      "The field of type System.Guid must be a string, array or ICollection type."
    ]
  },
  "traceId": "00-c1e85619f97b5c95a37bd8a1aada4380-afe11599ea5775a9-01",
  "requestId": "0HNE4LBCI97OJ:0000000D"
}

Exceptions (if any)

No response

.NET Version

10.0.100-preview.6.25358.103

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    ✔️ Resolution: AnsweredResolved because the question asked by the original author has been answered.Status: Resolvedarea-minimalIncludes minimal APIs, endpoint filters, parameter binding, request delegate generator etcfeature-validationIssues related to model validation in minimal and controller-based APIs

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions