datacontract / datacontract-cli

CLI to manage your datacontract.yaml files
https://cli.datacontract.com
Other
352 stars 60 forks source link

Export to jsonschema drops field traits when set to zero #281

Closed Figueus closed 2 days ago

Figueus commented 5 days ago

When exporting a datacontract to jsonschema it seems to drop the minimum and maximum field traits if they are set to 0.

Example:

[...]
models:
  my_model:
    description: testing minmax
    type: table
    fields:
      mintest:
        type: number
        minimum: 0
        maximum: 100
        description: Testing minimum on zero
      maxtest:
        type: number
        minimum: -100
        maximum: 0
        description: Testing maximum on zero
      onlyminimum:
        type: number
        minimum: 0
      onlymaximum:
        type: number
        maximum: 0
      all:
        type: number
        minimum: 0
        maximum: 0
        description: Both on zero
[...]

Executed command: datacontract export --format jsonschema

Expected result:

[...]
  "properties": {
    "mintest": {
      "type": [
        "number",
        "null"
      ],
      "description": "Testing minimum on zero",
      "minimum": 0,
      "maximum": 100
    },
    "maxtest": {
      "type": [
        "number",
        "null"
      ],
      "description": "Testing maximum on zero",
      "minimum": -100,
      "maximum": 0
    },
    "onlyminimum": {
      "type": [
        "number",
        "null"
      ],
      "minimum": 0
    },
    "onlymaximum": {
      "type": [
        "number",
        "null"
      ],
      "maximum": 0
    },
    "all": {
      "type": [
        "number",
        "null"
      ],
      "minimum": 0,
      "maximum": 0,
      "description": "Both on zero"
    }
  }
[...]

Actual result:

[...]
  "properties": {
    "mintest": {
      "type": [
        "number",
        "null"
      ],
      "description": "Testing minimum on zero",
      "maximum": 100
    },
    "maxtest": {
      "type": [
        "number",
        "null"
      ],
      "description": "Testing maximum on zero",
      "minimum": -100
    },
    "onlyminimum": {
      "type": [
        "number",
        "null"
      ]
    },
    "onlymaximum": {
      "type": [
        "number",
        "null"
      ]
    },
    "all": {
      "type": [
        "number",
        "null"
      ],
      "description": "Both on zero"
    }
  }
[...]

As you can see the minimum and maximum values that are set to zero are removed.

After taking a look at the source I wonder if this if-statement in the jsonconverter might resolve to a falsy value, causing it to not be included in the output.

    if field.minimum:
        property["minimum"] = field.minimum
jochenchrist commented 2 days ago

Yes, confirm. This is a bug. We should check on None.