Optional discrimination



I have type definitions similar to the following:

  discriminator: type
      enum: [one, two, three]
    fitsForAll: string

  type: OneTwo
  discriminatorValue: three
    onlyForThree: boolean

The idea is that OneTwo is suitable for types of ‘one’ and ‘two’, only ‘three’ requires a specialization.

Then I added the following example in a response body:

  "type": "one",
  "fitsForAll": "foo"

Now the API Workbench complains:

None of the ‘OneTwo’ type known subtypes declare ‘one’ as value of discriminating property ‘type’.

Am I really required to define subtypes for all variants, even though the base type is sufficient?

As far as I can see I have the following options:

  • Add “empty” subtype definitions
  • Merge “onlyForThree” into OneTwo and make it optional
  • Keep OneTwo and Three separate, but remove the type discrimination. From a technical POV it’s similar to option #2, but semantically different.

I’m wondering if there is a better way to achieve what I want?



Hi, @tamm0r

the idea of discriminators in RAML assumes that each type has an unique value of some discriminator property (lets say it is type tag), It is assumed that it is always one to one mapping of type tag to type, so there is no way to associate several values of discriminator property with one type.

As for me optimal solutions depends from if you actually need to use polymorphism in your API definition. If answer is yes, then you may consider,

  • add empty subtype definitions
  • introduce another property as discriminator