Skip to content

Commit

Permalink
Add JsonPCompiler
Browse files Browse the repository at this point in the history
  • Loading branch information
Mielek committed Nov 1, 2024
1 parent 328e6d5 commit 52bc52c
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/Authoring/IOutboundContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,12 @@ public interface IOutboundContext : IHaveExpressionContext
/// </summary>
/// <param name="config"></param>
void JsonToXml(JsonToXmlConfig config);

/// <summary>
/// TODO
/// </summary>
/// <param name="callbackParameterName"></param>
void JsonP(string callbackParameterName);

/// <summary>
/// Inlines the specified policy as is to policy document.
Expand Down
1 change: 1 addition & 0 deletions src/Core/Compilation/CSharpPolicyCompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public CSharpPolicyCompiler(ClassDeclarationSyntax document)
new InlinePolicyCompiler(),
new IpFilterCompiler(),
new JsonToXmlCompiler(),
new JsonPCompiler(),
new MockResponseCompiler(),
new QuotaCompiler(),
new RateLimitByKeyCompiler(),
Expand Down
25 changes: 25 additions & 0 deletions src/Core/Compilation/Policy/JsonPCompiler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System.Xml.Linq;

using Microsoft.CodeAnalysis.CSharp.Syntax;

using Mielek.Azure.ApiManagement.PolicyToolkit.Authoring;

namespace Mielek.Azure.ApiManagement.PolicyToolkit.Compilation.Policy;

public class JsonPCompiler : IMethodPolicyHandler
{
public string MethodName => nameof(IOutboundContext.JsonP);

public void Handle(ICompilationContext context, InvocationExpressionSyntax node)
{
var arguments = node.ArgumentList.Arguments;
if (arguments.Count != 1)
{
context.ReportError($"Wrong argument count for jsonp policy. {node.GetLocation()}");
return;
}

var value = node.ArgumentList.Arguments[0].Expression.ProcessParameter(context);
context.AddPolicy(new XElement("jsonp", new XAttribute("callback-parameter-name", value)));
}
}
51 changes: 51 additions & 0 deletions test/Test.Core/Compilation/JsonPTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
namespace Mielek.Azure.ApiManagement.PolicyToolkit.Compilation;

[TestClass]
public class JsonPTests
{
[TestMethod]
[DataRow(
"""
[Document]
public class PolicyDocument : IDocument
{
public void Outbound(IOutboundContext context) {
context.JsonP("cb");
}
}
""",
"""
<policies>
<outbound>
<jsonp callback-parameter-name="cb" />
</outbound>
</policies>
""",
DisplayName = "Should compile jsonp policy"
)]
[DataRow(
"""
[Document]
public class PolicyDocument : IDocument
{
public void Outbound(IOutboundContext context) {
context.JsonP(Exp(context.ExpressionContext));
}
public string Exp(IExpressionContext context) => context.User.Name;
}
""",
"""
<policies>
<outbound>
<jsonp callback-parameter-name="@(context.User.Name)" />
</outbound>
</policies>
""",
DisplayName = "Should compile jsonp policy with expression"
)]
public void ShouldCompileJsonPPolicy(string code, string expectedXml)
{
code.CompileDocument().Should().BeSuccessful().And.DocumentEquivalentTo(expectedXml);
}

}

0 comments on commit 52bc52c

Please sign in to comment.