From 4849f7a4d8b9252962731f14a231e1a05353dfc7 Mon Sep 17 00:00:00 2001 From: Laura Rodriguez Date: Tue, 29 Oct 2024 14:44:16 -0400 Subject: [PATCH] - Include @TriggerAu's contribution (#51) - Update Remove-NullPropertiesFromArray to ensure an array is always returned - Add tests to verify functionality - Fix #43 Signed-off-by: Laura Rodriguez --- Build.ps1 | 4 +- .../codegen-templates/json_helper.mustache | 12 ++- openapi3/config.json | 2 +- src/Okta.PowerShell/Client/JsonHelper.ps1 | 21 ++--- src/Okta.PowerShell/Okta.PowerShell.psd1 | 6 +- src/Okta.PowerShell/Private/OktaApiClient.ps1 | 2 +- .../en-US/about_Okta.PowerShell.help.txt | 2 +- tests/Client/JsonHelper.Tests.ps1 | 85 ++++++++++++++++++- 8 files changed, 112 insertions(+), 22 deletions(-) diff --git a/Build.ps1 b/Build.ps1 index 331aadf..a32dea2 100644 --- a/Build.ps1 +++ b/Build.ps1 @@ -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' diff --git a/openapi3/codegen-templates/json_helper.mustache b/openapi3/codegen-templates/json_helper.mustache index 1dddb52..7dc0e61 100644 --- a/openapi3/codegen-templates/json_helper.mustache +++ b/openapi3/codegen-templates/json_helper.mustache @@ -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 + } } } @@ -62,7 +67,6 @@ function Remove-NullPropertiesFromHashMap{ } # Remove null properties from an array recursively - function Remove-NullPropertiesFromArray { [CmdletBinding()] param( @@ -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 } \ No newline at end of file diff --git a/openapi3/config.json b/openapi3/config.json index 57008d3..3da7c0c 100644 --- a/openapi3/config.json +++ b/openapi3/config.json @@ -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.", diff --git a/src/Okta.PowerShell/Client/JsonHelper.ps1 b/src/Okta.PowerShell/Client/JsonHelper.ps1 index 18bb668..4f207ed 100644 --- a/src/Okta.PowerShell/Client/JsonHelper.ps1 +++ b/src/Okta.PowerShell/Client/JsonHelper.ps1 @@ -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 @@ -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 } @@ -74,7 +74,6 @@ function Remove-NullPropertiesFromHashMap{ } # Remove null properties from an array recursively - function Remove-NullPropertiesFromArray { [CmdletBinding()] param( @@ -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 } \ No newline at end of file diff --git a/src/Okta.PowerShell/Okta.PowerShell.psd1 b/src/Okta.PowerShell/Okta.PowerShell.psd1 index 4202979..8a2d0fa 100644 --- a/src/Okta.PowerShell/Okta.PowerShell.psd1 +++ b/src/Okta.PowerShell/Okta.PowerShell.psd1 @@ -3,7 +3,7 @@ # # Generated by: Okta, Inc. # -# Generated on: 2024-08-16 +# Generated on: 2024-10-29 # @{ @@ -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.' diff --git a/src/Okta.PowerShell/Private/OktaApiClient.ps1 b/src/Okta.PowerShell/Private/OktaApiClient.ps1 index 025889b..3f064e2 100644 --- a/src/Okta.PowerShell/Private/OktaApiClient.ps1 +++ b/src/Okta.PowerShell/Private/OktaApiClient.ps1 @@ -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 diff --git a/src/Okta.PowerShell/en-US/about_Okta.PowerShell.help.txt b/src/Okta.PowerShell/en-US/about_Okta.PowerShell.help.txt index d429370..3da90d0 100644 --- a/src/Okta.PowerShell/en-US/about_Okta.PowerShell.help.txt +++ b/src/Okta.PowerShell/en-US/about_Okta.PowerShell.help.txt @@ -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/) diff --git a/tests/Client/JsonHelper.Tests.ps1 b/tests/Client/JsonHelper.Tests.ps1 index 05f4e53..9d22799 100644 --- a/tests/Client/JsonHelper.Tests.ps1 +++ b/tests/Client/JsonHelper.Tests.ps1 @@ -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" @@ -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" + } } \ No newline at end of file