Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "Fix default port for mongosrv DSNs (#6827)" #7116

Merged
merged 2 commits into from Aug 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 2 additions & 5 deletions pydantic/networks.py
Expand Up @@ -4,7 +4,7 @@
import dataclasses as _dataclasses
import re
from ipaddress import IPv4Address, IPv4Interface, IPv4Network, IPv6Address, IPv6Interface, IPv6Network
from typing import TYPE_CHECKING, Any, Union
from typing import TYPE_CHECKING, Any

from pydantic_core import MultiHostUrl, PydanticCustomError, Url, core_schema
from typing_extensions import Annotated, TypeAlias
Expand Down Expand Up @@ -124,10 +124,7 @@ def __hash__(self) -> int:
UrlConstraints(allowed_schemes=['redis', 'rediss'], default_host='localhost', default_port=6379, default_path='/0'),
]
"""A type that will accept any Redis DSN."""
MongoDsn = Union[
Annotated[MultiHostUrl, UrlConstraints(allowed_schemes=['mongodb'], default_port=27017)],
Annotated[MultiHostUrl, UrlConstraints(allowed_schemes=['mongodb+srv'])],
]
MongoDsn = Annotated[MultiHostUrl, UrlConstraints(allowed_schemes=['mongodb', 'mongodb+srv'], default_port=27017)]
"""A type that will accept any MongoDB DSN."""
KafkaDsn = Annotated[Url, UrlConstraints(allowed_schemes=['kafka'], default_host='localhost', default_port=9092)]
"""A type that will accept any Kafka DSN."""
Expand Down
26 changes: 20 additions & 6 deletions tests/test_networks.py
Expand Up @@ -615,14 +615,28 @@ class Model(BaseModel):
assert m.a.hosts() == [{'username': None, 'password': None, 'host': 'localhost', 'port': 27017}]


def test_mongodsn_default_ports():
@pytest.mark.parametrize(
('dsn', 'expected'),
[
('mongodb://user:pass@localhost/app', 'mongodb://user:pass@localhost:27017/app'),
pytest.param(
'mongodb+srv://user:pass@localhost/app',
'mongodb+srv://user:pass@localhost/app',
marks=pytest.mark.xfail(
reason=(
'This case is not supported. '
'Check https://github.com/pydantic/pydantic/pull/7116 for more details.'
)
),
),
],
)
def test_mongodsn_default_ports(dsn: str, expected: str):
class Model(BaseModel):
a: MongoDsn
dsn: MongoDsn

m1 = Model(a='mongodb://user:pass@localhost/app')
m2 = Model(a='mongodb+srv://user:pass@localhost/app')
assert str(m1.a) == 'mongodb://user:pass@localhost:27017/app'
assert str(m2.a) == 'mongodb+srv://user:pass@localhost/app'
m = Model(dsn=dsn)
assert str(m.dsn) == expected


def test_kafka_dsns():
Expand Down