Skip to content

Commit

Permalink
Reject unknown additional fields
Browse files Browse the repository at this point in the history
  • Loading branch information
otto-ifak committed Nov 5, 2024
1 parent 08b832b commit c9c1f56
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 13 deletions.
4 changes: 2 additions & 2 deletions aas_test_engines/test_cases/v3_0/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ def __init_subclass__(cls, max_len_text):
def check_language(self):
if not is_bcp_lang_string(self.language):
raise CheckConstraintException("Property 'language' does not contain a language")
if len(self.language) <= 1:
if len(self.language) < 1:
raise CheckConstraintException("property 'language' must not be empty")

def check_text(self):
if len(self.text) <= 1:
if len(self.text) < 1:
raise CheckConstraintException("Property 'text' must not be empty")
if len(self.text) > self._max_len_text:
raise CheckConstraintException(f"Property 'text' is too long ({len(self.text)} > {self._max_len_text}")
Expand Down
36 changes: 25 additions & 11 deletions aas_test_engines/test_cases/v3_0/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def __eq__(self, other: "StringFormattedValue") -> bool:
return self.raw_value == other.raw_value

def __str__(self) -> str:
return f"*{self.raw_value}"
return self.raw_value


def parse_string_formatted_value(cls, value: Adapter, result: AasTestResult) -> StringFormattedValue:
Expand Down Expand Up @@ -166,17 +166,27 @@ def parse_concrete_object(cls, adapter: Adapter, result: AasTestResult):
result.append(AasTestResult(f"Model typ missing @ {adapter.path}", level=Level.ERROR))

args = {}
all_fields = set()
for field in fields(cls):
field_name = to_lower_camel_case(field.name)
all_fields.add(field_name)
required, field_type = unwrap_optional(field.type)
try:
obj_value = obj[field_name]
except KeyError:
if required:
result.append(AasTestResult(f"Missing attribute {field_name}", level=Level.ERROR))
args[field.name] = None
result.append(AasTestResult(f"Missing attribute {field_name} @ {adapter.path}", level=Level.ERROR))
args[field.name] = INVALID
else:
args[field.name] = None
continue
args[field.name] = parse(field_type, obj_value, result)

# Check unknown additional attributes
for key in obj.keys():
if key not in all_fields:
result.append(AasTestResult(f"Unknown additional attribute {key} @ {adapter.path}", level=Level.ERROR))

return cls(**args)


Expand Down Expand Up @@ -210,11 +220,13 @@ def parse(cls, obj_value: Adapter, result: AasTestResult):
return parse_enum(cls, obj_value, result)
elif isinstance(cls, StringFormattedValue.__class__):
return parse_string_formatted_value(cls, obj_value, result)
print(origin)
print(getattr(cls, '__args__', None))
print(obj_value)
print(cls)
raise NotImplementedError()
raise NotImplementedError(
f"There is no parsing implemented for:\n"
f" origin: {origin}\n"
f" args: {getattr(cls, '__args__', None)}\n"
f" obj_value: {obj_value}\n"
f" cls: {cls}\n"
)


def check_constraints(obj, result: AasTestResult, path: AdapterPath = AdapterPath()):
Expand Down Expand Up @@ -246,8 +258,10 @@ def _parse_and_check(cls, adapter: Adapter) -> Tuple[object, AasTestResult]:
result_root.append(result_constraints)
return result_root, env

def parse_and_check_json(cls, value: any) -> Tuple[object, AasTestResult]:

def parse_and_check_json(cls, value: any) -> Tuple[AasTestResult, object]:
return _parse_and_check(cls, JsonAdapter(value, AdapterPath()))

def parse_and_check_xml(cls, value: any) -> Tuple[object, AasTestResult]:
return _parse_and_check(cls, XmlAdapter(value, AdapterPath()))

def parse_and_check_xml(cls, value: any) -> Tuple[AasTestResult, object]:
return _parse_and_check(cls, XmlAdapter(value, AdapterPath()))

0 comments on commit c9c1f56

Please sign in to comment.