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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]

### Fixed

- model: several value list annotations on the same property no longer mark the schema invalid; the first one is kept and the rest are logged as ignored (#142)

## [1.12.2]

Expand Down
7 changes: 7 additions & 0 deletions pyodata/v2/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -1439,6 +1439,13 @@ def from_etree(schema_nodes, config: Config):
raise RuntimeError(f'Target Property {annotation.proprty_name} '
f'of {vh_type} as defined in {annotation} does not exist')

if target_proprty.value_helper is not None:
# OData allows several annotations of the same term on one target;
# PyOData keeps only one value helper per property, so keep the first.
modlog().warning('Ignoring additional value list %s, %s already has %s',
annotation, target_proprty, target_proprty.value_helper)
continue

annotation.proprty = target_proprty
target_proprty.value_helper = annotation
except (RuntimeError, PyODataModelError) as ex:
Expand Down
47 changes: 47 additions & 0 deletions tests/test_model_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -1751,3 +1751,50 @@ def test_invalid_xml(xml_builder_factory):
with pytest.raises(PyODataParserError) as e_info:
MetadataBuilder(xml).build()
assert str(e_info.value) == 'Metadata document syntax error'


@patch('logging.Logger.warning')
def test_annot_v_l_duplicate_target(mock_warning, xml_builder_factory):
"""Several value list annotations of one property must not invalidate the schema"""

vl = """
<Annotations xmlns="http://docs.oasis-open.org/odata/ns/edm" Target="DUP_VL.Dict/Value">
<Annotation Term="com.sap.vocabularies.Common.v1.ValueList">
<Record>
<PropertyValue Property="Label" String="{label}"/>
<PropertyValue Property="CollectionPath" String="DataValueHelp"/>
<PropertyValue Property="SearchSupported" Bool="true"/>
<PropertyValue Property="Parameters">
<Collection>
<Record Type="com.sap.vocabularies.Common.v1.ValueListParameterOut">
<PropertyValue Property="LocalDataProperty" PropertyPath="Value"/>
<PropertyValue Property="ValueListProperty" String="Value"/>
</Record>
</Collection>
</PropertyValue>
</Record>
</Annotation>
</Annotations>
"""

xml_builder = xml_builder_factory()
xml_builder.add_schema(
'DUP_VL',
"""
<EntityType Name="Dict" sap:content-version="1">
<Key><PropertyRef Name="Key"/></Key>
<Property Name="Key" Type="Edm.String" Nullable="false"/>
<Property Name="Value" Type="Edm.String" Nullable="false"/>
</EntityType>
<EntityContainer Name="EXAMPLE_SRV" m:IsDefaultEntityContainer="true">
<EntitySet Name="DataValueHelp" EntityType="DUP_VL.Dict"/>
</EntityContainer>
""" + vl.format(label='First') + vl.format(label='Second')
)

schema = MetadataBuilder(xml_builder.serialize()).build()

assert schema.is_valid
value_helper = schema.entity_type('Dict').proprty('Value').value_helper
assert value_helper.label == 'First'
assert any('Ignoring additional value list' in str(call.args[0]) for call in mock_warning.call_args_list)