Skip to content

Commit

Permalink
Fixes #1090 (#1108)
Browse files Browse the repository at this point in the history
  • Loading branch information
adamdriscoll authored Sep 12, 2019
1 parent 800cbdd commit a394a3c
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/UniversalDashboard.UITest/Assets/requires.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
using module Microsoft.PowerShell.Utility
'Hello' | ConvertTo-Json
18 changes: 18 additions & 0 deletions src/UniversalDashboard.UITest/Integration/Api.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,24 @@ Import-Module $ModulePath -Force
Get-UDRestApi | Stop-UDRestApi
Get-UDDashboard | Stop-UDDashboard
Describe "Api" {

Context "script block with requires" {
$Server = Start-UDRestApi -Port 10001 -Endpoint @(

$ScriptBlock = [ScriptBlock]::Create((Get-Content (Join-Path $PSScriptRoot "../Assets/requires.txt") -Raw))

New-UDEndpoint -Url "/scriptblock" -Method "GET" -Endpoint $ScriptBlock
)

It "not throw an error" {
(Invoke-RestMethod http://localhost:10001/api/scriptblock?hello=test) | Should be "Hello"
}

Stop-UDRestApi $Server
}

Wait-Debugger

Context "Special Characters" {
$Server = Start-UDRestApi -Port 10001 -Endpoint @(
New-UDEndpoint -Url "/recherches" -Method "GET" -Endpoint {
Expand Down
11 changes: 10 additions & 1 deletion src/UniversalDashboard/Execution/ExecutionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,15 @@ public object ExecuteEndpoint(ExecutionContext context, Endpoint endpoint)
}

var paramBlockBuilder = new StringBuilder();

if (scriptBlockAst.UsingStatements != null)
{
foreach(var usingStatement in scriptBlockAst.UsingStatements)
{
paramBlockBuilder.AppendLine(usingStatement.ToString());
}
}

paramBlockBuilder.Append("param(");

foreach (var parameter in context.Parameters)
Expand All @@ -84,7 +93,7 @@ public object ExecuteEndpoint(ExecutionContext context, Endpoint endpoint)
paramBlockBuilder.Append(")");
paramBlockBuilder.AppendLine();

script = paramBlockBuilder.ToString() + script;
script = paramBlockBuilder.ToString() + scriptBlockAst.EndBlock.ToString();
}

Collection<PSObject> output;
Expand Down

0 comments on commit a394a3c

Please sign in to comment.