Skip to content

Support avsc-compatible wrapped unions #43

Description

@ssonne

avro-typescript can be used to generate types mostly compatible with avsc. With unions, however, avsc often requires wrapped unions when there is ambiguity between the different union options. A wrapped union is an object with a single key, which contains the type name, and a single value, the actual value. Here's a comparison between unwrapped and wrapped unions:

  • Unwrapped: unionEnum: null | string | numberEnumType;
  • Wrapped: unionEnum: { "null": null } | { "string": string } | { "numberEnumType": numberEnumType };

This pattern allows avsc to disambiguate between values that are similar, such as numbers that might be int or double. In JS they are just number, so avsc can't tell the difference when faced with such values at runtime. This problem applies to records as well; in theory different record types can often be disambiguated, but a general algorithm is complicated (see this comment) and avsc hasn't implemented it. avsc docs on wrapEnums are here.

avro-typescript generates only unwrapped unions, but when the schema has ambiguous unions, avsc requires wrapped unions and the types don't match. At least one other user has encountered this problem as well; see this issue.

Proposal: add a new option wrapEnums which would cause avro-typescript to generate wrapped union types.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions