Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inner Middleware function output forces to return $true #1431

Open
ondsor opened this issue Oct 23, 2024 · 1 comment
Open

Inner Middleware function output forces to return $true #1431

ondsor opened this issue Oct 23, 2024 · 1 comment

Comments

@ondsor
Copy link

ondsor commented Oct 23, 2024

Using custom functions in middleware, which return an Write-Output seems to force Middleware to return $true, even through there is explicit return $false statement in the Logic. Forwarding function to Out-Null resolves the isssue.
Is this expected behaviour?

Environment:
Pode v2.11.0
PSVersion 7.4.2
Microsoft Windows 10.0.20348

Replication data:
----- Custom function ----

function Test-WriteMiddlewareoutput ($a) {
    Write-Output -InputObject $a 
}
function Test-WriteMiddlewareToFile ($path) {
    'Test' | Out-File -LiteralPath $path -Force
}

------ Server code --------

Import-PodeModule -Path '.\scripts\innerfunction.ps1'
Start-PodeServer -Threads 1 {
    
    $Config = Get-PodeConfig
    $state:Port = $Config.Port
    Add-PodeEndpoint -Address . -Port $state:Port -Protocol Http
    New-PodeLoggingMethod -Terminal | Enable-PodeErrorLogging
  
    Add-PodeMiddleware -Name Write-testoutput -Route '/' -ScriptBlock  {
        Write-PodeHost "Should return false -output"
        test-WriteMiddlewareoutput 'Hello World'

        return $false
    }

    Add-PodeMiddleware -Name stillProcessing -Route '/' -ScriptBlock  {
        Write-PodeHost "Should return  false -toFIle"
        Test-WriteMiddlewareToFile C:\temp\file.log
        return $false
    }

    Add-PodeRoute -Method Post -Path '/' -ContentType 'application/json' -ScriptBlock {
        "Destination Route" | Out-PodeHost
    }
}

-------------- API call ------

$port = '8084'
$route = ""
 $body = '{
	"request_type": "Request"
}'
$response = invoke-webrequest -Uri http:\\localhost:$port/$route -Method Post -Body $body -ContentType 'application/json'

------ Pode Host -----
pode start
Pode v2.11.0 (PID: 8108)
Listening on the following 1 endpoint(s) [1 thread(s)]:
- http://localhost:8084/
Should return false -output
Should return false -toFIle

Thank you very much for your answer.
Best Regards,
Ondrej

@Badgerati
Copy link
Owner

Hi @ondsor,

This is expected, Write-Output returns data back up the pipe; so even though there is a $false later on, the function is actually returning $a and $false, which will be seen as an array of items and treated as $true - i.e. it's a non-empty array of items.

Doing Write-Output -InputObject $a | Out-Null will fix it, as you've started. You could also use Out-Default or Out-PodeHost:

$a | Out-Default
$a | Out-PodeHost

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants