Skip to content

Commit acda9bd

Browse files
committed
fix: Endpoint configuration should also accept expressions
Signed-off-by: Giovanny Gutierrez <[email protected]>
1 parent ff500a0 commit acda9bd

File tree

2 files changed

+73
-7
lines changed

2 files changed

+73
-7
lines changed

model/endpoint.go

+31-7
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,9 @@ func (u *LiteralUri) String() string {
8686
}
8787

8888
type EndpointConfiguration struct {
89-
URI URITemplate `json:"uri" validate:"required"`
90-
Authentication *ReferenceableAuthenticationPolicy `json:"authentication,omitempty"`
89+
RuntimeExpression *RuntimeExpression `json:"-"`
90+
URI URITemplate `json:"-"`
91+
Authentication *ReferenceableAuthenticationPolicy `json:"authentication,omitempty"`
9192
}
9293

9394
// UnmarshalJSON implements custom unmarshalling for EndpointConfiguration.
@@ -107,12 +108,35 @@ func (e *EndpointConfiguration) UnmarshalJSON(data []byte) error {
107108

108109
// Unmarshal the URI field into the appropriate URITemplate implementation
109110
uri, err := UnmarshalURITemplate(temp.URI)
110-
if err != nil {
111-
return fmt.Errorf("invalid URI in EndpointConfiguration: %w", err)
111+
if err == nil {
112+
e.URI = uri
113+
return nil
114+
}
115+
116+
var runtimeExpr RuntimeExpression
117+
if err := json.Unmarshal(temp.URI, &runtimeExpr); err == nil && runtimeExpr.IsValid() {
118+
e.RuntimeExpression = &runtimeExpr
119+
return nil
112120
}
113-
e.URI = uri
114121

115-
return nil
122+
return errors.New("failed to unmarshal EndpointConfiguration: data does not match any known schema")
123+
}
124+
125+
// MarshalJSON implements custom marshalling for Endpoint.
126+
func (e *EndpointConfiguration) MarshalJSON() ([]byte, error) {
127+
m := make(map[string]interface{})
128+
if e.Authentication != nil {
129+
m["authentication"] = e.Authentication
130+
}
131+
132+
if e.RuntimeExpression != nil {
133+
m["uri"] = e.RuntimeExpression
134+
} else if e.URI != nil {
135+
m["uri"] = e.URI
136+
}
137+
138+
// Return an empty JSON object when no fields are set
139+
return json.Marshal(m)
116140
}
117141

118142
type Endpoint struct {
@@ -158,7 +182,7 @@ func (e *Endpoint) UnmarshalJSON(data []byte) error {
158182
return nil
159183
}
160184

161-
// Finally, try to unmarshal as EndpointConfiguration
185+
// Finally, try to unmarshal as EndpointConfigurati
162186
var endpointConfig EndpointConfiguration
163187
if err := json.Unmarshal(data, &endpointConfig); err == nil {
164188
e.EndpointConfig = &endpointConfig

model/endpoint_test.go

+42
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)