From f04c9b79927cf4bc39eaf3ae2694297e611af2ef Mon Sep 17 00:00:00 2001 From: yed podtrzitko Date: Sun, 24 Apr 2022 17:03:23 +0700 Subject: [PATCH] misc: use hash suffix instead of prefix in get_model_key (#214) * misc: use hash suffix instead of prefix when getting model unique name * improve docstrings --- spectree/utils.py | 30 ++++++------------------------ tests/common.py | 16 ++++++++++++++++ tests/test_plugin.py | 15 ++++++++++++--- tests/test_response.py | 3 +-- tests/test_utils.py | 3 +-- 5 files changed, 36 insertions(+), 31 deletions(-) diff --git a/spectree/utils.py b/spectree/utils.py index 73a3ae68..ca4d477d 100644 --- a/spectree/utils.py +++ b/spectree/utils.py @@ -191,48 +191,30 @@ def default_after_handler( def hash_module_path(module_path: str): """ - generate short hashed prefix for module path + generate short hash for module path - :param modelpath: `str` module path + :param module_path: `str` module path """ return sha1(module_path.encode()).hexdigest()[:7] -def get_model_path_key(model_path: str): - """ - generate short hashed prefix for module path (instead of its path to avoid - code-structure leaking) - - :param modelpath: `str` model path in string - """ - - model_path_parts = model_path.rsplit(".", 1) - if len(model_path_parts) > 1: - hashed_module_path = hash_module_path(module_path=model_path_parts[0]) - model_path_key = f"{hashed_module_path}.{model_path_parts[1]}" - else: - model_path_key = model_path_parts[0] - - return model_path_key - - def get_model_key(model: ModelType) -> str: """ - generate model name prefixed by short hashed path (instead of its path to + generate model name suffixed by short hashed path (instead of its path to avoid code-structure leaking) :param model: `pydantic.BaseModel` query, json, headers or cookies from request or response """ - return f"{hash_module_path(module_path=model.__module__)}.{model.__name__}" + return f"{model.__name__}.{hash_module_path(module_path=model.__module__)}" def get_model_schema(model: ModelType): """ - return a dictionary representing the model as JSON Schema with using hashed - prefix in ref + return a dictionary representing the model as JSON Schema with a hashed + infix in ref to ensure name uniqueness :param model: `pydantic.BaseModel` query, json, headers or cookies from request or response diff --git a/tests/common.py b/tests/common.py index 5a103dd8..348cc007 100644 --- a/tests/common.py +++ b/tests/common.py @@ -4,6 +4,7 @@ from pydantic import BaseModel, Field, root_validator from spectree import SecurityScheme, Tag +from spectree.utils import hash_module_path api_tag = Tag(name="API", description="🐱", externalDocs={"url": "https://pypi.org"}) @@ -133,3 +134,18 @@ def get_paths(spec): }, {"name": "wrong_Data", "data": {"x": "y"}}, ] + + +def get_model_path_key(model_path: str) -> str: + """ + generate short hashed prefix for module path (instead of its path to avoid + code-structure leaking) + + :param model_path: `str` model path in string + """ + + model_path, _, model_name = model_path.rpartition(".") + if not model_path: + return model_name + + return f"{model_name}.{hash_module_path(module_path=model_path)}" diff --git a/tests/test_plugin.py b/tests/test_plugin.py index dc0ed240..361c747e 100644 --- a/tests/test_plugin.py +++ b/tests/test_plugin.py @@ -1,8 +1,17 @@ import pytest -from spectree.utils import get_model_key, get_model_path_key, get_model_schema - -from .common import JSON, SECURITY_SCHEMAS, Cookies, Headers, Query, Resp, get_paths +from spectree.utils import get_model_key, get_model_schema + +from .common import ( + JSON, + SECURITY_SCHEMAS, + Cookies, + Headers, + Query, + Resp, + get_model_path_key, + get_paths, +) from .test_plugin_falcon import api as falcon_api from .test_plugin_flask import api as flask_api from .test_plugin_flask import api_global_secure as flask_api_global_secure diff --git a/tests/test_response.py b/tests/test_response.py index ab1bbaa9..519cf67b 100644 --- a/tests/test_response.py +++ b/tests/test_response.py @@ -2,9 +2,8 @@ from spectree.models import ValidationError from spectree.response import DEFAULT_CODE_DESC, Response -from spectree.utils import get_model_path_key -from .common import JSON, DemoModel +from .common import JSON, DemoModel, get_model_path_key class NormalClass: diff --git a/tests/test_utils.py b/tests/test_utils.py index 88a42f48..802c0215 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -3,7 +3,6 @@ from spectree.response import Response from spectree.spec import SpecTree from spectree.utils import ( - get_model_path_key, has_model, parse_code, parse_comments, @@ -13,7 +12,7 @@ parse_resp, ) -from .common import DemoModel, DemoQuery +from .common import DemoModel, DemoQuery, get_model_path_key api = SpecTree()