Skip to content

Commit 0f09c9d

Browse files
committed
Password format unmarshaller
1 parent 6acd56b commit 0f09c9d

File tree

6 files changed

+38
-4
lines changed

6 files changed

+38
-4
lines changed

openapi_core/unmarshalling/schemas/__init__.py

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from openapi_core.unmarshalling.schemas.unmarshallers import TypesUnmarshaller
1818
from openapi_core.unmarshalling.schemas.util import format_byte
1919
from openapi_core.unmarshalling.schemas.util import format_date
20+
from openapi_core.unmarshalling.schemas.util import format_password
2021
from openapi_core.unmarshalling.schemas.util import format_uuid
2122
from openapi_core.validation.schemas import (
2223
oas30_read_schema_validators_factory,
@@ -68,6 +69,7 @@
6869
"binary": bytes,
6970
"uuid": format_uuid,
7071
"byte": format_byte,
72+
"password": format_password,
7173
}
7274
oas31_format_unmarshallers = oas30_format_unmarshallers
7375

openapi_core/unmarshalling/schemas/util.py

+6
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
from typing import Union
77
from uuid import UUID
88

9+
from pydantic import SecretStr
10+
911

1012
def format_date(value: str) -> date:
1113
return datetime.strptime(value, "%Y-%m-%d").date()
@@ -26,3 +28,7 @@ def format_number(value: str) -> Union[int, float]:
2628
return value
2729

2830
return float(value)
31+
32+
33+
def format_password(value: str) -> SecretStr:
34+
return SecretStr(value)

poetry.lock

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

+1
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ jsonschema-spec = "^0.2.2"
7575
asgiref = "^3.6.0"
7676
jsonschema = {version = "^4.18.0a1", allow-prereleases = true}
7777
multidict = {version = "^6.0.4", optional = true}
78+
pydantic = "^1.10.9"
7879

7980
[tool.poetry.extras]
8081
django = ["django"]

tests/integration/unmarshalling/test_unmarshallers.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from isodate.tzinfo import FixedOffset
99
from jsonschema.exceptions import SchemaError
1010
from jsonschema.exceptions import UnknownType
11+
from pydantic import SecretStr
1112

1213
from openapi_core import Spec
1314
from openapi_core.unmarshalling.schemas import (
@@ -165,7 +166,7 @@ def test_basic_types_invalid(self, unmarshallers_factory, type, value):
165166
("int64", 13, 13),
166167
("float", 3.14, 3.14),
167168
("double", 3.14, 3.14),
168-
("password", "passwd", "passwd"),
169+
("password", "passwd", SecretStr("passwd")),
169170
("date", "2018-12-13", date(2018, 12, 13)),
170171
(
171172
"date-time",
@@ -204,7 +205,7 @@ def test_basic_formats(
204205
("integer", "int64", 13, 13),
205206
("number", "float", 3.14, 3.14),
206207
("number", "double", 3.14, 3.14),
207-
("string", "password", "passwd", "passwd"),
208+
("string", "password", "passwd", SecretStr("passwd")),
208209
("string", "date", "2018-12-13", date(2018, 12, 13)),
209210
(
210211
"string",
@@ -345,7 +346,7 @@ def test_string_password(self, unmarshallers_factory):
345346

346347
result = unmarshaller.unmarshal(value)
347348

348-
assert result == value
349+
assert result == SecretStr(value)
349350

350351
def test_string_uuid(self, unmarshallers_factory):
351352
schema = {

tests/unit/unmarshalling/test_schema_unmarshallers.py

+24
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,30 @@ def custom_format_validator(value):
164164
with pytest.raises(InvalidSchemaValue):
165165
unmarshaller.unmarshal(value)
166166

167+
def test_schema_password_format_value_masked(
168+
self, schema_unmarshaller_factory
169+
):
170+
schema = {
171+
"type": "string",
172+
"format": "password",
173+
"pattern": "\\d+",
174+
}
175+
spec = Spec.from_dict(schema, validator=None)
176+
value = "passwd"
177+
schema_validators_factory = SchemaValidatorsFactory(
178+
OAS30WriteValidator
179+
)
180+
unmarshaller = schema_unmarshaller_factory(
181+
schema_validators_factory,
182+
spec,
183+
)
184+
185+
with pytest.raises(
186+
InvalidSchemaValue,
187+
match=f"not valid for schema of type {type}",
188+
):
189+
unmarshaller.unmarshal(value)
190+
167191
def test_schema_extra_format_validator_format_custom(
168192
self, schema_unmarshaller_factory
169193
):

0 commit comments

Comments
 (0)