Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions src/Microsoft.OpenApi/Expressions/CompositeExpression.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace Microsoft.OpenApi.Expressions
{
/// <summary>
/// String literal with embedded expressions
/// </summary>
public class CompositeExpression : RuntimeExpression
{
private readonly string template;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

template [](start = 32, length = 8)

not sure if template is the right word here... maybe "originalExpression" ?

private Regex expressionPattern = new Regex("{(?<exp>[^}]+)");
/// <summary>

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit, please add one newline for readability

/// Expressions embedded into string literal
/// </summary>
public List<RuntimeExpression> ContainedExpressions = new List<RuntimeExpression>();

/// <summary>
/// Create a composite expression from a string literal with an embedded expression
/// </summary>
/// <param name="expression"></param>
public CompositeExpression(string expression)
{
template = expression;

// Extract subexpressions and convert to RuntimeExpressions
var matches = expressionPattern.Matches(expression);

foreach (var item in matches.Cast<Match>())
{
var value = item.Groups["exp"].Captures.Cast<Capture>().First().Value;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

value [](start = 20, length = 5)

nit, call this runtimeExpression instead of value

ContainedExpressions.Add(RuntimeExpression.Build(value));
}
}

/// <summary>
/// Return original string literal with embedded expression
/// </summary>
public override string Expression => template;
}
}
5 changes: 5 additions & 0 deletions src/Microsoft.OpenApi/Expressions/RuntimeExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ public static RuntimeExpression Build(string expression)
throw Error.ArgumentNullOrWhiteSpace(nameof(expression));
}

if (expression.Contains("{$"))
{
return new CompositeExpression(expression);
}

if (!expression.StartsWith(Prefix))
{
throw new OpenApiException(string.Format(SRResource.RuntimeExpressionMustBeginWithDollar, expression));
Expand Down
79 changes: 79 additions & 0 deletions test/Microsoft.OpenApi.Tests/Expressions/RuntimeExpressionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Microsoft.OpenApi.Expressions;
using Microsoft.OpenApi.Properties;
using System;
using System.Linq;
using Xunit;

namespace Microsoft.OpenApi.Tests.Writers
Expand Down Expand Up @@ -171,5 +172,83 @@ public void CompareRuntimeExpressionWorks(string expression)
Assert.NotSame(runtimeExpression1, runtimeExpression2);
Assert.Equal(runtimeExpression1, runtimeExpression2);
}


[Fact]
public void CompositeRuntimeExpressionContainsExpression()
{
// Arrange
string expression = "This is a composite expression {$url} yay";

// Act
var runtimeExpression = RuntimeExpression.Build(expression);

// Assert
Assert.NotNull(runtimeExpression);
var response = Assert.IsType<CompositeExpression>(runtimeExpression);
Assert.Equal(expression, response.Expression);

var compositeExpression = runtimeExpression as CompositeExpression;
Assert.Single(compositeExpression.ContainedExpressions);

}

[Fact]
public void CompositeRuntimeExpressionContainsMultipleExpressions()
{
// Arrange
string expression = "This is a composite expression {$url} yay and {$request.header.foo}";

// Act
var runtimeExpression = RuntimeExpression.Build(expression);

// Assert
Assert.NotNull(runtimeExpression);
var response = Assert.IsType<CompositeExpression>(runtimeExpression);
Assert.Equal(expression, response.Expression);

var compositeExpression = runtimeExpression as CompositeExpression;
Assert.Equal(2,compositeExpression.ContainedExpressions.Count);

Assert.IsType<UrlExpression>(compositeExpression.ContainedExpressions.First());
Assert.IsType<RequestExpression>(compositeExpression.ContainedExpressions.Last());
}



[Fact]
public void CompositeRuntimeExpressionForWebHook()
{
// Arrange
string expression = "http://notificationServer.com?transactionId={$request.body#/id}&email={$request.body#/email}";

// Act
var runtimeExpression = RuntimeExpression.Build(expression);

// Assert
Assert.NotNull(runtimeExpression);
var response = Assert.IsType<CompositeExpression>(runtimeExpression);
Assert.Equal(expression, response.Expression);

var compositeExpression = runtimeExpression as CompositeExpression;
Assert.Equal(2, compositeExpression.ContainedExpressions.Count);

Assert.IsType<RequestExpression>(compositeExpression.ContainedExpressions.First());
Assert.IsType<RequestExpression>(compositeExpression.ContainedExpressions.Last());
}

[Theory]
[InlineData("This is a composite expression yay and {} and {$sddsd}")]
[InlineData("This is a composite expression {url} yay and {} and {$url}")]
public void CompositeRuntimeExpressionContainsInvalidExpressions(string expression)
{
// Arrange

// Act
Action test = () => RuntimeExpression.Build(expression);

// Assert
Assert.Throws<OpenApiException>(test);
}
}
}