Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.next.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ Thanks, you're awesome :-) -->
#### Improvements

* Streamline RFC process from four stages (Strawperson, Draft, Candidate, Finished) to a single Proposal stage with target maturity. #2600
* Detect type conflicts when adding intermediate fields. #2671

#### Deprecated

Expand Down
8 changes: 7 additions & 1 deletion scripts/schema/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,17 +189,23 @@ def nest_fields(field_array: List[Field]) -> Dict[str, Dict[str, FieldEntry]]:
# Respect explicitly defined object fields
if 'type' in field_details and field_details['type'] in ['object', 'nested']:
field_details.setdefault('intermediate', False)
else:
elif 'type' not in field_details:
field_details.setdefault('type', 'object')
field_details.setdefault('name', '.'.join(parent_fields[:idx + 1]))
field_details.setdefault('intermediate', True)
else:
msg = f"Type conflict detected when adding intermediate field '{level}' (adding: object, existing: {field_details['type']})"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

so this basically triggers when 'type' in field_details but type is not object?

raise ValueError(msg)

# moving the nested_schema cursor deeper
current_path.extend([level])
nested_schema = nested_schema[level]['fields']
nested_schema.setdefault(leaf_field, {})
# Overwrite 'name' with the leaf field's name. The flat_name is already computed.
field['node_name'] = leaf_field
if 'field_details' in nested_schema[leaf_field] and nested_schema[leaf_field]['field_details']['type'] != field['type']:
msg = f"Type conflict detected when adding leaf field '{leaf_field}' (adding: {field['type']}, existing: {nested_schema[leaf_field]['field_details']['type']})"
raise ValueError(msg)
nested_schema[leaf_field]['field_details'] = field
return schema_root

Expand Down
13 changes: 13 additions & 0 deletions scripts/tests/unit/test_schema_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,19 @@ def test_nest_fields(self):
nested_fields = loader.nest_fields(process_fields)
self.assertEqual(nested_fields, expected_nested_fields)

def test_nest_fields_incompatible_types(self):
test_fields = [
{'name': 'foo', 'type': 'keyword'},
{'name': 'foo.bar', 'type': 'keyword'},
]
self.assertRaises(ValueError, loader.nest_fields, test_fields)

test_fields = [
{'name': 'foo.bar', 'type': 'keyword'},
{'name': 'foo', 'type': 'keyword'},
]
self.assertRaises(ValueError, loader.nest_fields, test_fields)

def test_nest_fields_recognizes_explicitly_defined_object_fields(self):
dns_fields = [
{'name': 'question.name', 'type': 'keyword'},
Expand Down
Loading