policy-compilation
Skill95 starsChanged 12 months agoMITView on GitHub
Conventions and patterns for creating policy compilers in the Azure API Management policy toolkit. Use this skill when creating or modifying compiler classes in src/Core/Compiling/Policy/.
In this repository6 other files it gives its agents.
- .github/copilot-instructions.mdCopilot instructions
- policy-authoringSkill
- policy-codebase-referenceSkill
- policy-emulatorSkill
- policy-emulator-testingSkill
- policy-testingSkill
The file
We split this skill into the rules it gives an agent
4 rules. About 0 are common, 2 are distinctive, and 2 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈2
Distinctive · estimate
This author's own choices. Worth a look.
2
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Distinctive: line 14
Use this for policies that take a config object parameter.
- Very specific: line 67
Use this for policies with 1–2 simple parameters and no optional fields (e.g., SetMethod, FindAndReplace).
- Very specific: line 104
All utility methods live in src/Core/Compiling/CompilerUtils.cs and src/Core/Compiling/SyntaxExtensions.cs.
- Distinctive: line 187
If a specific policy requires namespace prefixes or explicit schema references, this must be specified during authoring Phase 1.
≈ “Common” is an estimate: at least 1 in 5 of the 213,895 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Policy Compilation Patterns
This skill describes how to implement compilers that convert authoring models to XML in the Azure API Management policy toolkit.
Compiler Class — src/Core/Compiling/Policy/{PolicyName}Compiler.cs
Config-Based Compiler Template (Most Common)
Use this for policies that take a config object parameter.
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System.Xml.Linq;
using Microsoft.Azure.ApiManagement.PolicyToolkit.Authoring;
using Microsoft.Azure.ApiManagement.PolicyToolkit.Compiling.Diagnostics;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;
namespace Microsoft.Azure.ApiManagement.PolicyToolkit.Compiling.Policy;
public class {PolicyName}Compiler : IMethodPolicyHandler
{
public string MethodName => nameof(I{Section}Context.{MethodName});
public void Handle(IDocumentCompilationContext context, InvocationExpressionSyntax node)
{
if (!node.TryExtractingConfigParameter<{PolicyName}Config>(context, "{xml-element-name}", out var values))
{
return;
}
var element = new XElement("{xml-element-name}");
// Required attributes — report diagnostic and return on failure
if (!element.AddAttribute(values, nameof({PolicyName}Config.{RequiredProp}), "{xml-attr-name}"))
{
context.Report(Diagnostic.Create(
CompilationErrors.RequiredParameterNotDefined,
node.GetLocation(),
"{xml-element-name}",
nameof({PolicyName}Config.{RequiredProp})
));
return;
}
// Optional attributes — no error reporting needed
element.AddAttribute(values, nameof({PolicyName}Config.{OptionalProp}), "{xml-attr-name}");
// Child elements — iterate sub-configs if applicable
// (see "Handling Child Elements" below)
context.AddPolicy(element);
}
}Direct-Parameter Compiler Template
Use this for policies with 1–2 simple parameters and no optional fields (e.g., SetMethod, FindAndReplace).
public class {PolicyName}Compiler : IMethodPolicyHandler
{
public string MethodName => nameof(I{Section}Context.{MethodName});
public void Handle(IDocumentCompilationContext context, InvocationExpressionSyntax node)
{
if (node.ArgumentList.Arguments.Count != {expectedCount})
{
context.Report(Diagnostic.Create(
CompilationErrors.ArgumentCountMissMatchForPolicy,
node.ArgumentList.GetLocation(),
"{xml-element-name}"));
return;
}
var value = node.ArgumentList.Arguments[0].Expression.ProcessParameter(context);
context.AddPolicy(new XElement("{xml-element-name}", value));
}
}Auto-Registration
Compiler classes do not need manual DI registration. The CompilerModule.AddMethodPolicyHandlers() method in src/Core/IoC/CompilerModule.cs uses reflection to find all types matching:
IsClass: trueIsAbstract: falseIsPublic: trueNamespace: "Microsoft.Azure.ApiManagement.PolicyToolkit.Compiling.Policy"- Implements
IMethodPolicyHandler
Just ensure your compiler class meets all these criteria and it will be auto-registered.
Key Utility Methods
All utility methods live in src/Core/Compiling/CompilerUtils.cs and src/Core/Compiling/SyntaxExtensions.cs. You do not need to modify these files — just use their APIs.
TryExtractingConfigParameter<T>
Extracts the config object from the method invocation. Returns a dictionary of property name → InitializerValue. Returns false and reports diagnostics if extraction fails.
AddAttribute
Extension method on XElement. Adds an XML attribute from the values dictionary:
- Property name to XML attribute conversion: C# property names are converted to kebab-case XML attribute names automatically. For example:
RenewalPeriod→renewal-periodRetryAfterHeaderName→retry-after-header-name- The conversion happens in the
AddAttributehelper; you supply the kebab-case name in the compiler call.
- Expression handling: Automatically wraps expression values in
@(...)when the value comes from an expression method. - Return value:
trueif the attribute was added,falseif the key was not present (property not set).
GenericCompiler.HandleList
Utility in src/Core/Compiling/Policy/GenericCompiler.cs for compiling string arrays into repeated child elements:
GenericCompiler.HandleList(element, values, nameof(Config.Claims), "required-claims", "claim");
// Produces: <required-claims><claim>value1</claim><claim>value2</claim></required-claims>Used by ValidateJwtCompiler and ValidateAzureAdTokenCompiler.
ProcessParameter
Extension method on ExpressionSyntax. Processes a raw argument into its string value, handling both constants and expressions. Used in direct-parameter compilers.
Handling Child Elements
Typed Sub-Config Arrays
For child elements backed by sub-config records (e.g., ApiRateLimit[]):
if (values.TryGetValue(nameof({PolicyName}Config.{ChildArray}), out var items))
{
foreach (var item in items.UnnamedValues!)
{
var childElement = new XElement("{child-element-name}");
var childValues = item.NamedValues!;
childElement.AddAttribute(childValues, nameof({SubConfig}.{Prop}), "{attr-name}");
// ... more attributes
element.Add(childElement);
}
}String Arrays
For simple string arrays (e.g., IP addresses):
if (values.TryGetValue(nameof({PolicyName}Config.{StringArray}), out var items))
{
foreach (var item in items.UnnamedValues!)
{
element.Add(new XElement("{child-element-name}", item.Value!));
}
}Diagnostics
Error reporting uses descriptors from src/Core/Compiling/Diagnostics/CompilationErrors.cs. Common ones:
| Descriptor | When to Use |
RequiredParameterNotDefined | A required attribute/property is missing |
RequiredParameterIsEmpty | A required array is present but empty |
AtLeastOneOfTwoShouldBeDefined | At least one of two mutually supportive properties must be set |
OnlyOneOfTwoShouldBeDefined | Exactly one of two mutually exclusive properties must be set |
PolicyArgumentIsNotOfRequiredType | A child element has the wrong type |
ArgumentCountMissMatchForPolicy | Wrong number of arguments in direct-parameter compilers |
XML Namespaces
- By default, compiled XML policies do not include
xmlnsattributes. XML elements use the element name directly (e.g.,<rate-limit />), and the namespace context is managed at the gateway level. - If a specific policy requires namespace prefixes or explicit schema references, this must be specified during authoring Phase 1. Handle with care and test thoroughly.
IReturnValueMethodPolicyHandler
A second compiler interface exists for policies that return a value assigned to a variable (e.g., AuthenticationManagedIdentity returning a token). This interface is not auto-registered by CompilerModule and the syntax compiler for it (LocalDeclarationStatementCompiler) is currently commented out in CompilerModule.cs. If you encounter a policy that assigns its result to a variable, flag this to the user as a special case requiring manual wiring.
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

