apicollective / apibuilder

Simple, Comprehensive Tooling for Modern APIs
https://www.apibuilder.io/
MIT License
562 stars 82 forks source link

Unions generation: The operator '[]' isn't defined for the class 'int' #894

Open supervital opened 1 year ago

supervital commented 1 year ago

Hello,

I have the next union:

"unions": {
    "myValue": {
      "types": [
        {
          "type": "integer",
          "description": "Simple integer value."
        },
        {
          "type": "double",
          "description": "Simple double value."
        },
        {
          "type": "someCustomValue",
          "description": "A custom value."
        },
        {
          "type": "someCustomValue2",
          "description": "A custom value."
        }
      ]
    }
  }

And while compiling I get the next error:

Error: The operator '[]' isn't defined for the class 'int'. [ +6 ms] Try correcting the operator to an existing operator, or defining a '[]' operator. [ ] factory.first((json['integer'] as int)['value']));

A part of the generated code (dart):

switch (discriminator) {
      case 'integer':
        return blaBlaMyValue._(
            factory.first((json['integer'] as int)['value']));
      case 'double':
        return blaBlaMyValue._(
            factory.second((json['double'] as double)['value']));
      case 'pressureObservationValue':

When I manually remove ['value'] compilation is successful.

What am I doing wrong? Thanks.

mbryzek commented 1 year ago

is it possible for you to add a discriminator field on your union type?

"unions": {
    "discriminator": "name of field you want",
    "myValue": {
      "types": [
      ]
    }
  }

and then try again? I'm not familiar with the dart generator so may need to contact the author of that code generator directly

supervital commented 1 year ago

is it possible for you to add a discriminator field on your union type?

"unions": {
    "discriminator": "name of field you want",
    "myValue": {
      "types": [
      ]
    }
  }

and then try again? I'm not familiar with the dart generator so may need to contact the author of that code generator directly

Thanks for the reply. I have just tried and it has no effect. And "discriminator" of the union, like this:

"unions": {
    "myValue": {
    "discriminator": "name of field you want",
      "types": [
      ]
    }
  }
supervital commented 1 year ago

Looks like the dart generator doesn't work with primitive types in union. So, as a fix, I created a wrapper object for the primitive types:

{
   "unions":{
      "myValue":{
         "types":[
            {
               "type":"integerValue",
               "description":"Simple integer value."
            },
            {
               "type":"doubleValue",
               "description":"Simple double value."
            },
            {
               "type":"someCustomValue",
               "description":"A custom value."
            },
            {
               "type":"someCustomValue2",
               "description":"A custom value."
            }
         ]
      },
      "integerValue":{
         "description":"Object containing an integer.",
         "fields":[
            {
               "name":"value",
               "type":"integer"
            }
         ]
      },
      "doubleValue":{
         "description":"Object containing a double.",
         "fields":[
            {
               "name":"value",
               "type":"double"
            }
         ]
      }
   }
}
mbryzek commented 1 year ago

thanks for sharing!