spec-first / connexion

Connexion is a modern Python web framework that makes spec-first and api-first development easy.
https://connexion.readthedocs.io/en/latest/
Apache License 2.0
4.48k stars 761 forks source link

Following example in documentation gives DeprecationWarning #1914

Open johanfrykebrant opened 5 months ago

johanfrykebrant commented 5 months ago

The documentation on how to how to implement custom type formats suggest doing something like this

import re
from jsonschema import draft4_format_checker

MONEY_RE = re.compile('^\$\s*\d+(\.\d\d)?')

@draft4_format_checker.checks('money')
def is_money(val):
    if not isinstance(val, str):
        return True
    return MONEY_RE.match(val)

But doing that results in the following error DeprecationWarning: Accessing jsonschema.draft4_format_checker is deprecated and will be removed in a future release. Instead, use the FORMAT_CHECKER attribute on the corresponding Validator.

I tried to solve it by doing like this instead

import re
from jsonschema import draft4_format_checker

MONEY_RE = re.compile('^\$\s*\d+(\.\d\d)?')

@Draft7Validator.FORMAT_CHECKER.checks('money')
def is_money(val):
    if not isinstance(val, str):
        return True
    return MONEY_RE.match(val)

This seems to be the wrong way of doing it since the custom type is not being validated at all. If I can get some assistance on how to solve this correctly I'd be happy to create a PR altering the documentation as needed.

rtb-zla-karma commented 3 months ago

Related to #1786 .