Polymorphism on types


#1

Hi guys,

I am working on an API specs, whose response is an array of objects.
Each object exposes three fields:

  • field1
  • field2
  • field3

they are all string, but field3 should contain either typeA or typeB.

I have tried:
properties:
field3:
type: typeA | typeB

but apparently it doesnt work / it is not supported.

The API spec is RAML 1.0 compliant.

Any suggestion ?

Thanks in advance,
Cheers,
Gian.


#2

@gianluca: what do you mean by “it doesnt work / it is not supported”? are you referring to a specific tool?

For reference, this is where to find the section on “Union Types” in the RAML 1.0 docs: https://github.com/raml-org/raml-spec/blob/master/versions/raml-10/raml-10.md#union-type


#3

The API spec is implemented in a RAML 1.0 file.
It defines a GET method “/components”.
The /components method returns a JSON response.
The response is an array of objects of type “Component”, which is defined in a separate RAML 1.0 DataType file.
“Component” contains a “Data” field, which could be either TYPE_A or TYPE_B

{
data: { }
}

It looks like if I am not allowed to define Union Types inside a DataType file.

The API response is defined in this way:
responses:
200:
body:
application/json:
type: !include types/component.raml

uses:
marketingBanner: !include marketingBanners/marketingBanner.raml
contextualMessage: !include contextualMessages/contextualMessage.raml
union: marketingBanner | contextualMessage

And component is defined in a separate RAML 1.0 DataType file as showed below:

description: Represent a Component, and relative metadata.
properties:
data:
type: string
schema: type_a | type_b

This works but it doesnt produce the result I wish.
I have also tried to define a union type on the field type, but it gives me an error.

Any suggestion?

cheers,
Gian.


#4

Hi @gianluca. I don’t see problems with your definition of the union type, but I wonder what tool you are using that gives you a different intention. Can you clarify precisely what you are using and the error that you see in that tool?


#5

Hi @christian_vogel,

I am using the API Designer in Anypoint Mulesoft.
I don’t think the issue is with the union type at all.
As far as I can see the issue is with the “types:” snippet.
When present, the API Designer in Mulesoft renders a blank box where the method definition normally appears. In the console it is possible to see some Javascript error, not meaningful to me honestly.

Any help is much appreciated.

Cheers,
Gian.


#6

I agree - its probably a problem with the following component https://github.com/mulesoft/api-console. May I asked to raise an issue with a clear description, a RAML, and an image of the problem?