improve UnionProperty behavior for anyOf/oneOf, lists of types, and nullable enums #1121
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes #1120.
There are two related changes here:
First, when processing the anyOf/oneOf items in a union, check whether each item will generate a named Python class (i.e. is it an enum or a model). If exactly one of them will, then we should not use a synthetic name like
xyz_type_1; instead just use the original namexyz. (If more than one of them will, then we do need to add name suffixes; if none of them will, then it doesn't really matter since none of the synthetic names will show up in Python code anyway).That fixes the case described in the issue where nullable object/enum classes got an unnecessary "Type1" suffix. It's a breaking change, but, I think, a desirable one— I don't think anyone prefers to have "Type1" added in those cases, and depending on that would be a bad idea anyway since it's an obscure implementation detail. So I haven't gated it behind a config option.
Second, if we're creating a union and
typehas been explicitly specified— these were the cases where a bunch of spurious -Type1, -Type2Type1, etc. were being created, because of faulty logic that assumed every explicit type had to be added separately to the union. I believe the correct behavior is:types, then go ahead and add a Property for each one.typeortypes, but there is alsoanyOforoneOf, then we don't need to add anything, because the schemas in theanyOf/oneOflist already describe what kind of values there can be. (We could add a check to make sure the types aren't contradictory— for instance, iftypeis["string", "number"]but theanyOfvariants are objects— but that's out of scope here.)The second issue was also the reason for the weird behavior of
nullablewith enums in 3.0, because we are handlingnullableby converting the type into a union.