shuttle-hq / synth

The Declarative Data Generator
https://www.getsynth.com/
Apache License 2.0
1.37k stars 108 forks source link

Using a `u32` constant as an array length causes an error while a `u32` range does not #298

Closed bmoxb closed 2 years ago

bmoxb commented 2 years ago

Describe the bug I've noticed an issue with "length" types of "array" generators - u32 constants cause an error while 'range' does not. I think we should either disallow all 32-bit types to be used as a "length", or we should start allowing 32-bit constants in addition to ranges.

To Reproduce Steps to reproduce the behavior:

  1. Schema:
    {
    "type": "array",
    "length": {
    "type": "number",
    "subtype": "u32",
    "constant": 1
    },
    "content": {
      "type": "string",
      "pattern": "hello world"
    }
    }
  2. See error:
    
    Error: Unable to open the namespace "temp"

Caused by: 0: at file temp/a.json 1: Failed to parse collection 2: cannot use number::U32::Constant as an array length at line 11 column 1

3. And yet, all works expected (no error, `["hello world"]` is generated) when the schema is modified such that:

... "length": { "type": "number", "subtype": "u32", "range": { "low": 1, "high": 2, "step": 1 } }, ...

4. Interestingly, using the `"range"` workaround as above still gives an error with `"subtype": "i32"`:

Error: At namespace "temp"

Caused by: BadRequest: could not convert from value '1': Type { expected: "U32", got: "i32(1)" }



**Expected behavior**
Either allow all variations of 32-bit integer types as array lengths (constants, ranges, etc.) or none at all (i.e. limit to just 64-bit integers).