Azure / azure-iot-explorer

Cross-platform UI for interacting with devices attached to Azure IoT Hub. This tool is meant for learning and testing, not for production environment.
MIT License
223 stars 74 forks source link

[BUG] Enum values with 0 are ignored in writable properties #449

Closed rido-min closed 1 year ago

rido-min commented 3 years ago

Using a PnP model using an enum in a writable property (below one example).

When updating the writable property from IoT Explorer, the value with 0 is not sent in the payload. (all other values work).

{
  "@context": "dtmi:dtdl:context;2",
  "@id": "dtmi:rido:settings;1",
  "@type": "Interface",
  "contents": [
    {
      "@type": "Component",
      "name": "Settings",
      "schema": {
        "@type": "Interface",
        "@id": "dtmi:rido:settings;1",
        "contents": [
          {
            "@type": "Property",
            "name": "DeviceHealthTelemetryConfiguration",
            "schema": {
              "@type": "Enum",
              "valueSchema": "integer",
              "enumValues": [
                {
                  "name": "None",
                  "enumValue": 0
                },
                {
                  "name": "Required",
                  "enumValue": 1
                },
                {
                  "name": "Optional",
                  "enumValue": 2
                }
              ]
            },
            "writable": true
          }
        ]
      }
    }
  ]
}
YingXue commented 3 years ago

Hi @rido-min We are aware of this issue, which has exist for a long time. Unfortunately, javascript treats 0 as false, and there is no yet a good way to fix it.

rido-min commented 3 years ago

Thanks @YingXue, how can I help to find a valid workaround? Can you point to the code where JS converts the 0 to false?

YingXue commented 3 years ago

Hi @rido-min A possible workaround is, since Enum takes in both integer and string type in DTDL, define "@type": "Enum", "valueSchema": "string", and provide Enum value as '0', '1' and so on. From the code side, this is what I've been tracking down for so long but have not get it working properly: https://github.com/Azure/azure-iot-explorer/blob/master/src/app/devices/shared/components/dataForm.tsx#L127

It could be the library (json schema form) which is taking 0 as undefined, I am not entirely sure. Some help would be nice here.

rido-min commented 3 years ago

we cannot ask users to use valueSchema: string when they intend to use an int . This might be a bug in the json schema form library. (wondering how Central solved this problem).

YingXue commented 1 year ago

Fixed released in https://github.com/Azure/azure-iot-explorer/releases/tag/v0.15.8. Closing.