Forum Discussion

stuartreynolds's avatar
stuartreynolds
Occasional Visitor
4 months ago

Use anyOf/oneOf with codegen?

Suppose I have a schema:

params: is one of Message|Image|Audio
_type: the type to expect in params

I (currently) have the schema...

...
"components": {
  "schemas: {
   "Interaction": {
      "type": "object",
      "required": [ "_type", "params" ],
     "properties": {
        "_type": {"type": "string"},
         "params": {
             "anyOf": [   // or oneOf ?? 
                { "$ref": "#/components/schemas/Message" },
                { "$ref": "#/components/schemas/Image" },
                { "$ref": "#/components/schemas/Audio" },
             ],
             "discriminator": "_type"
         }
    }
}

What I would hope would be that the codegen code produced would represent a class such as

# dart
class Interaction{
  _type: str;
  params: dynamic;  // Message, Image or Audio
   ...
}

 

Instead I get from codegen

class InteractionData {
   String method;
   InteractionDataParams params;
}

class InteractionDataParams {
   // The union of the attributes of Message, Image or Audio
};

which, among other things, seems likely will run into collisions with differently typed attributes.

Is there a sensible solution to this when I could expect codegen to directly produce Message/Image/ Audio objects in the params field?

No RepliesBe the first to reply