From 5ee9586febd931cbdce54527f40c53def4d747d1 Mon Sep 17 00:00:00 2001 From: billyrrr Date: Wed, 7 Aug 2019 23:43:14 -0700 Subject: [PATCH] Squashed commit of the following: commit f77ea0a4b51b3a26d3d829efac057d6267e7ff9e Author: Isaac Hernandez Date: Tue Jul 9 21:12:29 2019 -0500 Improvement solution for issue 311 I've wrote a better solution replacing the references '$ref' with the recursive method __replace_ref commit 3cb03f499b104affd73037267ead3943cd42320b Author: Isaac Hernandez Date: Mon Jun 24 08:52:34 2019 -0500 Fix index commit bdcfd33c5420996c0d93e4c4a95c7f52ca62518b Author: Isaac Hernandez Date: Mon Jun 24 08:51:30 2019 -0500 Validate if the referenced schema has divider commit bf9c17ee580e8ed2886e173fa249413dcdf5b463 Author: Isaac Hernandez Date: Sun Jun 23 23:31:04 2019 -0500 Remove the spaces in 361 line commit bf262dff5d96d0de63307f23aac6d0cbe7a5c07b Author: Isaac Hernandez Date: Sun Jun 23 23:16:13 2019 -0500 Solution for issue #311 Sorry for all changes, but it pretend to solve the issue https://github.com/rochacbruno/flasgger/issues/311 I loaded the main_def from file --- flasgger/utils.py | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/flasgger/utils.py b/flasgger/utils.py index 9f37d672..2947fab5 100644 --- a/flasgger/utils.py +++ b/flasgger/utils.py @@ -6,6 +6,7 @@ import inspect import os import re +import sys import jsonschema import yaml from six import string_types, text_type @@ -251,6 +252,40 @@ def wrapper(*args, **kwargs): return decorator +def __replace_ref(schema: dict, relative_path: str): + """ TODO: add dev docs + + :param schema: + :param relative_path: + :return: + """ + absolute_path = os.path.dirname(sys.argv[0]) + new_value = {} + for key, value in schema.items(): + if isinstance(value, dict): + new_value[key] = __replace_ref(value, relative_path) + elif key == '$ref': + if len(value) > 0 and value[0] == '/': + file_ref_path = absolute_path + value + else: + file_ref_path = relative_path + '/' + value + relative_path = os.path.dirname(file_ref_path) + with open(file_ref_path) as file: + file_content = file.read() + comment_index = file_content.rfind('---') + if comment_index > 0: + comment_index = comment_index + 3 + else: + comment_index = 0 + content = yaml.safe_load((file_content[comment_index:])) + new_value = content + if isinstance(content, dict): + new_value = __replace_ref(content, relative_path) + else: + new_value[key] = value + return new_value + + def validate( data=None, schema_id=None, filepath=None, root=None, definition=None, specs=None, validation_function=None, validation_error_handler=None): @@ -359,6 +394,13 @@ def validate( if validation_function is None: validation_function = jsonschema.validate + absolute_path = os.path.dirname(sys.argv[0]) + if filepath is None: + relative_path = absolute_path + else: + relative_path = os.path.dirname(filepath) + main_def = __replace_ref(main_def, relative_path) + try: validation_function(data, main_def) except Exception as err: @@ -808,6 +850,7 @@ class LazyString(StringLike): A lazy string *without* caching. The resulting string is regenerated for every request. """ + def __init__(self, func): """ Creates a `LazyString` object using `func` as the delayed closure. @@ -826,6 +869,7 @@ class CachedLazyString(LazyString): """ A lazy string with caching. """ + def __init__(self, func): """ Uses `__init__()` from the parent and initializes a cache.