-
Notifications
You must be signed in to change notification settings - Fork 45
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Issue 77 - Add Translation Feature #82
Merged
jfinkhaeuser
merged 13 commits into
RonnyPfannschmidt:issue-77-integration
from
tahmidefaz:issue-77
Dec 7, 2020
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
63963f5
trying to resolve references in external file
tahmidefaz a52c111
super hackey commit; works on our specific schema; does not work for …
tahmidefaz 020f995
cleanup
Glutexo 10016c2
Add SOFT/HARD switch.
Glutexo 59e877c
update soft ref objects internally
tahmidefaz 63909e8
dereferencing file schema in a different object
tahmidefaz 0740b33
create components/schemas if not available, added comments
tahmidefaz 342f007
simplifying condition and renaming to TRANSLATE EXTERNAL
tahmidefaz 1f5bd99
adding tests
tahmidefaz 07d42c0
adding param info
tahmidefaz a9ca080
removing extra indentation
tahmidefaz d005013
don't resolve internal refs when RESOLVE_INTERNAL is false
tahmidefaz 0558322
test
tahmidefaz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,11 @@ | |
#: Resolve references to local files. | ||
RESOLVE_FILES = 2 ** 3 | ||
|
||
#: Copy the schema changing the reference. | ||
TRANSLATE_EXTERNAL = 0 | ||
#: Replace the reference with inlined schema. | ||
TRANSLATE_DEFAULT = 1 | ||
|
||
#: Default, resole all references. | ||
RESOLVE_ALL = RESOLVE_INTERNAL | RESOLVE_HTTP | RESOLVE_FILES | ||
|
||
|
@@ -66,6 +71,9 @@ def __init__(self, specs, url = None, **options): | |
detect_encoding is used to determine the encoding. | ||
:param int resolve_types: [optional] Specify which types of references to | ||
resolve. Defaults to RESOLVE_ALL. | ||
:param int resolve_method: [optional] Specify whether to translate external | ||
references in components/schemas or dereference in place. Defaults | ||
to TRANSLATE_DEFAULT. | ||
""" | ||
import copy | ||
self.specs = copy.deepcopy(specs) | ||
|
@@ -89,11 +97,20 @@ def __init__(self, specs, url = None, **options): | |
self.parsed_url = self._url_key = None | ||
|
||
self.__resolve_types = options.get('resolve_types', RESOLVE_ALL) | ||
self.__resolve_method = options.get('resolve_method', TRANSLATE_DEFAULT) | ||
self.__encoding = options.get('encoding', None) | ||
self.__soft_dereference_objs = {} | ||
|
||
def resolve_references(self): | ||
"""Resolve JSON pointers/references in the spec.""" | ||
self.specs = self._resolve_partial(self.parsed_url, self.specs, ()) | ||
|
||
# If there are any objects collected when using TRANSLATE_EXTERNAL, add them to components/schemas | ||
if self.__soft_dereference_objs: | ||
if "components" not in self.specs: self.specs["components"] = dict() | ||
if "schemas" not in self.specs["components"]: self.specs["components"].update({"schemas":{}}) | ||
|
||
self.specs["components"]["schemas"].update(self.__soft_dereference_objs) | ||
|
||
def _dereferencing_iterator(self, base_url, partial, path, recursions): | ||
""" | ||
|
@@ -111,6 +128,8 @@ def _dereferencing_iterator(self, base_url, partial, path, recursions): | |
# Split the reference string into parsed URL and object path | ||
ref_url, obj_path = _url.split_url_reference(base_url, refstring) | ||
|
||
translate = self.__resolve_method == TRANSLATE_EXTERNAL and self.parsed_url.path != ref_url.path | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The translate variable is initialized here, but used only down there in the yield condition. I know this is a remnant from the original approach, but now it is only confusing. |
||
|
||
if self._skip_reference(base_url, ref_url): | ||
continue | ||
|
||
|
@@ -136,7 +155,20 @@ def _dereferencing_iterator(self, base_url, partial, path, recursions): | |
full_path = path + item_path | ||
|
||
# First yield parent | ||
yield full_path, ref_value | ||
if translate: | ||
url = self._collect_soft_refs(ref_url, obj_path, ref_value) | ||
yield full_path, {"$ref": "#/components/schemas/"+url} | ||
else: | ||
yield full_path, ref_value | ||
|
||
def _collect_soft_refs(self, ref_url, item_path, value): | ||
""" | ||
Returns a portion of the dereferenced url for TRANSLATE_EXTERNAL mode. | ||
format - ref-url_obj-path | ||
""" | ||
dref_url = ref_url.path.split("/")[-1]+"_"+"_".join(item_path[1:]) | ||
self.__soft_dereference_objs[dref_url] = value | ||
return dref_url | ||
|
||
def _skip_reference(self, base_url, ref_url): | ||
"""Return whether the URL should not be dereferenced.""" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The names are strange, but I know, naming is hard. This switches the handling of external references. One value is to TRANSLATE and the other is to INLINE.