ghapi icon indicating copy to clipboard operation
ghapi copied to clipboard

Recommended way of sending null as a parameter

Open SirRender00 opened this issue 4 years ago • 10 comments

Hey, I've run into an issue where I can't figure out how to send null as required by some endpoints by the Github API, (specifically updating branch protection). This endpoint requires parameters of type object or nullable. I've tried var=None, var='null', var='', var={}. All are met with a HTTP422 Error: Unprocessable Entity (as opposed to specifying the values which work fine). Any help would be appreciated.

SirRender00 avatar Jul 23 '21 23:07 SirRender00

I would also like to ask this one. I am also trying to update branch protection and cannot send Null. Can anyone help?

salgo avatar Sep 14 '21 10:09 salgo

Hey any update on this? There are API endpoints that are broken presently. For required arguments, values of None must be converted to null and explicitly sent.

chebee7i avatar Jun 27 '22 01:06 chebee7i

I'm also seeing this issue, we've had to drop down to using the requests library where API calls via ghapi error out.

tide-jamiegwatkin avatar Jul 12 '22 11:07 tide-jamiegwatkin

I'm seeing this issue too. Running v1.0.3. python 3.10

drahamim avatar Nov 29 '22 17:11 drahamim

Any updates?

chebee7i avatar Dec 29 '22 18:12 chebee7i

I'm seeing this issue as well when using repos.update_branch_protection, since null is valid input for some of that endpoint's parameters.

It looks like the issue stems from this statement, which drops any key-value pairs where the value is None. https://github.com/fastai/ghapi/blob/b77bfaebd937a3b7527b117a5e43d99bcb98b39d/ghapi/core.py#L59

I tried removing that if condition like so:

kwargs = {k: v for k, v in kwargs.items()}

And I'm now able to use the endpoint I previously mentioned without any issues.

Your mileage may vary.

EDIT: Or, if you actually want to do this the right way, you can just remove that line entirely, since it doesn't do anything of consequence once that if is removed. 😉

raeganbarker avatar Apr 18 '23 20:04 raeganbarker

It worked for me

DavidSolWizeline avatar Aug 02 '23 22:08 DavidSolWizeline

Chiming in to say this is still an issue, particularly for update_branch_protection.

All of these fields accept null on the GitHub API side, but cannot be set as such using ghapi.

gh.repos.update_branch_protection(
    ...,
    required_status_checks=None,
    enforce_admins=None,
    required_pull_request_reviews=None,
    restrictions=None,
    allow_force_pushes=None,
)

It appears that this method is currently unusable to unset these fields because null is the only way to do so. In order to do so, you can use GhApi.__call__ even though it's not very nice.

gh(f"/repos/{owner}/{repo}/branches/{branch}/protection", "PUT", data=b"""
    {
        ...
        "required_status_checks": null,
        "enforce_admins": null,
        "required_pull_request_reviews": null,
        "restrictions": null,
        "allow_force_pushes": null
    }
""")

This is tested working with v1.0.4

shreve avatar Jan 15 '24 19:01 shreve

Seems like this project might be abandoned.

chebee7i avatar Mar 01 '24 22:03 chebee7i

Personally, I've given up on this library and just wrap around the API natively with requests or gql. With Github API supporting versioning as a header, this library is not worth the hassle in my opinion.

SirRender00 avatar Mar 01 '24 22:03 SirRender00

Seems like this project might be abandoned.

It's definitely not abandoned. No one at-mentioned me so I didn't get notified however.

Has anyone tried the suggested approach by @raeganbarker of removing that line to check whether all tests are still passed? If so, I'm happy to make that update (or someone could do a PR).

jph00 avatar Mar 31 '24 23:03 jph00

@jph00 I made a PR. One test is failing, but it appears to require write access to the fastai/ghapi-test repo, so I think we're good. I can confirm my use case from above now works. https://github.com/fastai/ghapi/issues/81#issuecomment-1892671885

shreve avatar Apr 03 '24 17:04 shreve

Super -- really great to have this resolved. :D

jph00 avatar Apr 04 '24 01:04 jph00

It's definitely not abandoned. No one at-mentioned me so I didn't get notified however.

What does this mean, precisely? The issue is years old, and there are only 43 issues in the entire repository. So I'd think maintainers would be looking actively at open tickets.

chebee7i avatar Apr 07 '24 21:04 chebee7i

What does this mean, precisely?

It means a single volunteer, me, is looking after dozens of projects largely on my own, so I'd like some basic level of help from the community of people who use my work for free. Is at-mentioning me really too much to ask?

jph00 avatar Apr 07 '24 23:04 jph00

@shreve thanks for making the PR and @jph00 thanks for reviewing and landing!

feanil avatar Apr 08 '24 15:04 feanil