Skip to content

Query parameters miss validation attributes with MicroElements.AspNetCore.OpenApi.FluentValidation #200

@saithis

Description

@saithis

We switched from swagger + MicroElements.Swashbuckle.FluentValidation to microsofts openapi generator + MicroElements.AspNetCore.OpenApi.FluentValidation.

We have a minimal api endpoint with [AsParameters] QueryParameters dto, and this validator:

    public class QueryParametersValidator : AbstractValidator<QueryParameters>
    {
        public RequestDtoValidator()
        {
            RuleFor(x => x.Skip).GreaterThanOrEqualTo(0).WithMessage("'Skip' must be greater than or equal to 0.");

            RuleFor(x => x.Take).InclusiveBetween(1, 100).WithMessage("'Take' must be between 1 and 100.");
        }
    }

Before the generated spec was:

   /api/endpoint: {
      get: {
        parameters: [
          {
            in: query,
            name: Skip,
            required: true,
            schema: {
              exclusiveMinimum: false,
              format: int32,
              minimum: 0,
              nullable: true,
              type: integer
            }
          },
          {
            description: Maximum 100 items can be returned.,
            in: query,
            name: Take,
            required: true,
            schema: {
              description: Maximum 100 items can be returned.,
              exclusiveMaximum: false,
              exclusiveMinimum: false,
              format: int32,
              maximum: 100,
              minimum: 1,
              nullable: true,
              type: integer
            }
          }
        ],

Now it missed the validation properties:

   /api/endpoint: {
      get: {
        parameters: [
          {
            in: query,
            name: Skip,
            schema: {
              format: int32,
              minimum: 0,
              type: integer
            }
          },
          {
            description: Maximum 100 items can be returned.,
            in: query,
            name: Take,
            schema: {
              format: int32,
              maximum: 100,
              minimum: 1,
              type: integer
            }
          }
        ],

For post endpoints with a request body it works just fine, only for query params it doesn't.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions