Skip to content

Commit b537610

Browse files
authored
Check explicitly for invalid model type in C# (#511)
Please see #510 for more details. This is a fix particularly for C# generation.
1 parent cd92d20 commit b537610

File tree

3 files changed

+757
-20
lines changed

3 files changed

+757
-20
lines changed

aas_core_codegen/csharp/jsonization/_generate.py

+50-2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
INDENT2 as II,
2424
INDENT3 as III,
2525
INDENT4 as IIII,
26+
INDENT5 as IIIII,
2627
)
2728

2829

@@ -381,6 +382,9 @@ def _generate_from_method_for_class(
381382

382383
blocks.append(Stripped(args_init_writer.getvalue()))
383384

385+
if cls.serialization.with_model_type:
386+
blocks.append(Stripped("string? modelType = null;"))
387+
384388
# endregion
385389

386390
# region Switch on property name
@@ -419,11 +423,42 @@ def _generate_from_method_for_class(
419423
return None, errors
420424

421425
if cls.serialization.with_model_type:
426+
model_type = naming.json_model_type(cls.name)
427+
422428
cases.append(
423429
Stripped(
424-
"""\
430+
f"""\
425431
case "modelType":
426-
continue;"""
432+
{I}{{
433+
{II}if (keyValue.Value == null)
434+
{II}{{
435+
{III}error = new Reporting.Error(
436+
{IIII}"Expected a model type, but got null");
437+
{III}return null;
438+
{II}}}
439+
{II}modelType = DeserializeImplementation.StringFrom(
440+
{III}keyValue.Value,
441+
{III}out error);
442+
{II}if (error != null)
443+
{II}{{
444+
{III}error.PrependSegment(
445+
{IIII}new Reporting.NameSegment(
446+
{IIIII}"modelType"));
447+
{III}return null;
448+
{II}}}
449+
450+
{II}if (modelType != "{model_type}")
451+
{II}{{
452+
{III}error = new Reporting.Error(
453+
{IIII}"Expected the model type '{model_type}', " +
454+
{IIII}$"but got {{modelType}}");
455+
{III}error.PrependSegment(
456+
{IIII}new Reporting.NameSegment(
457+
{IIIII}"modelType"));
458+
{III}return null;
459+
{II}}}
460+
{II}break;
461+
{I}}}"""
427462
)
428463
)
429464

@@ -482,6 +517,19 @@ def _generate_from_method_for_class(
482517

483518
blocks.append(Stripped(required_check_writer.getvalue()))
484519

520+
if cls.serialization.with_model_type:
521+
blocks.append(
522+
Stripped(
523+
f"""\
524+
if (modelType == null)
525+
{{
526+
{I}error = new Reporting.Error(
527+
{II}"Required property \\"modelType\\" is missing");
528+
{I}return null;
529+
}}"""
530+
)
531+
)
532+
485533
# endregion
486534

487535
# region Pass in arguments to the constructor

aas_core_codegen/naming.py

+5
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,11 @@ def json_property(identifier: Identifier) -> Identifier:
7979
@ensure(
8080
lambda result: "_" not in result
8181
) # This post-condition avoids naming conflicts with prefixing in the JSON schema.
82+
@ensure(
83+
lambda result: '"' not in result and "'" not in result and '\\' not in result
84+
# This post-condition simplifies a lot of generator code since we can simply in-line
85+
# the model type in strings.
86+
)
8287
# fmt: on
8388
def json_model_type(identifier: Identifier) -> Identifier:
8489
"""

0 commit comments

Comments
 (0)