Skip to content

flatten-polymorphism

Id
@azure-tools/typespec-client-generator-core/flatten-polymorphism

Severity: error

This diagnostic is issued when @flattenProperty is applied to a model property whose type is polymorphic (a discriminated type). Flattening a polymorphic type is not supported, because most languages cannot represent it.

  • Area: SDK model flattening. Blocks applying @flattenProperty because flattening a discriminated model would break generated polymorphic model structure.
  • Not affected: The discriminator and inheritance described by the service model remain unchanged.
@discriminator("kind")
model Pet {
kind: string;
}
model Cat extends Pet {
kind: "cat";
}
model Owner {
@flattenProperty // `pet` has polymorphic type `Pet`
pet: Pet;
}

TCGC reports:

Cannot flatten property of polymorphic type.

Remove @flattenProperty from the polymorphic property, or change the property to a non-polymorphic type.

@discriminator("kind")
model Pet {
kind: string;
}
model Cat extends Pet {
kind: "cat";
}
model Owner {
pet: Pet;
}