raml-org / raml-spec

RAML Specification
http://raml.org
3.87k stars 859 forks source link

Do object annotation types support discriminators too? #662

Open katmatt opened 6 years ago

katmatt commented 6 years ago

The specification is unclear about annotation types with type object and their support for discriminators. According to the spec annotation types have the same facets as types, but why should they support discriminators? Annotations always have a specific type and don't support inheritance. That means that it doesn't make sense that they allow to specify discriminators.

sichvoge commented 6 years ago

That is not always true. Consider the following:

types:
  Person:
    type: object
    discriminator: kind # refers to the `kind` property of object `Person`
    properties:
      kind: string # contains name of the kind of a `Person` instance
      name: string
  Employee: # kind can equal `Employee`; default value for `discriminatorValue`
    type: Person
    properties:
      employeeId: integer
  User: # kind can equal `User`; default value for `discriminatorValue`
    type: Person
    properties:
      userId: integer

annotationTypes:
  Roles: Person[] 

This is fully valid. Yes, defining an inline annotation type it does not make sense, but it makes more sense through the ability to reuse types as annotation types.

Hope that gives you what you need :)

katmatt commented 6 years ago

Now I"m confused. I thought that annotation types don't support inheritance?

sichvoge commented 6 years ago

Only through the RAML types system. You can't create one annotation type and use it as a type in another. That is not possible.

katmatt commented 6 years ago

If I get you correctly this allows sth. like:

/example:
  (Roles):
    - kind: Employee
      name: Jane Doe
      employeeId: 1
    - kind: User
      name: John Doe
      userId: 1

Right?

sichvoge commented 6 years ago

Yes. An annotation type can inherit its properties from a type definition. That is why you can use anything defined in types in as a base, but not something defined in annotationTypes.

katmatt commented 6 years ago

I see. Coming back to my initial question: Do you have another example showing an annotation type that defines a discriminator or at least a discriminatorValue? I'm still not convinced that it makes sense to have them for annotation types too 😉