Skip to content

[Bug]: Query parameter validation fails to match empty string when listed as a valid enum value #1210

Description

@Youssef1313

Actual Behavior

Run the following code:

from openapi_core import OpenAPI
from openapi_core.testing import MockRequest

SPEC = {
    "openapi": "3.2.0",
    "info": {"title": "Sample", "version": "1.0.0"},
    "paths": {
        "/api": {
            "get": {
                "tags": ["Sample"],
                "parameters": [
                    {
                        "name": "status",
                        "in": "query",
                        "schema": {"enum": ["Active", "Inactive", "Pending", ""]},
                    }
                ]
            }
        }
    },
    "tags": [{"name": "Sample"}],
}

openapi = OpenAPI.from_dict(SPEC)

# Empty query parameter: status=
request = MockRequest(
    host_url="http://localhost",
    method="get",
    path="/api",
    args={"status": ""},
)

if hasattr(openapi, "iter_request_errors"):
    errors = list(openapi.iter_request_errors(request))
    if errors:
        print("INVALID")
        for e in errors:
            print(f"  {type(e).__name__}: {e}")
    else:
        print("VALID")
else:
    try:
        openapi.validate_request(request)
        print("VALID")
    except Exception as exc:
        print("INVALID")
        print(f"  {type(exc).__name__}: {exc}")

It prints:

INVALID
  ParameterValidationError: Query parameter error: status

Expected Behavior

It should print VALID.

OpenAPI Core Version

0.23.1

See OAI/OpenAPI-Specification#5411

Metadata

Metadata

Assignees

No one assigned

    Labels

    kind/bugIndicates an issue

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions