diff --git a/src/main/java/com/regnosys/rosetta/generator/python/object/PythonChoiceDispatchProcessor.java b/src/main/java/com/regnosys/rosetta/generator/python/object/PythonChoiceDispatchProcessor.java index 0973bf5e..5c7079ec 100644 --- a/src/main/java/com/regnosys/rosetta/generator/python/object/PythonChoiceDispatchProcessor.java +++ b/src/main/java/com/regnosys/rosetta/generator/python/object/PythonChoiceDispatchProcessor.java @@ -15,6 +15,7 @@ import com.regnosys.rosetta.types.RChoiceOption; import com.regnosys.rosetta.types.RChoiceType; import com.regnosys.rosetta.types.RDataType; +import com.regnosys.rosetta.types.REnumType; import com.regnosys.rosetta.types.RObjectFactory; import com.regnosys.rosetta.types.RType; import com.regnosys.rosetta.types.TypeSystem; @@ -29,9 +30,9 @@ * leaf, not the option of the choice that holds it. The table maps each {@code @type} the choice * can be read with to the Python attribute name of that option: * @@ -110,7 +111,7 @@ private void collectOptionKeys(RChoiceOption option, Set keys) { } private static String key(RType optionType, String optionName) { - return optionType instanceof RDataType + return optionType instanceof RDataType || optionType instanceof REnumType ? fullyQualifiedName(optionType) : optionName; } diff --git a/src/test/java/com/regnosys/rosetta/generator/python/object/PythonChoiceDispatchTest.java b/src/test/java/com/regnosys/rosetta/generator/python/object/PythonChoiceDispatchTest.java index daa09e5d..a0eb219a 100644 --- a/src/test/java/com/regnosys/rosetta/generator/python/object/PythonChoiceDispatchTest.java +++ b/src/test/java/com/regnosys/rosetta/generator/python/object/PythonChoiceDispatchTest.java @@ -22,9 +22,9 @@ /** * Every choice class gets a {@code _CHOICE_DISPATCH} table mapping each {@code @type} it can be - * read with to the attribute name of the option that holds it (#333). A data type is keyed by its - * Rune FQN, a basic type, enum or type alias of either by the option name, and a nested choice - * contributes its leaves. An intermediate choice also contributes its own FQN when the option + * read with to the attribute name of the option that holds it (#333). Data types and enums use + * their Rune FQN, basic types use the option name, and aliases follow their underlying type. + * A nested choice contributes its leaves. An intermediate choice also contributes its own FQN when the option * has metadata, because Java writes that choice's metadata/reference wrapper with its own type. */ @ExtendWith(InjectionExtension.class) @@ -133,7 +133,7 @@ public void testThreeLevelChoice() { } @Test - public void testBasicTypeAndEnumOptionsAreKeyedByOptionName() { + public void testDataAndEnumOptionsUseFullyQualifiedNames() { String model = """ enum CurrencyEnum: USD @@ -151,13 +151,85 @@ value string (1..1) generated(model, "rosetta/test/model/Pick.py"), MARKER + """ _CHOICE_DISPATCH: ClassVar[dict[str, str]] = { - "CurrencyEnum": "CurrencyEnum", "com.rosetta.test.model.A": "A", + "com.rosetta.test.model.CurrencyEnum": "CurrencyEnum", "string": "string", } """); } + @Test + public void testMetadataDoesNotChangeBasicAndEnumDiscriminatorKeys() { + String model = """ + enum CurrencyEnum: + USD + EUR + + choice Pick: + CurrencyEnum + [metadata scheme] + string + [metadata scheme] + """; + testUtils.assertGeneratedContainsExpectedString( + generated(model, "rosetta/test/model/Pick.py"), + MARKER + """ + _CHOICE_DISPATCH: ClassVar[dict[str, str]] = { + "com.rosetta.test.model.CurrencyEnum": "CurrencyEnum", + "string": "string", + } + """); + } + + @Test + public void testEnumAliasesUseUnderlyingEnumFullyQualifiedName() { + String model = """ + enum CurrencyEnum: + USD + EUR + + typeAlias Money: CurrencyEnum + typeAlias AliasOfMoney: Money + + choice Pick: + AliasOfMoney + string + """; + testUtils.assertGeneratedContainsExpectedString( + generated(model, "rosetta/test/model/Pick.py"), + MARKER + """ + _CHOICE_DISPATCH: ClassVar[dict[str, str]] = { + "com.rosetta.test.model.CurrencyEnum": "AliasOfMoney", + "string": "string", + } + """); + } + + @Test + public void testNestedEnumDiscriminatorsUseFullyQualifiedNames() { + String model = """ + enum CurrencyEnum: + USD + EUR + + choice Inner: + CurrencyEnum + string + choice Outer: + Inner + int + """; + testUtils.assertGeneratedContainsExpectedString( + generated(model, "rosetta/test/model/Outer.py"), + MARKER + """ + _CHOICE_DISPATCH: ClassVar[dict[str, str]] = { + "com.rosetta.test.model.CurrencyEnum": "Inner", + "int": "int", + "string": "Inner", + } + """); + } + @Test public void testPlainNestedChoicesDoNotExposeIntermediateDiscriminators() { String outer = generated(NESTED_MODEL, "rosetta/test/model/OuterChoice.py");