Azure / Azure/azure-api-management-policy-toolkit

Variable management for policy fragments

Open
#253 0 comments 1 reaction 1 assignee Claimed by @Mielek View on GitHub
Dominant language
C#
Stars
94
Forks
36
Avg merge
2d 6m
Merged PRs (30d)
6

Description

### Proposal

Based on the article [Variable management for policy fragments](https://learn.microsoft.com/en-us/azure/api-management/fragment-variable-management) in the ms docs, it would be cool to have a functionality that can take advantage of the C# object oriented mindset to call fragments using the object itself instead of using the string identifier.

We could for example declare fragment inputs as properties decorated with [FragmentVariable], and pass values through object initializer syntax:

```csharp
// Fragment definition
[Document(Type = DocumentType.Fragment)]
public class MyFragment : IFragment
{
[FragmentVariable]
public string HeaderValue { get; set; }

public void Fragment(IFragmentContext context)
{
context.SetHeader("X-Custom", HeaderValue);
}
}

// Caller
public void Inbound(IInboundContext context)
{
context.IncludeFragment(new MyFragment { HeaderValue = "hello" });
}
```

This should compile to:
```xml


```

And inside the fragment, HeaderValue should resolve to `@(context.Variables.GetValueOrDefault("my-fragment-HeaderValue", ""))`.

[FragmentVariable] should support an optional custom variable name and a default value:
```csharp
[FragmentVariable("x-header", DefaultValue = "default")]
public string HeaderValue { get; set; }
```

This of course only works for fragments called from a certain scope (api/operation/product/...) and the variables are also defined at this scope. For variables defined in other scopes, the string identifier method is still the way to go.

### Component

Compiler

### Contact Details

jeroen@innojero.com

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.