snyk-labs / pysnyk

A Python client for the Snyk API.
https://snyk.docs.apiary.io/
MIT License
85 stars 116 forks source link

[BUG]: SAST Projects causing InvalidFieldValue error when getting all projects for an org #162

Open brian-hamill-bv opened 1 year ago

brian-hamill-bv commented 1 year ago

Is there an existing issue for this?

Description of the bug

When using the Snyk client to get an individual org, then get all projects for that org an error is returned. This error seems to be because the SAST/Code Analysis projects return null/None for the totalDependencies value.

Steps To Reproduce

Create a python script with the following code and run it against any org that includes projects of type SAST/Code Analysis.

import snyk
client = snyk.SnykClient('api-key-here', tries=5, debug=True)
print(client.organizations.get('org-id-here').projects.all())

Additional Information

The full error messages (with file paths and org IDs removed) is as follows:


DEBUG:snyk.client:GET: https://snyk.io/api/v1/orgs
DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): snyk.io:443
DEBUG:urllib3.connectionpool:https://snyk.io:443 "GET /api/v1/orgs HTTP/1.1" 200 2050
DEBUG:snyk.client:GET: https://snyk.io/api/v1/org/org-id-here/projects
DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): snyk.io:443
DEBUG:urllib3.connectionpool:https://snyk.io:443 "GET /api/v1/org/org-id-here/projects HTTP/1.1" 200 1665
Traceback (most recent call last):
  File "<string>", line 78, in from_dict
TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/path-here/script.py", line 7, in <module>
    print(client.organizations.get('org-id-here').projects.all())
  File "/path-here/Library/Python/3.9/lib/python/site-packages/snyk/managers.py", line 172, in all
    return self._query()
  File "/path-here/Library/Python/3.9/lib/python/site-packages/snyk/managers.py", line 163, in _query
    projects.append(self.klass.from_dict(project_data))
  File "<string>", line 85, in from_dict
mashumaro.exceptions.InvalidFieldValue: Field "totalDependencies" of type int in Project has invalid value None
Fatal1ty commented 1 year ago

I believe this is related to the version 3.2 of mashumaro that fixed no error when passing None values to fields that shouldn't be None.

if None value is ok for totalDependencies then this field should be annotated as Optional[int] to fix this issue.

Gil-Tohar-Forter commented 1 year ago

I am running into this issue a well, i think TotalDependencies needs to be annotated as mentioned above!

sndrsnk commented 1 year ago

We're also experiencing this issue.

ndoell commented 1 year ago

Same issue here. I seem to be seeing this error pop up when pysnyk runs into an IaC project while using client.organizations.get(org_id).projects.get(p.id).issueset_aggregated.all()

ig596 commented 1 year ago

I believe I corrected this in #169. If so, please mark this as closed/resolved.

kbroughton commented 9 months ago

@ig596 I just tried with version 0.9.18 and still get the same error using the simple test at the top of the issue