Implement building types for AnyOf · danger/python@2fc7e90 · GitHub
Skip to content

Commit 2fc7e90

Browse files
committed
Implement building types for AnyOf
1 parent 3051f4d commit 2fc7e90

3 files changed

Lines changed: 166 additions & 1 deletion

File tree

‎danger_python/generator/builder.py‎

Lines changed: 27 additions & 1 deletion

‎tests/generator/test_builder.py‎

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
EnumDefinition,
55
PropertyDefinition,
66
SchemaAllOf,
7+
SchemaAnyOf,
78
SchemaArray,
89
SchemaEnum,
910
SchemaObject,
@@ -429,3 +430,98 @@ def test_type_builder_handles_enumerations_with_uppercase_values():
429430
],
430431
depends_on=set(),
431432
)
433+
434+
435+
def test_type_builder_handles_optional_references():
436+
"""
437+
Test type builder handles optional references.
438+
"""
439+
schema = [
440+
SchemaObject(
441+
name="ObjectA",
442+
properties=[
443+
SchemaAnyOf(
444+
name="refB",
445+
any_of=[
446+
SchemaReference(name="refB", reference="ObjectB"),
447+
SchemaValue(name="refB", value_type="null"),
448+
],
449+
)
450+
],
451+
),
452+
SchemaObject(
453+
name="ObjectB",
454+
properties=[SchemaValue(name="strField", value_type="string")],
455+
),
456+
]
457+
458+
build_result = build_types(schema)
459+
460+
assert len(build_result) == 2
461+
assert build_result[0] == ClassDefinition(
462+
name="ObjectA",
463+
properties=[
464+
PropertyDefinition(
465+
name="ref_b",
466+
key="refB",
467+
value_type="Optional[ObjectB]",
468+
known_type=False,
469+
)
470+
],
471+
depends_on={"ObjectB"},
472+
)
473+
assert build_result[1] == ClassDefinition(
474+
name="ObjectB",
475+
properties=[
476+
PropertyDefinition(
477+
name="str_field", key="strField", value_type="str", known_type=True
478+
)
479+
],
480+
depends_on=set(),
481+
)
482+
483+
484+
def test_type_builder_resolves_union_types_to_any():
485+
"""
486+
Test type builder resolves union types to Any.
487+
"""
488+
schema = [
489+
SchemaObject(
490+
name="ObjectA",
491+
properties=[
492+
SchemaAnyOf(
493+
name="refB",
494+
any_of=[
495+
SchemaReference(name="refB", reference="ObjectB"),
496+
SchemaValue(name="refB", value_type="str"),
497+
],
498+
)
499+
],
500+
),
501+
SchemaObject(
502+
name="ObjectB",
503+
properties=[SchemaValue(name="strField", value_type="string")],
504+
),
505+
]
506+
507+
build_result = build_types(schema)
508+
509+
assert len(build_result) == 2
510+
assert build_result[0] == ClassDefinition(
511+
name="ObjectA",
512+
properties=[
513+
PropertyDefinition(
514+
name="ref_b", key="refB", value_type="Any", known_type=True,
515+
)
516+
],
517+
depends_on=set(),
518+
)
519+
assert build_result[1] == ClassDefinition(
520+
name="ObjectB",
521+
properties=[
522+
PropertyDefinition(
523+
name="str_field", key="strField", value_type="str", known_type=True
524+
)
525+
],
526+
depends_on=set(),
527+
)

‎tests/generator/test_parser.py‎

Lines changed: 43 additions & 0 deletions

0 commit comments

Comments
 (0)