Skip to content

Commit

Permalink
APIGOV-26069 - do not send an empty organization as part of the owner…
Browse files Browse the repository at this point in the history
… object (#671)

* APIGOV-26069 - do not send an empty organization as part of the owner object

* APIGOV-26069 - validate

* APIGOV-26069 - more verification
  • Loading branch information
jcollins-axway authored Aug 8, 2023
1 parent 86607fd commit e61ac72
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
12 changes: 8 additions & 4 deletions pkg/apic/apiserver/models/api/v1/owner.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,18 @@ func (o *Owner) MarshalJSON() ([]byte, error) {
}

aux := struct {
Type string `json:"type,omitempty"`
ID string `json:"id"`
Organization Organization `json:"organization,omitempty"`
Type string `json:"type,omitempty"`
ID string `json:"id"`
Organization *Organization `json:"organization,omitempty"`
}{}

aux.Type = t
aux.ID = o.ID
aux.Organization = o.Organization
if o.Organization.ID != "" {
aux.Organization = &Organization{
ID: o.Organization.ID,
}
}

return json.Marshal(aux)
}
Expand Down
18 changes: 18 additions & 0 deletions pkg/apic/apiserver/models/api/v1/owner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,14 @@ func TestOwner_MarshalJSON(t *testing.T) {

b, err = o.MarshalJSON()
assert.Nil(t, err)
assert.NotContains(t, string(b), "organization")

o2 = &Owner{}
err = json.Unmarshal(b, o2)
assert.Nil(t, err)
assert.Equal(t, o.Type, o2.Type)
assert.Equal(t, o.ID, o2.ID)
assert.Equal(t, o.Organization, o2.Organization)

invalid := []byte(`{"type":"fake","id":"123"}`)
err = json.Unmarshal(invalid, o2)
Expand All @@ -42,4 +44,20 @@ func TestOwner_MarshalJSON(t *testing.T) {
assert.Nil(t, err)
assert.Equal(t, o.Type, TeamOwner)
assert.Equal(t, o.ID, o2.ID)

o3 := &Owner{}
o3.SetType(TeamOwner)
o3.SetID("123")
o3.Organization = Organization{ID: "321"}

b, err = o3.MarshalJSON()
assert.Nil(t, err)
assert.Contains(t, string(b), "organization")

o4 := &Owner{}
err = json.Unmarshal(b, o4)
assert.Nil(t, err)
assert.Equal(t, o3.Type, o4.Type)
assert.Equal(t, o3.ID, o4.ID)
assert.Equal(t, o3.Organization, o4.Organization)
}

0 comments on commit e61ac72

Please sign in to comment.