Skip to content
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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Patched bravado jsonschema issue #1051

Merged
merged 5 commits into from
Oct 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -3,6 +3,7 @@
### Fixes
- Update jsonschema requirement with explicit `format` specifier ([#1010](https://github.com/neptune-ai/neptune-client/pull/1010))
- Escape inputs to SQL in Artifact LocalFileHashStorage ([#1034](https://github.com/neptune-ai/neptune-client/pull/1034))
- `jsonschema` requirements unpined and patched related Bravado issue ([#1051](https://github.com/neptune-ai/neptune-client/pull/1051))
- Version checking with importlib and versioneer config update ([#1048](https://github.com/neptune-ai/neptune-client/pull/1048))

### Changes
Expand Down
3 changes: 1 addition & 2 deletions requirements.txt
@@ -1,4 +1,4 @@
bravado
bravado>=11.0.0,<12.0.0
click>=7.0
future>=0.17.1
oauthlib>=2.1.0
Expand All @@ -16,5 +16,4 @@ urllib3
dataclasses>=0.6; python_version<"3.7"
swagger-spec-validator>=2.7.4
psutil
jsonschema[format]<4.0.0
importlib-metadata<4; python_version<"3.8.0"
4 changes: 4 additions & 0 deletions src/neptune/__init__.py
Expand Up @@ -13,6 +13,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
from neptune.common.patches import apply_patches
from neptune.legacy import (
ANONYMOUS,
ANONYMOUS_API_TOKEN,
Expand Down Expand Up @@ -46,3 +47,6 @@
stop,
)
from neptune.version import __version__

# Apply patches of external libraries
apply_patches()
27 changes: 27 additions & 0 deletions src/neptune/common/patches/__init__.py
@@ -0,0 +1,27 @@
#
# Copyright (c) 2022, Neptune Labs Sp. z o.o.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
__all__ = ["apply_patches"]

from neptune.common.patches.bravado import patch as bravado_patch

patches = [bravado_patch]


# Apply patches when importing a patching module
# Should be called before usages of patched objects
def apply_patches():
for patch in patches:
patch()
81 changes: 81 additions & 0 deletions src/neptune/common/patches/bravado.py
@@ -0,0 +1,81 @@
#
# Copyright (c) 2022, Neptune Labs Sp. z o.o.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import functools
import re

import bravado_core.model
from bravado_core.model import (
_bless_models,
_collect_models,
_get_unprocessed_uri,
_post_process_spec,
_tag_models,
)


def _run_post_processing(spec):
visited_models = {}

def _call_post_process_spec(spec_dict):
# Discover all the models in spec_dict
_post_process_spec(
spec_dict=spec_dict,
spec_resolver=spec.resolver,
on_container_callbacks=[
functools.partial(
_tag_models,
visited_models=visited_models,
swagger_spec=spec,
),
functools.partial(
_bless_models,
visited_models=visited_models,
swagger_spec=spec,
),
functools.partial(
_collect_models,
models=spec.definitions,
swagger_spec=spec,
),
],
)

# Post process specs to identify models
_call_post_process_spec(spec.spec_dict)

processed_uris = {
uri
for uri in spec.resolver.store
if uri == spec.origin_url or re.match(r"http(s)?://json-schema\.org/draft(/\d{4})?-\d+/(schema|meta/.*)", uri)
}
additional_uri = _get_unprocessed_uri(spec, processed_uris)
while additional_uri is not None:
# Post process each referenced specs to identify models in definitions of linked files
with spec.resolver.in_scope(additional_uri):
_call_post_process_spec(
spec.resolver.store[additional_uri],
)

processed_uris.add(additional_uri)
additional_uri = _get_unprocessed_uri(spec, processed_uris)


# Issue: https://github.com/Yelp/bravado-core/issues/388
# Bravado currently makes additional requests to `json-schema.org` in order to gather mission schemas
# This makes `neptune-client` unable to run without internet connection or with a many security policies
def patch():
# pylint: disable=protected-access
bravado_core.model._run_post_processing = _run_post_processing