Skip to content

Commit 24509fc

Browse files
MintsIncclaude
andauthored
Add path parameter support for undo operations (#3623)
Support extracting path parameters from request URLs for use in undo operations. This enables cleanup of resources identified by path parameters (e.g., /api/v2/resource/{id}). Co-authored-by: Claude Sonnet 4.5 <noreply@anthropic.com>
1 parent a0664aa commit 24509fc

3 files changed

Lines changed: 61 additions & 0 deletions

File tree

tests/scenarios/actions.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,34 @@ func GetRequestsUndo(ctx gobdd.Context, version string, operationID string) (fun
343343
}
344344
}
345345
sourceJSONData = requestJSON
346+
} else if *undo.Undo.Parameters[i-1].Origin == "path" {
347+
// Extract from stored path parameters
348+
pathParams := GetPathParameters(ctx)
349+
if undo.Undo.Parameters[i-1].Source != nil {
350+
paramName := *undo.Undo.Parameters[i-1].Source
351+
if val, ok := pathParams[paramName]; ok {
352+
// Convert the stored value to the expected type
353+
object := reflect.New(undoOperation.Type().In(i))
354+
sourceJSON, err := datadog.Marshal(val)
355+
if err != nil {
356+
t.Fatalf("Error marshalling path parameter '%s': %v", paramName, err)
357+
return
358+
}
359+
err = datadog.Unmarshal(sourceJSON, object.Interface())
360+
if err != nil {
361+
t.Fatalf("Error unmarshalling path parameter '%s': %v", paramName, err)
362+
return
363+
}
364+
in[i] = object.Elem()
365+
continue // Skip template/source processing for path parameters
366+
} else {
367+
t.Fatalf("Path parameter '%s' not found", paramName)
368+
return
369+
}
370+
} else {
371+
t.Fatalf("Path origin requires 'source' field")
372+
return
373+
}
346374
} else if *undo.Undo.Parameters[i-1].Origin == "response" {
347375
sourceJSONData = responseJSON
348376
}

tests/scenarios/scenarios.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ type jsonResponseKey struct{}
8383
type dataKey struct{}
8484
type cleanupKey struct{}
8585
type pathParamCountKey struct{}
86+
type pathParametersKey struct{}
8687

8788
// GetIgnoredTags returns list of ignored tags.
8889
func GetIgnoredTags() []string {
@@ -326,6 +327,29 @@ func SetData(ctx gobdd.Context, value map[string]interface{}) {
326327
ctx.Set(dataKey{}, value)
327328
}
328329

330+
// GetPathParameters returns stored path parameters by name.
331+
func GetPathParameters(ctx gobdd.Context) map[string]interface{} {
332+
c, err := ctx.Get(pathParametersKey{})
333+
if err != nil {
334+
params := make(map[string]interface{})
335+
ctx.Set(pathParametersKey{}, params)
336+
return params
337+
}
338+
return c.(map[string]interface{})
339+
}
340+
341+
// SetPathParameter stores a path parameter.
342+
func SetPathParameter(ctx gobdd.Context, name string, value interface{}) {
343+
params := GetPathParameters(ctx)
344+
params[name] = value
345+
ctx.Set(pathParametersKey{}, params)
346+
}
347+
348+
// ClearPathParameters clears stored path parameters.
349+
func ClearPathParameters(ctx gobdd.Context) {
350+
ctx.Set(pathParametersKey{}, make(map[string]interface{}))
351+
}
352+
329353
// GetRequestParameters helps to build a request.
330354
func GetRequestParameters(ctx gobdd.Context) map[string]interface{} {
331355
c, err := ctx.Get(requestParamsKey{})

tests/scenarios/step_definitions.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ func newRequest(t gobdd.StepTest, ctx gobdd.Context, name string) {
8989
ctx.Set(requestParamsKey{}, make(map[string]interface{}))
9090
ctx.Set(requestArgsKey{}, make([]interface{}, 0))
9191
ctx.Set(pathParamCountKey{}, 1)
92+
ClearPathParameters(ctx)
9293

9394
}
9495
func statusIs(t gobdd.StepTest, ctx gobdd.Context, expected int, text string) {
@@ -141,9 +142,13 @@ func addParameterFrom(t gobdd.StepTest, ctx gobdd.Context, name string, path str
141142
datadog.Unmarshal(data, varType.Interface())
142143
GetRequestParameters(ctx)[name] = varType.Elem()
143144
ctx.Set(requestArgsKey{}, append(GetRequestArguments(ctx), varType.Elem()))
145+
// Store path parameter for undo operations
146+
SetPathParameter(ctx, name, varType.Elem().Interface())
144147
} else {
145148
GetRequestParameters(ctx)[name] = value
146149
ctx.Set(requestArgsKey{}, append(GetRequestArguments(ctx), value))
150+
// Store path parameter for undo operations
151+
SetPathParameter(ctx, name, value.Interface())
147152
}
148153
}
149154

@@ -159,6 +164,10 @@ func addPathArgumentWithValue(t gobdd.StepTest, ctx gobdd.Context, param string,
159164
ctx.Set(pathParamCountKey{}, pathCount.(int)+1)
160165

161166
templatedValue := Templated(t, GetData(ctx), value)
167+
168+
// Store path parameter by name for undo operations
169+
SetPathParameter(ctx, param, templatedValue)
170+
162171
var varType reflect.Value
163172

164173
if request.Type().IsVariadic() && pathCount.(int) > numArgs-2 {

0 commit comments

Comments
 (0)