Skip to content

fix: safely convert random_user_agent to bool for Pydantic 2.13#506

Closed
Watermelcn wants to merge 2 commits into
PasarGuard:mainfrom
Watermelcn:fix/pydantic-bool-conversion
Closed

fix: safely convert random_user_agent to bool for Pydantic 2.13#506
Watermelcn wants to merge 2 commits into
PasarGuard:mainfrom
Watermelcn:fix/pydantic-bool-conversion

Conversation

@Watermelcn
Copy link
Copy Markdown

@Watermelcn Watermelcn commented May 19, 2026

Fixes #504

Problem: Pydantic 2.13 strict mode converts False to 0 (int), and when a pattern constraint is applied to a non-string field, it throws TypeError.

Previous approach (#505): Changed random_user_agent from bool to str.

This fix: Keeps random_user_agent: bool as the maintainer intended. Instead, safely converts the database value to bool before passing it to the Pydantic model constructor using bool(value) if value is not None else False.

Summary by CodeRabbit

  • Bug Fixes
    • Normalize user-agent randomization flag across subscription transports to a strict boolean, improving consistency.
    • Normalize numeric-or-range transport fields when integers are provided (converted for consistent validation/serialization), reducing validation errors.

Review Change Stack

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 19, 2026

Caution

Review failed

The pull request is closed.

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 739e1e3b-3072-4147-be2d-2eed2011acd9

📥 Commits

Reviewing files that changed from the base of the PR and between ef373f7 and b0b07d2.

📒 Files selected for processing (1)
  • app/models/subscription.py

Walkthrough

Coerce host.random_user_agent to a strict boolean across all transport config branches and final SubscriptionInboundData, and add a Pydantic before-mode validator on XHTTPTransportConfig to convert integer numeric-or-range inputs to strings.

Changes

Boolean normalization for random_user_agent field & XHTTP validator

Layer / File(s) Summary
Transport and subscription data boolean normalization
app/core/hosts.py
random_user_agent is coerced with bool(host.random_user_agent) when not None, otherwise False, across xhttp/splithttp, grpc/gun, ws/websocket/httpupgrade, tcp/raw/http/h2, fallback, and the final SubscriptionInboundData construction.
XHTTP numeric-or-range field validator
app/models/subscription.py
Added a @field_validator(..., mode="before") on XHTTPTransportConfig that converts int inputs to str for sc_max_each_post_bytes, sc_min_posts_interval_ms, and uplink_chunk_size.

🎯 3 (Moderate) | ⏱️ ~20 minutes

🐰 A tiny patch I gladly bring,
Bool hops in, and numbers sing,
Ints become strings before the gate,
Now Pydantic sleeps — no crash, all's great!
Hooray for tidy, sane datatype spring. 🥕

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: safely converting random_user_agent to bool for Pydantic 2.13 compatibility, which matches the code changes normalizing the field to a strict boolean.
Linked Issues check ✅ Passed The PR fully addresses issue #504 by implementing the safe conversion approach: using bool(value) if value is not None else False to ensure boolean values are passed to Pydantic models, avoiding the pattern-on-non-string TypeError.
Out of Scope Changes check ✅ Passed All changes in app/core/hosts.py are directly within scope: the modifications normalize random_user_agent to boolean in _prepare_subscription_inbound_data for multiple transport configs and the SubscriptionInboundData object, addressing the root cause of issue #504.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@M03ED
Copy link
Copy Markdown
Contributor

M03ED commented May 19, 2026

so now we convert bool to bool ?
image

@M03ED M03ED closed this May 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: Panel crashes on startup due to random_user_agent type mismatch with Pydantic 2.13

2 participants