-
Notifications
You must be signed in to change notification settings - Fork 202
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add discriminator
tag to generate if-then
schemas
#1376
Conversation
This tag allows for generating if-then, in favor of anyOf clauses.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for the pull request. I am honestly still a bit suspicious of the utility of this feature but it looks like the code changes are localized so I'm okay with accepting it.
Can you add a test to cover the remaining lines in this patch that are not covered yet? Also, please add an issue for the bug you found.
@@ -15,11 +17,51 @@ export class UnionTypeFormatter implements SubTypeFormatter { | |||
return type instanceof UnionType; | |||
} | |||
public getDefinition(type: UnionType): Definition { | |||
// FIXME: Filtering never types from union types is wrong. However, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you file an issue for this? A fixme in the code here is too easy to overlook.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was incorrect, filtering never types from unions is valid. However, I thought this statement would be superfluous as never types are filtered upon construction of the union. Apparently, this is not always the case, as the hidden
tests point out.
I have removed the line that wasn't tested as it turned out it was unreachable. The FIXME has also been removed, as it was incorrect. |
🚀 PR was released in |
Fixes #1209
This pr adds the ability to select a common property in a union type to act as a discriminator. This is done by specifying the desired property key using the
discriminator
tag. For an example, see the tagged issue or a supplied test case. Note that each of the constituent types should contain this key. A possible next step would be to allow each of the constituent types to define a separate discriminator. Thediscriminator
tag would then not be applied to the union definition, but to the separate object definitions.