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

Open
#1,210 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
68/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Quiet
Tech stack
python
Domain
api

Research direction

Start with the OpenAPI.from_dict and MockRequest reproduction, then trace iter_request_errors through query-parameter enum validation. Add a regression test covering status= when the enum includes an empty string, and confirm the request is accepted without a ParameterValidationError.

Written by the indexing model from the issue text.

Description

kind/bug
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 https://github.com/OAI/OpenAPI-Specification/discussions/5411

Dominant language
Python
Stars
368
Forks
140
PR merge metrics
No merged PRs in 30d

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from python-openapi/openapi-core

All issues in python-openapi/openapi-core

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.