From 361af5e4b86cf1a91f405c25fef1ba079e735513 Mon Sep 17 00:00:00 2001 From: Takashi Kajinami Date: Wed, 20 Nov 2024 02:28:11 +0900 Subject: [PATCH] Replace deprecated FormatChecker.cls_checks The method was deprecated in jsonschema 4.14.0[1] and now triggers the following warning. DeprecationWarning: FormatChecker.cls_checks is deprecated. Call FormatChecker.checks on a specific FormatChecker instance instead. [1] https://github.com/python-jsonschema/jsonschema/commit/cd8f0592b93947a9deb8b3e6502cc5a69cb6d722 Closes-Bug: #2089051 Change-Id: I5c28a8b52f2199e6b0316198cd3708395e5b2463 --- manila/api/validation/validators.py | 54 +++++++++++++++-------------- 1 file changed, 28 insertions(+), 26 deletions(-) diff --git a/manila/api/validation/validators.py b/manila/api/validation/validators.py index 1cabbc1704..0eb6fd9f5c 100644 --- a/manila/api/validation/validators.py +++ b/manila/api/validation/validators.py @@ -117,30 +117,6 @@ def _validate_string_length( ) -@jsonschema.FormatChecker.cls_checks('date-time') -def _validate_datetime_format(instance: object) -> bool: - # format checks constrain to the relevant primitive type - # https://github.com/OAI/OpenAPI-Specification/issues/3148 - if not isinstance(instance, str): - return True - try: - timeutils.parse_isotime(instance) - except ValueError: - return False - else: - return True - - -@jsonschema.FormatChecker.cls_checks('uuid') -def _validate_uuid_format(instance: object) -> bool: - # format checks constrain to the relevant primitive type - # https://github.com/OAI/OpenAPI-Specification/issues/3148 - if not isinstance(instance, str): - return True - - return uuidutils.is_uuid_like(instance) - - class FormatChecker(jsonschema.FormatChecker): """A FormatChecker can output the message from cause exception @@ -176,6 +152,33 @@ class FormatChecker(jsonschema.FormatChecker): raise jsonschema_exc.FormatError(msg, cause=cause) +_FORMAT_CHECKER = FormatChecker() + + +@_FORMAT_CHECKER.checks('date-time') +def _validate_datetime_format(instance: object) -> bool: + # format checks constrain to the relevant primitive type + # https://github.com/OAI/OpenAPI-Specification/issues/3148 + if not isinstance(instance, str): + return True + try: + timeutils.parse_isotime(instance) + except ValueError: + return False + else: + return True + + +@_FORMAT_CHECKER.checks('uuid') +def _validate_uuid_format(instance: object) -> bool: + # format checks constrain to the relevant primitive type + # https://github.com/OAI/OpenAPI-Specification/issues/3148 + if not isinstance(instance, str): + return True + + return uuidutils.is_uuid_like(instance) + + class _SchemaValidator(object): """A validator class @@ -205,8 +208,7 @@ class _SchemaValidator(object): validator_cls = jsonschema.validators.extend( self.validator_org, validators ) - format_checker = FormatChecker() - self.validator = validator_cls(schema, format_checker=format_checker) + self.validator = validator_cls(schema, format_checker=_FORMAT_CHECKER) def validate(self, *args, **kwargs): try: