Skip to content
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

Improve declaration of untagged unions to allow for generator optimizations #2548

Merged
merged 6 commits into from
Jun 28, 2024

Conversation

flobernd
Copy link
Member

@flobernd flobernd commented May 8, 2024

Problem

RangeQuery is currently declared as:

/** @codegen_names date, number, terms */
// Note: deserialization depends on value types
export type RangeQuery = DateRangeQuery | NumberRangeQuery | TermsRangeQuery

This declaration is highly problematic for statically typed clients as there is no way to correctly deserialize into the correct union variant, because for example the following variants share the same underlying JSON type:

export class TermsRangeQuery extends RangeQueryBase {
  gt?: string
export class DateRangeQuery extends RangeQueryBase {
  gt?: DateMath // string

There are more types for which the same issue applies, for example:

/** @codegen_names date, numeric, geo */
// Note: deserialization depends on value types
export type DecayFunction =
  | DateDecayFunction
  | NumericDecayFunction
  | GeoDecayFunction
/** @codegen_names geo, date */
// Note: deserialization depends on value types
export type DistanceFeatureQuery =
  | GeoDistanceFeatureQuery
  | DateDistanceFeatureQuery

Proposal

Rearrange and mark these kind of untagged unions, that only differ by the type of some fields, in a specific way so that code generators can drive specialized generation to allow for asymmetric (de-)serialization:

export class MyTypeBase<T1, T2, ...> { ... }

export class MyTypeSpecialized1 extends MyTypeBase<int> {}
export class MyTypeSpecialized2 extends MyTypeBase<string> {}
export class MyTypeSpecialized3 extends MyTypeBase<bool> {}

/**
 * @codegen_names 1, 2, 3 
 * @variant untagged
 */
// Note: deserialization depends on value types
export type MyType = MyTypeSpecialized1 | MyTypeSpecialized2 | MyTypeSpecialized3 

In .NET world, the resulting class design could look like this:

// Untyped/any variant always used for deserialization; can optionally be used by the user when building queries
public class RangeQuery
{
    public object? Gt { get; set; }
}

public abstract class GenericRangeQuery<T> : RangeQuery
{
    public new T? Gt { get; set; }
}

// Specialized range query for numbers
public sealed class NumberRangeQuery : GenericRangeQuery<double> { }

// Specialized range query for dates
public sealed class DateRangeQuery : GenericRangeQuery<DateMath> { }

Motivation and initial discussion: #2545

@flobernd flobernd marked this pull request as ready for review June 12, 2024 11:00
@flobernd flobernd requested review from swallez, technige and a team June 12, 2024 11:00
@flobernd flobernd force-pushed the improve-untagged-unions branch 3 times, most recently from 45845e3 to 636ff57 Compare June 12, 2024 12:59
Copy link
Member

@swallez swallez left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a brief description of this new variant type in the docs?

And mentioning explicitly that this is used for legacy types and should generally avoided as it results in less optimal code generation in client libraries.

@flobernd flobernd requested a review from swallez June 13, 2024 08:05
@flobernd
Copy link
Member Author

Done.

@l-trotta
Copy link
Contributor

@swallez added a commit to fix openapi generation (also enhanced error logging for the deserialization of the schema.json, thanks @Anaethelion)

@flobernd flobernd force-pushed the improve-untagged-unions branch 4 times, most recently from 3f8a336 to 1828fe2 Compare June 19, 2024 07:00
@flobernd
Copy link
Member Author

Spec Meeting 2024/06/27

  1. Explicitly name the "deserialization"/untyped variant in the decorator
  2. Add a strong validation to disallow adding new "untagged" variants
  3. Check flattening of generics

@flobernd flobernd force-pushed the improve-untagged-unions branch 3 times, most recently from 734db8d to 3456d83 Compare June 28, 2024 09:42
Copy link
Contributor

@Anaethelion Anaethelion left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@flobernd flobernd merged commit 3f5c846 into main Jun 28, 2024
8 checks passed
@flobernd flobernd deleted the improve-untagged-unions branch June 28, 2024 11:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants