Repository navigation
[Enhancement] Add support for deeply nested arrays in gh api magic fields #8761
Description
Activity
- addedenhancementa request to improve CLIa request to improve CLIgh-apirelating to the gh api commandrelating to the gh api commandand removedbugSomething isn't workingSomething isn't working
on Feb 27, 2024 @Ebonsignori : thank you for opening up this issue along with the associated PR! ❤
Less a bug and more an enhancement as I don't believe the original nested fields feature added in
v2.21.0via #6614 was intended for highly complex data structures as you are describing. That said, let's see how we might be able to enhancegh apifor a new use case.Reacted by Evan Bonsignori- changed the title
[-][Bug] Unable to add multiple values to arrays nested in an object using nested fields syntax[/-][+][Enhancement] Add support for deeply nested arrays in `gh api` magic fields[/+]on Feb 27, 2024 Good point! My hope is that the syntax isn't too complex and that this is a useful enhancement 😁
If we do want to go forward with the new syntax, the docs are ready to be updated to accommodate it https://github.com/github/docs-internal/pull/49385
Meaning every REST endpoint in our docs will have a
GitHub CLIexample tab that is functionally identical to the cURL & JavaScript tabsFor what it's worth, workarounds for the custom properties issue were discussed in #8621
Also, as I'm reading your description of this issue I am confused. You say:
gh api -f labels[][name]=a -f "labels[][description]=label a" would turn into { "labels": { "name": "a", "description": "label a" } }but what I see is:
{ "labels": [ { "description": "label a", "name": "a" } ] }This might just be a typo on your side forgetting
[]?And just for clarities sake, when you say regarding
gh api -f 'labels[][name]=a' -f 'labels[][description]=label a' -f 'labels[][allowedColors][]=red' -f 'labels[][allowedColors][]=blue'It errors when it should result in the following JSON
What we get is:
{ "labels": [ { "allowedColors": [ "red" ], "description": "label a", "name": "a" }, { "allowedColors": [ "blue" ] } ] }This might just be a typo on your side forgetting []?
Yes this is exactly the case 😅
And just for clarities sake, when you say regarding gh api -f 'labels[][name]=a' -f 'labels[][description]=label a' -f 'labels[][allowedColors][]=red' -f 'labels[][allowedColors][]=blue'
It errors when it should result in the following JSON
Again correct, editing my original body to reflect the current behavior
Describe the bug
#6614 added support for a syntax that supports adding arrays and/or nested objects, e.g.
You're able to set an array of objects like so,
gh api -f labels[][name]=a -f "labels[][description]=label a"would turn into,
{ "labels": { "name": "a", "description": "label a" } }The order of the arguments matters for adding multiple items to the same object. But if you try to set add more than one item in an array in one of these objects the
gh climesses up the parsing and no longer thinks that the next item in the array is part of the same object.For example,
Works and adds
allowedColorsto the object as an array with one item,red, e.g.{ "labels": { "name": "a", "description": "label a", "allowedColors": ["red"] }, { "allowedColors": ["blue"] } }But the following command does not add another item to the array,
It errors when it should result in the following JSON,
{ "labels": { "name": "a", "description": "label a", "allowedColors": ["red", "blue"] } }Logs
I discovered this bug while attempting to add docs to our
GitHub CLIexample tab on GitHub docs. There are requests that require passing more than one item to an array, but are not able to due to this bug.An example from docs for creating / updating custom properties for an org. In the link the
GitHub CLIexample is bugged, but it should be able to show the following,