fix: correctly handled mapped union types#2490
Conversation
There was a problem hiding this comment.
@domoritz is this change correct here? I don't have vega-lite context
There was a problem hiding this comment.
I don't have vega-lite context either, but I took a look at the source code that was used to generate the types and the new schema is correct. It's correctly accounting for unions using anyOf instead of merging them into an object schema. Any validations that were previously passing should still pass with the new schema
There was a problem hiding this comment.
Let's confirm. Please clone https://github.com/vega/vega-lite, link to this version of the schema generator, generate the schema, and run npm run test. If everything still passes, we are good. Could you do that to confirm that we don't have a regression in Vega-Lite?
There was a problem hiding this comment.
We previously simplified the type instead of keeping the anyOf format. Should we mark this as a breaking change?
There was a problem hiding this comment.
Yeah, it may be considered a breaking change, but I really don't see the gain in simplifying these Unions. I agree that allOf can be simplified into a parent merged schema, but anyOf and oneOf are good ways of representing unions (and, in my case, working with discriminated unions and validating via ajv, they're absolutely necessary).
I don't think this is a breaking change, though. It is in the sense that it changes the generated schemas by fixing them (they were wrong before), but the schemas are more correct now and if anything was previously validating but stops validating due to this change, the validation BEFORE this fix was wrong.
There was a problem hiding this comment.
I agree it's a fix. Let's still make it a minor version bump, not just a patch? We might need to mark this pull request as feat: to get that behavior.
There was a problem hiding this comment.
I would really like to keep the simplifications where possible. Makes schemas much more readable.
b887cca to
fd1881e
Compare
The annotations from referenced types were not always preserved. Mainly, in `preserveAnnotation`, `isAssignableTo` and `inferMap` function flows were wrongly stripping them.
Mapped types may take Unions as "parameters", and, in that case, they must distribute over each Union individually. This was wrong before, it was creating an intersection. This implementation handles discriminated unions as well and preserves the potential `@discriminator` annotation. Non-OpenAPI `discriminator` were not tested (they generate `if`/`then` schemas). Closes vega#2106
This formatter was mutating the original type by deleting the `discriminator` annotation. If the type was shared/referenced in multiple places, this would result in the annotation being lost.
This allows for projects requiring the package from GitHub to build them directly.
fd1881e to
5a6affd
Compare
domoritz
left a comment
There was a problem hiding this comment.
Left a few comments. Looks good overall.
There was a problem hiding this comment.
Let's confirm. Please clone https://github.com/vega/vega-lite, link to this version of the schema generator, generate the schema, and run npm run test. If everything still passes, we are good. Could you do that to confirm that we don't have a regression in Vega-Lite?
| const annotations = type.getAnnotations(); | ||
| // Copy annotations to avoid mutating the original object, which may be shared | ||
| // with other AnnotatedType instances (e.g., via preserveAnnotation). | ||
| let restAnnotations = annotations; |
There was a problem hiding this comment.
This doesn't actually make a copy. You could also still use annotations. The comment about not mutating is for like 66 now, I guess?
| insideTypes: Set<BaseType> = new Set(), | ||
| ): boolean { | ||
| // Keep original source for infer map so annotations (e.g. @discriminator) are preserved | ||
| const originalSource = source; |
There was a problem hiding this comment.
I agree it's a fix. Let's still make it a minor version bump, not just a patch? We might need to mark this pull request as feat: to get that behavior.
There was a problem hiding this comment.
I would really like to keep the simplifications where possible. Makes schemas much more readable.
|
@AmadeusK525 do you still need the fixes on this PR? |
|
@arthurfiorette Hey, yes I do. Sorry, a lot of stuff came up in life and I couldn't get back to this. I'll ask someone else (who is using the fork because of this feature) to try and handle this for me. Thanks for reminding me. |
Mapped types may take Unions as "parameters", and, in that case, they
must distribute over each Union individually. This was wrong before, it
was creating an intersection.
This implementation handles discriminated unions as well and preserves
the potential
@discriminatorannotation. Non-OpenAPIdiscriminatorwere not tested (they generate
if/thenschemas).Closes #2106