-
Notifications
You must be signed in to change notification settings - Fork 0
fix: ensure that "empty" object fields on advisories are handled correctly #56
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,5 +19,6 @@ select = [ | |
| known-first-party = [ | ||
| 'generate_osv_advisories', | ||
| 'user_agent', | ||
| 'typings' | ||
| 'typings', | ||
| 'utils' | ||
| ] | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,21 +17,42 @@ class Node(typing.TypedDict): | |
| type: str | ||
|
|
||
|
|
||
| class Advisory(Node): | ||
| class AdvisoryBase(Node): | ||
| field_is_psa: typing.Literal['0', '1'] | ||
| field_affected_versions: str | None | ||
| field_project: EntityReferenceField | ||
| field_fixed_in: list[EntityReferenceField] | ||
| field_sa_reported_by: RichTextField | list[typing.Never] | ||
| field_sa_criticality: str | ||
| field_sa_cve: list[str] | ||
| field_sa_description: RichTextField | ||
| created: str | ||
| changed: str | ||
| title: str | ||
| url: str | ||
|
|
||
|
|
||
| class Advisory(AdvisoryBase): | ||
| """ | ||
| Represents an advisory sourced from the Drupal JSON API that has been | ||
| transformed to make it easier to work with | ||
| """ | ||
|
|
||
| field_project: EntityReferenceField | None | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. note: I've included this field as its technically possible, even though I think its required on the upstreams end |
||
| field_sa_reported_by: RichTextField | ||
| field_sa_description: RichTextField | ||
|
|
||
|
|
||
| class AdvisoryRaw(AdvisoryBase): | ||
| """ | ||
| Represents an advisory provided by the Drupal JSON API without any post-processing. | ||
|
|
||
| This mainly means that object fields which don't have a value in the database | ||
| will be represented by an empty list due to how associated arrays in PHP work | ||
| """ | ||
|
|
||
| field_project: EntityReferenceField | list[typing.Never] | ||
| field_sa_reported_by: RichTextField | list[typing.Never] | ||
| field_sa_description: RichTextField | list[typing.Never] | ||
|
|
||
|
|
||
| class Project(Node): | ||
| # type will be project_module, project_theme, or project_core | ||
| field_project_machine_name: str | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| import json | ||
|
|
||
| from typings import drupal | ||
|
|
||
|
|
||
| def load_sa_advisory(file_path: str) -> drupal.Advisory: | ||
| """ | ||
| Loads a Drupal advisory from a json file stored on disk, making some adjustments | ||
| in the process to make it easier to work with | ||
| """ | ||
| with open(file_path) as f: | ||
| raw_advisory: drupal.AdvisoryRaw = json.load(f) | ||
|
|
||
| # noinspection PyTypeChecker | ||
| # https://youtrack.jetbrains.com/issue/PY-58714/False-positive-TypedDict-has-missing-key-when-using-unpacking | ||
| sa_advisory: drupal.Advisory = { | ||
| **raw_advisory, | ||
| 'field_project': None, | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. note: since this represents a relationship with another entity whose ID we will look sometimes, I didn't think it was a good idea to try and have a fallback for this for the other fields though I felt that was safe and worth doing as it lets us avoid changing most other code |
||
| 'field_sa_reported_by': {'format': '1', 'value': ''}, | ||
| 'field_sa_description': {'format': '1', 'value': ''}, | ||
| } | ||
|
|
||
| if isinstance(raw_advisory['field_project'], dict): | ||
| sa_advisory['field_project'] = raw_advisory['field_project'] | ||
|
|
||
| if isinstance(raw_advisory['field_sa_reported_by'], dict): | ||
| sa_advisory['field_sa_reported_by'] = raw_advisory['field_sa_reported_by'] | ||
|
|
||
| if isinstance(raw_advisory['field_sa_description'], dict): | ||
| sa_advisory['field_sa_description'] = raw_advisory['field_sa_description'] | ||
|
|
||
| return sa_advisory | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
note: I've had to split this into three types because Python doesn't allow you to change the type of a field when inheriting, regardless of the variance of the type
i.e. ideally we'd do something like
but that is not supported