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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Schema Update #262

Merged
merged 11 commits into from
Aug 4, 2023
5 changes: 5 additions & 0 deletions snovault/elasticsearch/create_mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
get_uuids_for_types,
SCAN_PAGE_SIZE,
)
from ..schema_utils import load_schema
from .interfaces import ELASTIC_SEARCH, INDEXER_QUEUE
from ..settings import Settings

Expand Down Expand Up @@ -104,6 +105,8 @@ def schema_mapping(field, schema, top_level=False, from_array=False):
TODO: rename 'lower_case_sort' to 'lowercase' and adjust search code
"""
ignored(top_level) # TODO: maybe wants to be used below, but isn't yet?
if '$merge' in schema:
schema = load_schema(schema)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a risk of circularity here? Is something checking that already?

type_ = schema['type']

# Elasticsearch handles multiple values for a field
Expand Down Expand Up @@ -715,6 +718,8 @@ def type_mapping(types, item_type, embed=True):
# to relevant fields so that they are not mapped into full_text, for example.
properties = schema['properties']
for _, sub_mapping in properties.items():
if '$merge' in sub_mapping:
sub_mapping = load_schema(sub_mapping)
if sub_mapping['type'] == 'text':
sub_mapping['copy_to'] = ['full_text']
return mapping
Expand Down
2 changes: 1 addition & 1 deletion snovault/schema_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def resolve_remote(self, uri):
if any(s in uri for s in ['http', 'https', 'ftp', 'sftp']):
raise ValueError(f'Resolution disallowed for: {uri}')
else:
return load_schema(favor_app_specific_schema_ref(uri))
return load_schema(uri)


def favor_app_specific_schema(schema: str) -> str:
Expand Down
15 changes: 15 additions & 0 deletions snovault/tests/test_schema_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -349,3 +349,18 @@ def test_schema_utils_resolve_merge_ref_in_real_schema(testapp):
'linked_targets': [
{'target': atid, 'test_description': 'test'}
]}, status=201)

def test_schema_utils_resolve_merge_ref_in_embedded_schema(testapp):
""" Tests that we can resolve a $merge object in our test schema embedded in another schema """
collection_url = '/testing-linked-schema-fields'
atid = testapp.post_json('/testing-link-targets-sno', {'name': 'target'}, status=201).json['@graph'][0]['@id']
atid = testapp.post_json(collection_url, {'quality': 5,
'linked_targets': [
{'target': atid, 'test_description': 'test'}
]}, status=201).json['@graph'][0]['@id']
atid = testapp.post_json('/testing-embedded-linked-schema-fields', {
'link': atid
}, status=201).json['@graph'][0]['@id']
embedded = testapp.get(f'/{atid}?frame=embedded', status=200).json
assert embedded['link']['quality'] == 5
assert embedded['link']['linked_targets'][0]['test_description'] == 'test'
27 changes: 27 additions & 0 deletions snovault/tests/testing_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -874,10 +874,37 @@ class TestingKey(Item):
},
)
class TestingLinkedSchemaField(Item):
""" Tests that we can resolve $merge refs within this repo """
item_type = 'testing_linked_schema_field'
schema = load_schema('snovault:test_schemas/TestingLinkedSchemaField.json')


@collection(
'testing-embedded-linked-schema-fields',
properties={
'title': 'Test Embedded Linked Schema Fields',
'description': 'Testing that we can link fields across schemas.',
},
)
class TestingEmbeddedLinkedSchemaField(Item):
""" Tests that we can embed fields that are $merge refs on other schemas """
item_type = 'testing_embedded_linked_schema_field'
schema = {
'type': 'object',
'properties': {
'link': {
'type': 'string',
'linkTo': 'TestingLinkedSchemaField'
}
}
}
embedded_list = [
'link.linked_targets.*',
'link.quality'
]



@collection('testing-hidden-facets')
class TestingHiddenFacets(Item):
""" Collection designed to test searching with hidden facets. Yes this is large, but this is a complex feature
Expand Down
Loading