Skip to content

Commit

Permalink
Merge pull request #74 from siemens/fix/license_type_issue
Browse files Browse the repository at this point in the history
refactor(external_ref): Remove unnecessary UpdateExternalRefsJSONPayload type

Reviewed-by: [email protected]
Tested-by: [email protected]
  • Loading branch information
GMishx authored Jul 16, 2024
2 parents b486528 + a2caaaf commit 9524e7b
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 42 deletions.
4 changes: 4 additions & 0 deletions cmd/laas/docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -1906,6 +1906,10 @@ const docTemplate = `{
"models.LicensePATCHRequestJSONSchema": {
"type": "object",
"properties": {
"external_ref": {
"type": "object",
"additionalProperties": true
},
"marydone": {
"type": "boolean"
},
Expand Down
4 changes: 4 additions & 0 deletions cmd/laas/docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -1899,6 +1899,10 @@
"models.LicensePATCHRequestJSONSchema": {
"type": "object",
"properties": {
"external_ref": {
"type": "object",
"additionalProperties": true
},
"marydone": {
"type": "boolean"
},
Expand Down
3 changes: 3 additions & 0 deletions cmd/laas/docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,9 @@ definitions:
type: object
models.LicensePATCHRequestJSONSchema:
properties:
external_ref:
additionalProperties: true
type: object
marydone:
type: boolean
rf_FSFfree:
Expand Down
21 changes: 3 additions & 18 deletions pkg/api/licenses.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"github.com/fossology/LicenseDb/pkg/utils"
"github.com/gin-gonic/gin"

"github.com/gin-gonic/gin/binding"
"gorm.io/gorm"
"gorm.io/gorm/clause"
)
Expand Down Expand Up @@ -341,7 +340,6 @@ func CreateLicense(c *gin.Context) {
func UpdateLicense(c *gin.Context) {
_ = db.DB.Transaction(func(tx *gorm.DB) error {
var updates models.LicensePATCHRequestJSONSchema
var externalRefsPayload models.UpdateExternalRefsJSONPayload
var newLicense models.LicenseDB
var oldLicense models.LicenseDB
newLicenseMap := make(map[string]interface{})
Expand All @@ -361,8 +359,7 @@ func UpdateLicense(c *gin.Context) {
return err
}

// https://github.com/gin-gonic/gin/pull/1341
if err := c.ShouldBindBodyWith(&updates, binding.JSON); err != nil {
if err := c.ShouldBindJSON(&updates); err != nil {
er := models.LicenseError{
Status: http.StatusBadRequest,
Message: "invalid json body update",
Expand All @@ -374,20 +371,8 @@ func UpdateLicense(c *gin.Context) {
return err
}

if err := c.ShouldBindBodyWith(&externalRefsPayload, binding.JSON); err != nil {
er := models.LicenseError{
Status: http.StatusBadRequest,
Message: "invalid json body",
Error: err.Error(),
Path: c.Request.URL.Path,
Timestamp: time.Now().Format(time.RFC3339),
}
c.JSON(http.StatusBadRequest, er)
return err
}

// Overwrite values of existing keys, add new key value pairs and remove keys with null values.
if err := tx.Model(&oldLicense).UpdateColumn("external_ref", gorm.Expr("jsonb_strip_nulls(external_ref || ?)", externalRefsPayload.ExternalRef)).Error; err != nil {
if err := tx.Model(&newLicense).Clauses(clause.Returning{}).Where(models.LicenseDB{Id: oldLicense.Id}).UpdateColumn("external_ref", gorm.Expr("jsonb_strip_nulls(COALESCE(external_ref, '{}'::jsonb) || ?)", updates.ExternalRef)).Error; err != nil {
er := models.LicenseError{
Status: http.StatusInternalServerError,
Message: "Failed to update license",
Expand Down Expand Up @@ -520,7 +505,7 @@ func UpdateLicense(c *gin.Context) {
}

// Update all other fields except external_ref
if err := tx.Model(&newLicense).Where(models.LicenseDB{Id: oldLicense.Id}).Clauses(clause.Returning{}).Updates(newLicenseMap).Error; err != nil {
if err := tx.Model(&newLicense).Clauses(clause.Returning{}).Where(models.LicenseDB{Id: oldLicense.Id}).Updates(newLicenseMap).Error; err != nil {
er := models.LicenseError{
Status: http.StatusInternalServerError,
Message: "Failed to update license",
Expand Down
43 changes: 19 additions & 24 deletions pkg/models/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,24 +115,25 @@ type LicenseJson struct {
// LicensePATCHRequestJSONSchema struct represents the input format for updating an existing license.
// Note that the license ID and shortname cannot be updated.
type LicensePATCHRequestJSONSchema struct {
Fullname OptionalData[string] `json:"rf_fullname" swaggertype:"string" example:"MIT License"`
Text OptionalData[string] `json:"rf_text" swaggertype:"string" example:"MIT License Text here"`
Url OptionalData[string] `json:"rf_url" swaggertype:"string" example:"https://opensource.org/licenses/MIT"`
Copyleft OptionalData[bool] `json:"rf_copyleft" swaggertype:"boolean"`
FSFfree OptionalData[bool] `json:"rf_FSFfree" swaggertype:"boolean"`
OSIapproved OptionalData[bool] `json:"rf_OSIapproved" swaggertype:"boolean"`
GPLv2compatible OptionalData[bool] `json:"rf_GPLv2compatible" swaggertype:"boolean"`
GPLv3compatible OptionalData[bool] `json:"rf_GPLv3compatible" swaggertype:"boolean"`
Notes OptionalData[string] `json:"rf_notes" example:"This license has been superseded." swaggertype:"string"`
Fedora OptionalData[string] `json:"rf_Fedora" swaggertype:"string"`
TextUpdatable OptionalData[bool] `json:"rf_text_updatable" swaggertype:"boolean"`
DetectorType OptionalData[int64] `json:"rf_detector_type" example:"1" swaggertype:"integer"`
Active OptionalData[bool] `json:"rf_active" swaggertype:"boolean"`
Source OptionalData[string] `json:"rf_source" swaggertype:"string"`
SpdxId OptionalData[string] `json:"rf_spdx_id" example:"MIT" swaggertype:"string"`
Risk OptionalData[int64] `json:"rf_risk" swaggertype:"integer" example:"3"`
Flag OptionalData[int64] `json:"rf_flag" example:"1" swaggertype:"integer"`
Marydone OptionalData[bool] `json:"marydone" swaggertype:"boolean"`
Fullname OptionalData[string] `json:"rf_fullname" swaggertype:"string" example:"MIT License"`
Text OptionalData[string] `json:"rf_text" swaggertype:"string" example:"MIT License Text here"`
Url OptionalData[string] `json:"rf_url" swaggertype:"string" example:"https://opensource.org/licenses/MIT"`
Copyleft OptionalData[bool] `json:"rf_copyleft" swaggertype:"boolean"`
FSFfree OptionalData[bool] `json:"rf_FSFfree" swaggertype:"boolean"`
OSIapproved OptionalData[bool] `json:"rf_OSIapproved" swaggertype:"boolean"`
GPLv2compatible OptionalData[bool] `json:"rf_GPLv2compatible" swaggertype:"boolean"`
GPLv3compatible OptionalData[bool] `json:"rf_GPLv3compatible" swaggertype:"boolean"`
Notes OptionalData[string] `json:"rf_notes" example:"This license has been superseded." swaggertype:"string"`
Fedora OptionalData[string] `json:"rf_Fedora" swaggertype:"string"`
TextUpdatable OptionalData[bool] `json:"rf_text_updatable" swaggertype:"boolean"`
DetectorType OptionalData[int64] `json:"rf_detector_type" example:"1" swaggertype:"integer"`
Active OptionalData[bool] `json:"rf_active" swaggertype:"boolean"`
Source OptionalData[string] `json:"rf_source" swaggertype:"string"`
SpdxId OptionalData[string] `json:"rf_spdx_id" example:"MIT" swaggertype:"string"`
Risk OptionalData[int64] `json:"rf_risk" swaggertype:"integer" example:"3"`
Flag OptionalData[int64] `json:"rf_flag" example:"1" swaggertype:"integer"`
Marydone OptionalData[bool] `json:"marydone" swaggertype:"boolean"`
ExternalRef map[string]interface{} `json:"external_ref"`
}

// LicensePreviewResponse gets us the list of all license shortnames
Expand All @@ -141,12 +142,6 @@ type LicensePreviewResponse struct {
Shortnames []string `json:"shortnames" example:"GPL-2.0-only,GPL-2.0-or-later"`
}

// UpdateExternalRefsJSONPayload struct represents the external ref key value
// pairs for update
type UpdateExternalRefsJSONPayload struct {
ExternalRef map[string]interface{} `json:"external_ref"`
}

// LicenseImport represents an license record in the import json file.
type LicenseImport struct {
Shortname NullableAndOptionalData[string] `json:"rf_shortname" validate:"required" example:"MIT"`
Expand Down

0 comments on commit 9524e7b

Please sign in to comment.