Skip to main content

Polymorphism

· One min read
Shaun Turner
Director @ Head in the Cloud Solutions

I have now added polymorphism capabilities into Malleable Models.

Polymorph - Courtesy of the smega-drive

No, not that polymorph thankfully!

But sometimes your models will need lists of objects with a shared heritage

apiVersion: kadense.io/v1
kind: MalleableModule
metadata:
name: test-polymorph
namespace: default
spec:
classes:
PolymorphicBaseClass:
discriminatorProperty: "type"
properties:
BaseStringProperty:
type: string

PolymorphicDerivedClass1:
baseClass: PolymorphicBaseClass
typeDiscriminator: DerivedString
properties:
DerivedStringProperty:
type: string

PolymorphicDerivedClass2:
baseClass: PolymorphicBaseClass
typeDiscriminator: DerivedInt
properties:
DerivedIntProperty:
type: int

The MalleableAssemblyBuilder will populate the MalleableAssembly that it generates with the configuration for the JsonSerializer. This can then be passed into the MalleablePolymorphicTypeResolver and in turn into the JsonSerializerOptions

Full information on polymorphism in Malleable Models can be found in the documentation.