Skip to content

Commit

Permalink
- Include @TriggerAu's contribution (#51)
Browse files Browse the repository at this point in the history
- Update Remove-NullPropertiesFromArray to ensure an array is always returned
- Add tests to verify functionality
- Fix #43

Signed-off-by: Laura Rodriguez <rdz.maria.laura@gmail.com>
  • Loading branch information
laura-rodriguez committed Oct 30, 2024
1 parent d3d8fc5 commit 4849f7a
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 22 deletions.
4 changes: 2 additions & 2 deletions Build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,11 @@ $Manifest = @{
ReleaseNotes = 'https://github.com/okta/okta-powershell-cli/releases'


ModuleVersion = '1.0.2'
ModuleVersion = '1.0.3'


RootModule = 'Okta.PowerShell.psm1'
Guid = '{74F58D24-72C5-4A88-AF6D-204B1DE897A5}' # Has to be static, otherwise each new build will be considered different module
Guid = '{5E82B81C-92D5-47BC-A10F-E26B40081903}' # Has to be static, otherwise each new build will be considered different module

PowerShellVersion = '6.2'

Expand Down
12 changes: 9 additions & 3 deletions openapi3/codegen-templates/json_helper.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,12 @@ function Remove-NullProperties {
$PropertyList = $InputObject.PSObject.Properties | Where-Object { $null -ne $_.Value }

foreach ($Property in $PropertyList) {
$NewObject[$Property.Name] = Remove-NullProperties $Property.Value
if($Property.Value -is [array]){
# explicit cast to avoid arrays to be converted to object (i.e @('foo'))
$NewObject[$Property.Name] = Remove-NullPropertiesFromArray $Property.Value
}else{
$NewObject[$Property.Name] = Remove-NullProperties $Property.Value
}
}
}

Expand Down Expand Up @@ -62,7 +67,6 @@ function Remove-NullPropertiesFromHashMap{
}

# Remove null properties from an array recursively

function Remove-NullPropertiesFromArray {
[CmdletBinding()]
param(
Expand All @@ -78,5 +82,7 @@ function Remove-NullPropertiesFromArray {
$NewArray += $NewItem
}

return $NewArray
#https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_operators?view=powershell-7.4#comma-operator-
# Ensure the return is always an array, even if there's only one item
return ,$NewArray
}
2 changes: 1 addition & 1 deletion openapi3/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"templateDir": "./codegen-templates",
"outputDir" : "../",
"inputSpec" : "./management.yaml",
"packageVersion" : "1.0.2",
"packageVersion" : "1.0.3",
"packageDescription" : "Official PowerShell CLI for the Okta API",
"packageTitle" : "Official PowerShell for the Okta API",
"packageCompany" : "Okta, Inc.",
Expand Down
21 changes: 11 additions & 10 deletions src/Okta.PowerShell/Client/JsonHelper.ps1
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#
# Okta Management
# Allows customers to easily access the Okta Management APIs
# Version: 3.0.0
# Contact: devex-public@okta.com
# Generated by OpenAPI Generator: https://openapi-generator.tech
#
#
# Okta Management
# Allows customers to easily access the Okta Management APIs
# Version: 3.0.0
# Contact: devex-public@okta.com
# Generated by OpenAPI Generator: https://openapi-generator.tech
#


# Remove null properties from an object recursively
Expand Down Expand Up @@ -35,7 +35,7 @@ function Remove-NullProperties {
foreach ($Property in $PropertyList) {
if($Property.Value -is [array]){
# explicit cast to avoid arrays to be converted to object (i.e @('foo'))
$NewObject[$Property.Name] = [array]@(Remove-NullProperties $Property.Value)
$NewObject[$Property.Name] = Remove-NullPropertiesFromArray $Property.Value
}else{
$NewObject[$Property.Name] = Remove-NullProperties $Property.Value
}
Expand Down Expand Up @@ -74,7 +74,6 @@ function Remove-NullPropertiesFromHashMap{
}

# Remove null properties from an array recursively

function Remove-NullPropertiesFromArray {
[CmdletBinding()]
param(
Expand All @@ -90,5 +89,7 @@ function Remove-NullPropertiesFromArray {
$NewArray += $NewItem
}

return $NewArray
#https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_operators?view=powershell-7.4#comma-operator-
# Ensure the return is always an array, even if there's only one item
return ,$NewArray
}
6 changes: 3 additions & 3 deletions src/Okta.PowerShell/Okta.PowerShell.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#
# Generated by: Okta, Inc.
#
# Generated on: 2024-08-16
# Generated on: 2024-10-29
#

@{
Expand All @@ -12,13 +12,13 @@
RootModule = 'Okta.PowerShell.psm1'

# Version number of this module.
ModuleVersion = '1.0.2'
ModuleVersion = '1.0.3'

# Supported PSEditions
CompatiblePSEditions = 'Core'

# ID used to uniquely identify this module
GUID = 'e5ea151f-cf29-4f2c-ac27-58b61ac36807'
GUID = '5e82b81c-92d5-47bc-a10f-e26b40081903'

# Author of this module
Author = 'Okta, Inc.'
Expand Down
2 changes: 1 addition & 1 deletion src/Okta.PowerShell/Private/OktaApiClient.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ function Invoke-OktaApiClient {
}
}

$OktaUserAgent = [Microsoft.PowerShell.Commands.PSUserAgent]::Chrome + " okta-powershell-module/1.0.2"
$OktaUserAgent = [Microsoft.PowerShell.Commands.PSUserAgent]::Chrome + " okta-powershell-module/1.0.3"


# Setting up vars for retry
Expand Down
2 changes: 1 addition & 1 deletion src/Okta.PowerShell/en-US/about_Okta.PowerShell.help.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ LONG DESCRIPTION
This PowerShell module is automatically generated by the [OpenAPI Generator](https://openapi-generator.tech) project:

- API version: 3.0.0
- SDK version: 1.0.2
- SDK version: 1.0.3
- Build package: org.openapitools.codegen.languages.PowerShellClientCodegen
For more information, please visit [https://developer.okta.com/](https://developer.okta.com/)

Expand Down
85 changes: 84 additions & 1 deletion tests/Client/JsonHelper.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ Context 'Remove-NullProperties' {
Get-Member -inputobject $CleanedJsonObject.details.children[0] -name "name" -MemberType Properties | Should -Be -Not $null
}

It 'Should maintain the same object when there''s no null values'{
It 'Should maintain the same object when there''s no null values in a single-item array'{
# GroupIds - 1 item array
$NewGroupRule = [PSObject]@{
name = "Assign users to the Sales Team"
type = "group_rule"
Expand All @@ -63,4 +64,86 @@ Context 'Remove-NullProperties' {
$CleanedRule.actions.assignUserToGroups.groupIds.Count | Should -Be 1
$CleanedRule.actions.assignUserToGroups.groupIds[0] | Should -Be "foo"
}

It 'Should maintain the same object when there''s no null values in a multiple-items array'{
# GroupIds - multiple-items array
$NewGroupRule = [PSObject]@{
name = "Assign users to the Sales Team"
type = "group_rule"
actions = [PSObject]@{
assignUserToGroups = [PSObject]@{
groupIds = [array]@('foo', 'bar', 'baz')
}
}
}

$CleanedRule = Remove-NullProperties $NewGroupRule

$CleanedRule.name | Should -Be "Assign users to the Sales Team"
$CleanedRule.type | Should -Be "group_rule"
$CleanedRule.actions.assignUserToGroups.groupIds.Count | Should -Be 3
$CleanedRule.actions.assignUserToGroups.groupIds[0] | Should -Be "foo"
$CleanedRule.actions.assignUserToGroups.groupIds[1] | Should -Be "bar"
$CleanedRule.actions.assignUserToGroups.groupIds[2] | Should -Be "baz"
}

It 'Should maintain the same object when there''s no null values in a single-item array when using initializers'{
# GroupIds - 1 item array
$GroupRuleGroupAssignment = Initialize-OktaGroupRuleGroupAssignment -GroupIds ([array]@("foo"))

$GroupRuleAction = Initialize-OktaGroupRuleAction -AssignUserToGroups $GroupRuleGroupAssignment

$GroupRuleExpression = Initialize-OktaGroupRuleExpression -Type "urn:okta:expression:1.0" -Value "user.title=='Sales Representative'"

$GroupRuleConditions = Initialize-OktaGroupRuleConditions -Expression $GroupRuleExpression

$NewGroupRule = Initialize-OktaGroupRule -Actions $GroupRuleAction -Conditions $GroupRuleConditions -Name "Assign users to the Sales Team" -Type "group_rule"

$CleanedRule = Remove-NullProperties $NewGroupRule

$CleanedRule.name | Should -Be "Assign users to the Sales Team"
$CleanedRule.type | Should -Be "group_rule"
$CleanedRule.actions.assignUserToGroups.groupIds.Count | Should -Be 1
$CleanedRule.actions.assignUserToGroups.groupIds[0] | Should -Be "foo"
}

It 'Should maintain the same object when there''s no null values in a multiple-items array when using initializers'{
# GroupIds - 1 item array
$GroupRuleGroupAssignment = Initialize-OktaGroupRuleGroupAssignment -GroupIds ([array]@("foo"))

$GroupRuleAction = Initialize-OktaGroupRuleAction -AssignUserToGroups $GroupRuleGroupAssignment

$GroupRuleExpression = Initialize-OktaGroupRuleExpression -Type "urn:okta:expression:1.0" -Value "user.title=='Sales Representative'"

$GroupRuleConditions = Initialize-OktaGroupRuleConditions -Expression $GroupRuleExpression

$NewGroupRule = Initialize-OktaGroupRule -Actions $GroupRuleAction -Conditions $GroupRuleConditions -Name "Assign users to the Sales Team" -Type "group_rule"

$CleanedRule = Remove-NullProperties $NewGroupRule

$CleanedRule.name | Should -Be "Assign users to the Sales Team"
$CleanedRule.type | Should -Be "group_rule"
$CleanedRule.actions.assignUserToGroups.groupIds.Count | Should -Be 1
$CleanedRule.actions.assignUserToGroups.groupIds[0] | Should -Be "foo"

# GroupIds - 1 multiple-items array
$GroupRuleGroupAssignment = Initialize-OktaGroupRuleGroupAssignment -GroupIds ([array]@("foo", "bar", "baz"))

$GroupRuleAction = Initialize-OktaGroupRuleAction -AssignUserToGroups $GroupRuleGroupAssignment

$GroupRuleExpression = Initialize-OktaGroupRuleExpression -Type "urn:okta:expression:1.0" -Value "user.title=='Sales Representative'"

$GroupRuleConditions = Initialize-OktaGroupRuleConditions -Expression $GroupRuleExpression

$NewGroupRule = Initialize-OktaGroupRule -Actions $GroupRuleAction -Conditions $GroupRuleConditions -Name "Assign users to the Sales Team" -Type "group_rule"

$CleanedRule = Remove-NullProperties $NewGroupRule

$CleanedRule.name | Should -Be "Assign users to the Sales Team"
$CleanedRule.type | Should -Be "group_rule"
$CleanedRule.actions.assignUserToGroups.groupIds.Count | Should -Be 3
$CleanedRule.actions.assignUserToGroups.groupIds[0] | Should -Be "foo"
$CleanedRule.actions.assignUserToGroups.groupIds[1] | Should -Be "bar"
$CleanedRule.actions.assignUserToGroups.groupIds[2] | Should -Be "baz"
}
}

0 comments on commit 4849f7a

Please sign in to comment.