-
Notifications
You must be signed in to change notification settings - Fork 42
Extend methods to generate Selection Schema for CVE and other projects #1002
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
Changes from 1 commit
420f143
7131c0d
4ca0064
205c508
7a9bf3b
c05fa15
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 |
|---|---|---|
|
|
@@ -312,7 +312,51 @@ def model_json_schema(cls, **kwargs): | |
| schema = strip_nullable_anyof(schema) | ||
|
|
||
| return order_schema(schema) | ||
| def _post_process(self, data): | ||
| """ | ||
| Ensures all Selection.values are lists and removes empty array elements. | ||
| """ | ||
| def fix_selection(selection): | ||
|
||
| # Convert tuple to list and filter out empty items | ||
| values = selection.get("values", []) | ||
| # Ensure it's a list, filter out empty/falsy items | ||
| selection["values"] = [v for v in list(values) if v] | ||
| return selection | ||
|
|
||
| # If this is a dict with selections, process each selection | ||
| if isinstance(data, dict) and "selections" in data: | ||
| data["selections"] = [ | ||
| fix_selection(sel) for sel in data["selections"] if sel | ||
| ] | ||
| # Remove empty array fields from the top level | ||
| keys_to_delete = [k for k, v in data.items() if isinstance(v, list) and not v] | ||
|
||
| for k in keys_to_delete: | ||
| del data[k] | ||
| return data | ||
|
|
||
| def model_dump(self, *args, **kwargs): | ||
| data = super().model_dump(*args, **kwargs) | ||
| return self._post_process(data) | ||
|
|
||
| def model_dump_json(self, *args, **kwargs): | ||
| # Dump to python dict first, post-process, then dump to JSON | ||
| import json | ||
| from datetime import timezone | ||
| model_dump_kwargs = kwargs.copy() | ||
| json_kwargs = {} | ||
| # List of json.dumps kwargs you want to support | ||
| json_kwarg_names = ['indent', 'sort_keys', 'separators', 'ensure_ascii'] | ||
|
||
| for key in json_kwarg_names: | ||
| if key in model_dump_kwargs: | ||
| json_kwargs[key] = model_dump_kwargs.pop(key) | ||
| # Get dict with Pydantic's processing (exclude_none, etc.) | ||
| data = super().model_dump(*args, **model_dump_kwargs) | ||
| data = self._post_process(data) | ||
| # Format timestamp as UTC RFC3339 string | ||
| if "timestamp" in data and isinstance(data["timestamp"], datetime): | ||
| utc_dt = data["timestamp"].astimezone(timezone.utc) | ||
sei-vsarvepalli marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| data["timestamp"] = utc_dt.strftime("%Y-%m-%dT%H:%M:%SZ") | ||
sei-vsarvepalli marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return json.dumps(data, **json_kwargs) | ||
sei-vsarvepalli marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| def main() -> None: | ||
| print( | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.