microsoft / microsoft/knack

When json value has null, knack in Azure CLI changes column order unintentionally.

Open
#255 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
358
Forks
101
PR merge metrics
No merged PRs in 30d

Description

Problem

When json value has "null", knack in Azure CLI unintentionally changes column order regardless of specifying order of the columns by JMESPath.

Reproduce steps

  1. Prepare some json data including null value in member like this
$ az graph query -q 'Resources | where type == "microsoft.compute/virtualmachines" | project name, properties.storageProfile.dataDisks[0].name'
{
  "count": 3,
  "data": [
    {
      "name": "kvm-ubuntu1804Gen2-NoneDisk",
      "properties_storageProfile_dataDisks_0_name": null
    },
    {
      "name": "tkdsvm",
      "properties_storageProfile_dataDisks_0_name": null
    },
    {
      "name": "tkvm-ubuntu1804Gen2-AddDisk",
      "properties_storageProfile_dataDisks_0_name": "tkvm-ubuntu1804Gen2-AddDisk_DataDisk_0"
    }
  ],
  "skip_token": null,
  "total_records": 3
}
  1. Run JMESPath query using --query option:
$ az graph query -q 'Resources | where type == "microsoft.compute/virtualmachines"'  --query 'data[].{Name: name, Location: location, Disk: properties.storageProfile.dataDisks[0].name, ResouceGroup: resourceGroup}' -o table
Name                         Location    ResouceGroup    Disk
---------------------------  ----------  --------------  --------------------------------------
kvm-ubuntu1804Gen2-NoneDisk  japaneast   vm-rg
tkvm-ubuntu1804Gen2-AddDisk  japaneast   vm-rg           tkvm-ubuntu1804Gen2-AddDisk_DataDisk_0
tkdsvm                       japaneast   dsvmrg

As the above --query result shows different order of columns regardless the query specifies "Name, Location, Disk, ResourceGroup" orders.

In my view

The behavior seems to change the result when the first record includes "null" value. If the first record has no "null" value, the result expectly shows according to --query order:

$ az graph query -q 'Resources | where type == "microsoft.compute/virtualmachines"'  --query 'data[].{Name: name, Location: location, Disk: properties.storageProfile.dataDisks[0].name, ResouceGroup: resourceGroup}' -o table --skip 1
Name                         Location    Disk                                    ResouceGroup
---------------------------  ----------  --------------------------------------  --------------
tkvm-ubuntu1804Gen2-AddDisk  japaneast   tkvm-ubuntu1804Gen2-AddDisk_DataDisk_0  vm-rg
tkdsvm                       japaneast                                           dsvmrg

This null value behavior seems to be caused by the following line:
knack/output.py at d407b81d451d36c6ca1baeb3cd067a3f1275cb85 · microsoft/knack

Request

Could you enhance/fix this behavior? We want you to show the same result whether the first record includes null value or not.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the Azure CLI table output with the JSON data and JMESPath query shown, then inspect knack/output.py at line 187. Compare the column order when the first record contains null with the order when it does not. Done means table output preserves the requested column order in both cases.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
cli
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.