Skip to content

Commit

Permalink
Password format unmarshaller
Browse files Browse the repository at this point in the history
  • Loading branch information
p1c2u committed Jul 10, 2023
1 parent 6acd56b commit 0f09c9d
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 4 deletions.
2 changes: 2 additions & 0 deletions openapi_core/unmarshalling/schemas/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from openapi_core.unmarshalling.schemas.unmarshallers import TypesUnmarshaller
from openapi_core.unmarshalling.schemas.util import format_byte
from openapi_core.unmarshalling.schemas.util import format_date
from openapi_core.unmarshalling.schemas.util import format_password
from openapi_core.unmarshalling.schemas.util import format_uuid
from openapi_core.validation.schemas import (
oas30_read_schema_validators_factory,
Expand Down Expand Up @@ -68,6 +69,7 @@
"binary": bytes,
"uuid": format_uuid,
"byte": format_byte,
"password": format_password,
}
oas31_format_unmarshallers = oas30_format_unmarshallers

Expand Down
6 changes: 6 additions & 0 deletions openapi_core/unmarshalling/schemas/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
from typing import Union
from uuid import UUID

from pydantic import SecretStr


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

return float(value)


def format_password(value: str) -> SecretStr:
return SecretStr(value)
2 changes: 1 addition & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ jsonschema-spec = "^0.2.2"
asgiref = "^3.6.0"
jsonschema = {version = "^4.18.0a1", allow-prereleases = true}
multidict = {version = "^6.0.4", optional = true}
pydantic = "^1.10.9"

[tool.poetry.extras]
django = ["django"]
Expand Down
7 changes: 4 additions & 3 deletions tests/integration/unmarshalling/test_unmarshallers.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from isodate.tzinfo import FixedOffset
from jsonschema.exceptions import SchemaError
from jsonschema.exceptions import UnknownType
from pydantic import SecretStr

from openapi_core import Spec
from openapi_core.unmarshalling.schemas import (
Expand Down Expand Up @@ -165,7 +166,7 @@ def test_basic_types_invalid(self, unmarshallers_factory, type, value):
("int64", 13, 13),
("float", 3.14, 3.14),
("double", 3.14, 3.14),
("password", "passwd", "passwd"),
("password", "passwd", SecretStr("passwd")),
("date", "2018-12-13", date(2018, 12, 13)),
(
"date-time",
Expand Down Expand Up @@ -204,7 +205,7 @@ def test_basic_formats(
("integer", "int64", 13, 13),
("number", "float", 3.14, 3.14),
("number", "double", 3.14, 3.14),
("string", "password", "passwd", "passwd"),
("string", "password", "passwd", SecretStr("passwd")),
("string", "date", "2018-12-13", date(2018, 12, 13)),
(
"string",
Expand Down Expand Up @@ -345,7 +346,7 @@ def test_string_password(self, unmarshallers_factory):

result = unmarshaller.unmarshal(value)

assert result == value
assert result == SecretStr(value)

def test_string_uuid(self, unmarshallers_factory):
schema = {
Expand Down
24 changes: 24 additions & 0 deletions tests/unit/unmarshalling/test_schema_unmarshallers.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,30 @@ def custom_format_validator(value):
with pytest.raises(InvalidSchemaValue):
unmarshaller.unmarshal(value)

def test_schema_password_format_value_masked(
self, schema_unmarshaller_factory
):
schema = {
"type": "string",
"format": "password",
"pattern": "\\d+",
}
spec = Spec.from_dict(schema, validator=None)
value = "passwd"
schema_validators_factory = SchemaValidatorsFactory(
OAS30WriteValidator
)
unmarshaller = schema_unmarshaller_factory(
schema_validators_factory,
spec,
)

with pytest.raises(
InvalidSchemaValue,
match=f"not valid for schema of type {type}",
):
unmarshaller.unmarshal(value)

def test_schema_extra_format_validator_format_custom(
self, schema_unmarshaller_factory
):
Expand Down

0 comments on commit 0f09c9d

Please sign in to comment.