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

get rid of pydantic v2>v3 deprication warning; add optional indentation #170

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions examples/registration.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
)

print("\n[Registration Options - Simple]")
print(options_to_json(simple_registration_options))
print(options_to_json(simple_registration_options, indent=2))

# Complex Options
complex_registration_options = generate_registration_options(
Expand All @@ -52,7 +52,7 @@
)

print("\n[Registration Options - Complex]")
print(options_to_json(complex_registration_options))
print(options_to_json(complex_registration_options, indent=2))

# Registration Response Verification
registration_verification = verify_registration_response(
Expand Down
22 changes: 16 additions & 6 deletions webauthn/helpers/options_to_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,23 @@ def options_to_json(
options: Union[
PublicKeyCredentialCreationOptions,
PublicKeyCredentialRequestOptions,
]
],
indent: int|None=None,
) -> str:
"""
Prepare options for transmission to the front end as JSON
"""
return options.json(
by_alias=True,
exclude_unset=False,
exclude_none=True,
)
if hasattr(options, 'model_dump_json'): # model_dump_json was introduced in v2..
return options.model_dump_json( # ..won't work in v1.
by_alias=True,
exclude_unset=False,
exclude_none=True,
indent=indent,
)
else:
return options.json( # noqa: D102
by_alias=True,
exclude_unset=False,
exclude_none=False,
indent=indent,
)
42 changes: 40 additions & 2 deletions webauthn/helpers/structs.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from enum import Enum
from typing import Callable, List, Literal, Optional, Any, Dict

from typing import Callable, List, Literal, Optional, Any, Dict, TypeVar
import typing_extensions

try:
from pydantic import ( # type: ignore[attr-defined]
Expand All @@ -10,6 +10,7 @@
FieldValidationInfo,
model_serializer,
)
from pydantic.deprecated import parse as _deprecated_parse

PYDANTIC_V2 = True
except ImportError:
Expand All @@ -18,6 +19,9 @@

PYDANTIC_V2 = False

Model = TypeVar('Model', bound='BaseModel')
IncEx: typing_extensions.TypeAlias = 'set[int] | set[str] | dict[int, Any] | dict[str, Any] | None'

from .base64url_to_bytes import base64url_to_bytes
from .bytes_to_base64url import bytes_to_base64url
from .cose import COSEAlgorithmIdentifier
Expand Down Expand Up @@ -98,6 +102,40 @@ def _pydantic_v2_serialize_bytes_fields(

return serialized

@classmethod
def parse_raw( # noqa: D102
cls: type[Model],
b: str | bytes,
*,
content_type: str | None = None,
encoding: str = 'utf8',
proto: _deprecated_parse.Protocol | None = None,
allow_pickle: bool = False,
) -> Model:
return super().model_validate_json(b)

def json( # noqa: D102
self,
*,
include: IncEx = None,
exclude: IncEx = None,
by_alias: bool = False,
exclude_unset: bool = False,
exclude_defaults: bool = False,
exclude_none: bool = False,
encoder: Callable[[Any], Any] | None = None, # type: ignore[assignment]
models_as_dict: bool = None, # type: ignore[assignment]
**dumps_kwargs: Any,
) -> str:
return self.model_dump_json(
include=include,
exclude=exclude,
by_alias=by_alias,
exclude_unset=exclude_unset,
exclude_defaults=exclude_defaults,
exclude_none=exclude_none,
)

else:

class Config:
Expand Down
Loading