Skip to content

Commit

Permalink
misc: use hash suffix instead of prefix in get_model_key (#214)
Browse files Browse the repository at this point in the history
* misc: use hash suffix instead of prefix when getting model unique name

* improve docstrings
  • Loading branch information
yedpodtrzitko committed Apr 24, 2022
1 parent 744f0c9 commit f04c9b7
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 31 deletions.
30 changes: 6 additions & 24 deletions spectree/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions tests/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"})

Expand Down Expand Up @@ -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)}"
15 changes: 12 additions & 3 deletions tests/test_plugin.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
3 changes: 1 addition & 2 deletions tests/test_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
3 changes: 1 addition & 2 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -13,7 +12,7 @@
parse_resp,
)

from .common import DemoModel, DemoQuery
from .common import DemoModel, DemoQuery, get_model_path_key

api = SpecTree()

Expand Down

0 comments on commit f04c9b7

Please sign in to comment.