Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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:
* <ul>
* <li>a data type is keyed by its Rune fully qualified name, as {@code _fqrtn()} returns it;</li>
* <li>a basic type, an enum, or a type alias of either is keyed by the option name, which is what
* the serializer writes as {@code @type};</li>
* <li>a data type or enum is keyed by its Rune fully qualified name;</li>
* <li>a basic type is keyed by the option name;</li>
* <li>type aliases use the key of their underlying type, retaining the alias option name for basic types;</li>
* <li>a nested choice contributes its leaves; its own FQN is included only when the option has
* metadata, because Java writes that option's metadata/reference wrapper with its own type.</li>
* </ul>
Expand Down Expand Up @@ -110,7 +111,7 @@ private void collectOptionKeys(RChoiceOption option, Set<String> keys) {
}

private static String key(RType optionType, String optionName) {
return optionType instanceof RDataType
return optionType instanceof RDataType || optionType instanceof REnumType
? fullyQualifiedName(optionType)
: optionName;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -133,7 +133,7 @@ public void testThreeLevelChoice() {
}

@Test
public void testBasicTypeAndEnumOptionsAreKeyedByOptionName() {
public void testDataAndEnumOptionsUseFullyQualifiedNames() {
String model = """
enum CurrencyEnum:
USD
Expand All @@ -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");
Expand Down
Loading