Skip to content

RequestConfiguration instances share the same HeadersCollection instance by default #507

@jackmpcollins

Description

@jackmpcollins

The default for RequestConfiguration.headers is an instance of HeadersCollection. This is created at class definition time, meaning that all instances of RequestConfiguration share the same instance of HeadersCollection by default. This leads to conflict when different instances of RequestConfiguration add/remove headers.

@dataclass
class RequestConfiguration(Generic[QueryParameters]):
"""
Configuration for the request such as headers, query parameters, and middleware options.
"""
# Request headers
headers: HeadersCollection = HeadersCollection()

Instead, a factory should be used so that a new instance is created:

from dataclasses import dataclass, field
from typing import Generic, Optional, TypeVar

QueryParameters = TypeVar("QueryParameters")

@dataclass
class RequestConfiguration(Generic[QueryParameters]):
    headers: HeadersCollection = field(default_factory=HeadersCollection)
    options: Optional[list[RequestOption]] = None
    query_parameters: Optional[QueryParameters] = None

The same/similar issue also affects HeadersInspectionHandlerOption init param defaults.

class HeadersInspectionHandlerOption(RequestOption):
"""Config options for the HeaderInspectionHandler"""
HEADERS_INSPECTION_HANDLER_OPTION_KEY = "HeadersInspectionHandlerOption"
def __init__(
self,
inspect_request_headers: bool = True,
inspect_response_headers: bool = True,
request_headers: HeadersCollection = HeadersCollection(),
response_headers: HeadersCollection = HeadersCollection(),

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Status

    Needs Triage 🔍

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions