Skip to content

Commit edd5f5e

Browse files
authored
fix: Endpoint configuration should also accept expressions (#225)
Signed-off-by: Gio Gutierrez <[email protected]>
1 parent 23710ee commit edd5f5e

File tree

2 files changed

+72
-6
lines changed

2 files changed

+72
-6
lines changed

model/endpoint.go

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,9 @@ func (u *LiteralUri) GetValue() interface{} {
9595
}
9696

9797
type EndpointConfiguration struct {
98-
URI URITemplate `json:"uri" validate:"required"`
99-
Authentication *ReferenceableAuthenticationPolicy `json:"authentication,omitempty"`
98+
RuntimeExpression *RuntimeExpression `json:"-"`
99+
URI URITemplate `json:"uri" validate:"required"`
100+
Authentication *ReferenceableAuthenticationPolicy `json:"authentication,omitempty"`
100101
}
101102

102103
// UnmarshalJSON implements custom unmarshalling for EndpointConfiguration.
@@ -116,12 +117,35 @@ func (e *EndpointConfiguration) UnmarshalJSON(data []byte) error {
116117

117118
// Unmarshal the URI field into the appropriate URITemplate implementation
118119
uri, err := UnmarshalURITemplate(temp.URI)
119-
if err != nil {
120-
return fmt.Errorf("invalid URI in EndpointConfiguration: %w", err)
120+
if err == nil {
121+
e.URI = uri
122+
return nil
123+
}
124+
125+
var runtimeExpr RuntimeExpression
126+
if err := json.Unmarshal(temp.URI, &runtimeExpr); err == nil && runtimeExpr.IsValid() {
127+
e.RuntimeExpression = &runtimeExpr
128+
return nil
121129
}
122-
e.URI = uri
123130

124-
return nil
131+
return errors.New("failed to unmarshal EndpointConfiguration: data does not match any known schema")
132+
}
133+
134+
// MarshalJSON implements custom marshalling for Endpoint.
135+
func (e *EndpointConfiguration) MarshalJSON() ([]byte, error) {
136+
m := make(map[string]interface{})
137+
if e.Authentication != nil {
138+
m["authentication"] = e.Authentication
139+
}
140+
141+
if e.RuntimeExpression != nil {
142+
m["uri"] = e.RuntimeExpression
143+
} else if e.URI != nil {
144+
m["uri"] = e.URI
145+
}
146+
147+
// Return an empty JSON object when no fields are set
148+
return json.Marshal(m)
125149
}
126150

127151
type Endpoint struct {

model/endpoint_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,48 @@ func TestEndpoint_UnmarshalJSON(t *testing.T) {
7171
assert.Equal(t, "admin", endpoint.EndpointConfig.Authentication.AuthenticationPolicy.Basic.Password, "Authentication Password should match")
7272
})
7373

74+
t.Run("Valid EndpointConfiguration with reference", func(t *testing.T) {
75+
input := `{
76+
"uri": "http://example.com/{id}",
77+
"authentication": {
78+
"oauth2": { "use": "secret" }
79+
}
80+
}`
81+
82+
var endpoint Endpoint
83+
err := json.Unmarshal([]byte(input), &endpoint)
84+
85+
assert.NoError(t, err, "Unmarshal should not return an error")
86+
assert.NotNil(t, endpoint.EndpointConfig, "EndpointConfig should be set")
87+
assert.NotNil(t, endpoint.EndpointConfig.URI, "EndpointConfig URI should be set")
88+
assert.Nil(t, endpoint.EndpointConfig.RuntimeExpression, "EndpointConfig Expression should not be set")
89+
assert.Equal(t, "secret", endpoint.EndpointConfig.Authentication.AuthenticationPolicy.OAuth2.Use, "Authentication secret should match")
90+
b, err := json.Marshal(&endpoint)
91+
assert.NoError(t, err, "Marshal should not return an error")
92+
assert.JSONEq(t, input, string(b), "Output JSON should match")
93+
})
94+
95+
t.Run("Valid EndpointConfiguration with reference and expression", func(t *testing.T) {
96+
input := `{
97+
"uri": "${example}",
98+
"authentication": {
99+
"oauth2": { "use": "secret" }
100+
}
101+
}`
102+
103+
var endpoint Endpoint
104+
err := json.Unmarshal([]byte(input), &endpoint)
105+
106+
assert.NoError(t, err, "Unmarshal should not return an error")
107+
assert.NotNil(t, endpoint.EndpointConfig, "EndpointConfig should be set")
108+
assert.Nil(t, endpoint.EndpointConfig.URI, "EndpointConfig URI should not be set")
109+
assert.NotNil(t, endpoint.EndpointConfig.RuntimeExpression, "EndpointConfig Expression should be set")
110+
assert.Equal(t, "secret", endpoint.EndpointConfig.Authentication.AuthenticationPolicy.OAuth2.Use, "Authentication secret should match")
111+
b, err := json.Marshal(&endpoint)
112+
assert.NoError(t, err, "Marshal should not return an error")
113+
assert.JSONEq(t, input, string(b), "Output JSON should match")
114+
})
115+
74116
t.Run("Invalid JSON Structure", func(t *testing.T) {
75117
input := `{"invalid": "data"}`
76118
var endpoint Endpoint

0 commit comments

Comments
 (0)