Skip to content

Commit

Permalink
Fixes 143: Remove null fields from api responses
Browse files Browse the repository at this point in the history
  • Loading branch information
rverdile committed Aug 26, 2022
1 parent 2f75c3d commit bf17beb
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 13 deletions.
3 changes: 3 additions & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@ linters:
- gofmt
- whitespace
- govet
issues:
exclude:
- composite
15 changes: 15 additions & 0 deletions docs/openapi/Models/api.RepositoryResponseJSON.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# api.RepositoryResponseJSON
## Properties

| Name | Type | Description | Notes |
|------------ | ------------- | ------------- | -------------|
| **account\_id** | **String** | Account ID of the owner | [optional] [default to null] |
| **distribution\_arch** | **String** | Architecture to restrict client usage to | [optional] [default to null] |
| **distribution\_versions** | **List** | Versions to restrict client usage to | [optional] [default to null] |
| **name** | **String** | Name of the remote yum repository | [optional] [default to null] |
| **org\_id** | **String** | Organization ID of the owner | [optional] [default to null] |
| **url** | **String** | URL of the remote yum repository | [optional] [default to null] |
| **uuid** | **String** | UUID of the object | [optional] [default to null] |

[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)

2 changes: 1 addition & 1 deletion pkg/api/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type RepositoryRequest struct {
}

type RepositoryBulkCreateResponse struct {
ErrorMsg *string `json:"error"` // Error during creation
ErrorMsg string `json:"error"` // Error during creation
Repository *RepositoryResponse `json:"repository"` // Repository object information
}

Expand Down
6 changes: 3 additions & 3 deletions pkg/dao/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func (r repositoryDaoImpl) bulkCreate(tx *gorm.DB, newRepositories []api.Reposit
dbErr = DBErrorToApi(err)
errMsg := dbErr.Error()
result[i] = api.RepositoryBulkCreateResponse{
ErrorMsg: &errMsg,
ErrorMsg: errMsg,
Repository: nil,
}
tx.RollbackTo("beforecreate")
Expand All @@ -117,7 +117,7 @@ func (r repositoryDaoImpl) bulkCreate(tx *gorm.DB, newRepositories []api.Reposit
dbErr = DBErrorToApi(err)
errMsg := dbErr.Error()
result[i] = api.RepositoryBulkCreateResponse{
ErrorMsg: &errMsg,
ErrorMsg: errMsg,
Repository: nil,
}
tx.RollbackTo("beforecreate")
Expand All @@ -128,7 +128,7 @@ func (r repositoryDaoImpl) bulkCreate(tx *gorm.DB, newRepositories []api.Reposit
response.URL = newRepos[i].URL
if dbErr == nil {
result[i] = api.RepositoryBulkCreateResponse{
ErrorMsg: nil,
ErrorMsg: "",
Repository: &response,
}
}
Expand Down
21 changes: 13 additions & 8 deletions pkg/handler/repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
"github.com/content-services/content-sources-backend/pkg/dao"
"github.com/content-services/content-sources-backend/pkg/db"
"github.com/labstack/echo/v4"
"github.com/openlyinc/pointy"
"github.com/redhatinsights/platform-go-middlewares/identity"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
Expand Down Expand Up @@ -494,12 +493,18 @@ func (suite *ReposSuite) TestBulkCreate() {

expected := []api.RepositoryBulkCreateResponse{
{
ErrorMsg: nil,
Repository: &api.RepositoryResponse{Name: "repo_1", URL: "https://example1.com"},
ErrorMsg: "",
Repository: &api.RepositoryResponse{
Name: "repo_1",
URL: "https://example1.com",
},
},
{
ErrorMsg: nil,
Repository: &api.RepositoryResponse{Name: "repo_2", URL: "https://example2.com"},
ErrorMsg: "",
Repository: &api.RepositoryResponse{
Name: "repo_2",
URL: "https://example2.com",
},
},
}

Expand Down Expand Up @@ -543,11 +548,11 @@ func (suite *ReposSuite) TestBulkCreateOneFails() {

expected := []api.RepositoryBulkCreateResponse{
{
ErrorMsg: nil,
ErrorMsg: "",
Repository: nil,
},
{
ErrorMsg: pointy.String("Bad validation"),
ErrorMsg: "Bad validation",
Repository: nil,
},
}
Expand Down Expand Up @@ -576,7 +581,7 @@ func (suite *ReposSuite) TestBulkCreateOneFails() {
var response []api.RepositoryBulkCreateResponse
err = json.Unmarshal(body, &response)
assert.Nil(t, err)
assert.Nil(t, response[0].ErrorMsg)
assert.Equal(t, "", response[0].ErrorMsg)
assert.Nil(t, response[0].Repository)
assert.NotNil(t, response[1].ErrorMsg)
assert.Nil(t, response[1].Repository)
Expand Down
2 changes: 1 addition & 1 deletion pkg/models/repository_configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func (rc *RepositoryConfiguration) BeforeUpdate(tx *gorm.DB) error {

func (rc *RepositoryConfiguration) DedupeVersions(tx *gorm.DB) error {
var versionMap = make(map[string]bool)
var unique pq.StringArray
var unique = make(pq.StringArray, 0)
for i := 0; i < len(rc.Versions); i++ {
if _, found := versionMap[rc.Versions[i]]; !found {
versionMap[rc.Versions[i]] = true
Expand Down

0 comments on commit bf17beb

Please sign in to comment.