Skip to content
This repository has been archived by the owner on Feb 12, 2022. It is now read-only.

Unions of arrays are broken #106

Open
psixdev opened this issue Oct 3, 2018 · 3 comments
Open

Unions of arrays are broken #106

psixdev opened this issue Oct 3, 2018 · 3 comments

Comments

@psixdev
Copy link

psixdev commented Oct 3, 2018

I have the context:

const typesContext = {
	U1: {type: 'integer[] | string[]'},
	U2: {type: 'integer[] | string[] | nil'}
};

And I output expanded and canonical forms for types:

U1

expanded

{
  "type": "array",
  "items": {
    "anyOf": [
      {
        "type": "array",
        "items": {
          "type": "integer"
        }
      },
      {
        "type": "string"
      }
    ],
    "type": "union"
  }
}

canonical

{
  "type": "array",
  "items": {
    "anyOf": [
      {
        "type": "array",
        "items": {
          "type": "integer"
        }
      },
      {
        "type": "string"
      }
    ],
    "type": "union"
  }
}

U2

expanded

{
  "type": "union",
  "anyOf": [
    {
      "type": "array",
      "items": {
        "type": "integer"
      }
    },
    {
      "type": "array",
      "items": {
        "type": "string"
      }
    },
    {
      "type": "nil"
    }
  ]
}

canonical

{
  "type": "union",
  "anyOf": [
    {
      "type": "array",
      "items": {
        "type": "integer"
      }
    },
    {
      "type": "array",
      "items": {
        "type": "string"
      }
    },
    {
      "type": "nil"
    }
  ]
}

As you can see, the array of strings in the first union is turned into a string. It looks like a bug.

@trevorr
Copy link
Contributor

trevorr commented Oct 4, 2018

IIRC, the precedence of the array and union operators is not what you'd expect. Based on your spacing, it's obvious to a human that you want a union of arrays (and possibly nil). However, the type parser doesn't consider spacing and applies different rules. Could you try wrapping your arrays in parentheses and see if that gives the result you expect?

@trevorr
Copy link
Contributor

trevorr commented Oct 4, 2018

BTW, the code for that is here. As you can probably see, it's an ad hoc parser, and it doesn't follow consistent precedence rules. Unfortunately, AFAIK the RAML spec does not provide a grammar or precedences rules for type expressions.

@psixdev
Copy link
Author

psixdev commented Oct 5, 2018

If I try to use type (integer[]) | (string[]) the tools.expandedForm throws an error:

Error: could not resolve: integer[])
    at expandForm (/node_modules/datatype-expansion/src/expanded.js:134:11)
    at form.anyOf.form.anyOf.map.elem (/node_modules/datatype-expansion/src/expanded.js:226:39)
    at Array.map (<anonymous>)
    at expandUnion (/node_modules/datatype-expansion/src/expanded.js:226:27)
    at expandForm (/node_modules/datatype-expansion/src/expanded.js:104:14)
    at expandForm (/node_modules/datatype-expansion/src/expanded.js:96:16)
    at expandForm (/node_modules/datatype-expansion/src/expanded.js:165:36)
    at Object.expandedForm (/node_modules/datatype-expansion/src/expanded.js:33:12)

Is this the library bug?

But I can use type ((integer[]) | (string[])) and get the result I expect, thank you.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants