Skip to content

Commit f9144b3

Browse files
Vasilii Khomutovfilak-sap
Vasilii Khomutov
authored andcommitted
'Nullable' attributes are correctly parsed and reviewed.
Changes: - Consistent way of parsing bool attributes - Supports 'Nullable' attribute for properties (default: True) - Supports 'Nullable' attribute for function import parameters (default: False) Issue: SAP#98
1 parent ea1f1d6 commit f9144b3

File tree

4 files changed

+26
-4
lines changed

4 files changed

+26
-4
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1414

1515
### Fixed
1616
- removed superfluous debug print when parsing FunctionImports from metadata - Jakub Filak
17+
- property 'Nullable' attributes are correctly parsed and respected - Vasilii Khomutov
1718

1819
## [1.4.0]
1920

pyodata/v2/model.py

+11-4
Original file line numberDiff line numberDiff line change
@@ -1668,7 +1668,7 @@ def from_etree(entity_type_property_node):
16681668
return StructTypeProperty(
16691669
entity_type_property_node.get('Name'),
16701670
Types.parse_type_name(entity_type_property_node.get('Type')),
1671-
entity_type_property_node.get('Nullable'),
1671+
attribute_get_bool(entity_type_property_node, 'Nullable', True),
16721672
entity_type_property_node.get('MaxLength'),
16731673
entity_type_property_node.get('Precision'),
16741674
entity_type_property_node.get('Scale'),
@@ -2364,7 +2364,7 @@ def from_etree(function_import_node, config: Config):
23642364
for param in function_import_node.xpath('edm:Parameter', namespaces=config.namespaces):
23652365
param_name = param.get('Name')
23662366
param_type_info = Types.parse_type_name(param.get('Type'))
2367-
param_nullable = param.get('Nullable')
2367+
param_nullable = attribute_get_bool(param, 'Nullable', False)
23682368
param_max_length = param.get('MaxLength')
23692369
param_precision = param.get('Precision')
23702370
param_scale = param.get('Scale')
@@ -2401,8 +2401,7 @@ def sap_attribute_get_string(node, attr):
24012401
return sap_attribute_get(node, attr)
24022402

24032403

2404-
def sap_attribute_get_bool(node, attr, default):
2405-
value = sap_attribute_get(node, attr)
2404+
def str_to_bool(value, attr, default):
24062405
if value is None:
24072406
return default
24082407

@@ -2415,6 +2414,14 @@ def sap_attribute_get_bool(node, attr, default):
24152414
raise TypeError('Not a bool attribute: {0} = {1}'.format(attr, value))
24162415

24172416

2417+
def attribute_get_bool(node, attr, default):
2418+
return str_to_bool(node.get(attr), attr, default)
2419+
2420+
2421+
def sap_attribute_get_bool(node, attr, default):
2422+
return str_to_bool(sap_attribute_get(node, attr), attr, default)
2423+
2424+
24182425
ANNOTATION_NAMESPACES = {
24192426
'edm': 'http://docs.oasis-open.org/odata/ns/edm',
24202427
'edmx': 'http://docs.oasis-open.org/odata/ns/edmx'

tests/metadata.xml

+1
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,7 @@
303303
<Property Name="ID" Type="Edm.Int32" Nullable="false"/>
304304
<Property Name="NameFirst" Type="Edm.String" Nullable="true"/>
305305
<Property Name="NameLast" Type="Edm.String" Nullable="true"/>
306+
<Property Name="NickName" Type="Edm.String"/>
306307
<NavigationProperty Name="Addresses" Relationship="EXAMPLE_SRV_SETS.AssociationEmployeeAddress"
307308
FromRole="EmployeeRole" ToRole="AddressRole"/>
308309
</EntityType>

tests/test_model_v2.py

+13
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,19 @@ def test_edmx(schema):
147147
assert typ_ex_info.value.args[0] == 'Type FooBar does not exist in Schema Namespace EXAMPLE_SRV'
148148

149149

150+
def test_schema_entity_type_nullable(schema):
151+
emp_entity = schema.entity_type('Employee')
152+
153+
id_property = emp_entity.proprty('ID')
154+
assert not id_property.nullable
155+
156+
firstname_property = emp_entity.proprty('NameFirst')
157+
assert firstname_property.nullable
158+
159+
nickname_property = emp_entity.proprty('NickName')
160+
assert nickname_property.nullable
161+
162+
150163
def test_schema_entity_sets(schema):
151164
"""Test Schema methods for EntitySets"""
152165

0 commit comments

Comments
 (0)