Adds custom marshaller for Project to support empty LastBomImports#40
Open
bomoko wants to merge 2 commits intoDependencyTrack:mainfrom
Open
Adds custom marshaller for Project to support empty LastBomImports#40bomoko wants to merge 2 commits intoDependencyTrack:mainfrom
bomoko wants to merge 2 commits intoDependencyTrack:mainfrom
Conversation
Signed-off-by: Blaize Kaye <blaize.kaye@amazee.io>
✅ Snyk checks have passed. No issues have been found so far.
💻 Catch issues earlier using the plugins for VS Code, JetBrains IDEs, Visual Studio, and Eclipse. |
There was a problem hiding this comment.
Pull request overview
This PR adjusts how Project is serialized to JSON so that lastBomImport is omitted when it would otherwise be sent as 0, matching Dependency-Track’s behavior of leaving “Last BOM Import” empty for projects without an import.
Changes:
- Added a custom
Project.MarshalJSONimplementation to omitlastBomImportwhenLastBOMImport == 0. - Added
encoding/jsonimport to support the custom marshaler.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+48
to
+64
| // Here we write a custom MarshalJSON function to give us more control over the JSON output. | ||
| func (p Project) MarshalJSON() ([]byte, error) { | ||
| type Alias Project // Avoid infinite recursion | ||
| aux := struct { | ||
| Alias | ||
| LastBOMImport *int `json:"lastBomImport,omitempty"` | ||
| }{ | ||
| Alias: (Alias)(p), | ||
| LastBOMImport: nil, | ||
| } | ||
| // In particular, sending a 0 to the API gives us an invalid date | ||
| // i.e. the beginning of the epoch. Better to be nil. | ||
| if p.LastBOMImport != 0 { | ||
| aux.LastBOMImport = &p.LastBOMImport | ||
| } | ||
| return json.Marshal(aux) | ||
| } |
Comment on lines
+49
to
+64
| func (p Project) MarshalJSON() ([]byte, error) { | ||
| type Alias Project // Avoid infinite recursion | ||
| aux := struct { | ||
| Alias | ||
| LastBOMImport *int `json:"lastBomImport,omitempty"` | ||
| }{ | ||
| Alias: (Alias)(p), | ||
| LastBOMImport: nil, | ||
| } | ||
| // In particular, sending a 0 to the API gives us an invalid date | ||
| // i.e. the beginning of the epoch. Better to be nil. | ||
| if p.LastBOMImport != 0 { | ||
| aux.LastBOMImport = &p.LastBOMImport | ||
| } | ||
| return json.Marshal(aux) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR introduces a custom marshaller function for the Project type. It essentially allows for matching types with Dependency Track itself, which seems to allow nil values for their "Last BOM Import" column.
closes #38