From a99a4955bec16db6dae494ebe1712a3d74defc31 Mon Sep 17 00:00:00 2001 From: ota42y Date: Fri, 22 Sep 2023 13:31:48 +0000 Subject: [PATCH] add normalize_name method --- flask_openapi3/utils.py | 7 ++++--- tests/test_utils.py | 8 ++++++++ 2 files changed, 12 insertions(+), 3 deletions(-) create mode 100644 tests/test_utils.py diff --git a/flask_openapi3/utils.py b/flask_openapi3/utils.py index d6f41880..c27e525b 100644 --- a/flask_openapi3/utils.py +++ b/flask_openapi3/utils.py @@ -10,9 +10,6 @@ from flask import make_response, current_app from flask.wrappers import Response as FlaskResponse from pydantic import BaseModel, ValidationError -from pydantic.schema import normalize_name -# GenerateJsonSchema class have this method in pydantic 2-x -# use `from pydantic.json_schema import GenerateJsonSchema` from ._http import HTTP_STATUS, HTTPMethod from .models import Encoding @@ -593,3 +590,7 @@ def convert_responses_key_to_string(responses: ResponseDict) -> ResponseStrKeyDi _responses[key] = value return _responses + + +def normalize_name(name: str) -> str: + return re.sub(r'[^\w.\-]', '_', name) diff --git a/tests/test_utils.py b/tests/test_utils.py new file mode 100644 index 00000000..8ed1fb87 --- /dev/null +++ b/tests/test_utils.py @@ -0,0 +1,8 @@ +# -*- coding: utf-8 -*- +# @Author : llc +# @Time : 2022/12/19 10:34 + +from flask_openapi3.utils import normalize_name + +def test_normalize_name(): + assert "List-Generic.Response_Detail_" == normalize_name("List-Generic.Response[Detail]")