diff --git a/PSSwagger/PSSwagger.Resources.psd1 b/PSSwagger/PSSwagger.Resources.psd1 index 5b3b8f5..276c238 100644 --- a/PSSwagger/PSSwagger.Resources.psd1 +++ b/PSSwagger/PSSwagger.Resources.psd1 @@ -87,5 +87,6 @@ ConvertFrom-StringData @' InvalidPSMetaFlattenParameter=Flatten property is specified as 'true' for an invalid parameter '{0}' with type '{1}'. InvalidHeaderFileExtension=Header '{0}' file extension should be '.txt'. InvalidHeaderFilePath=The specified value '{0}' for Header parameter is should be a valid file path. + HeaderContentTwoHyphenWarning=The specified Header content has '--', replacing '--' with '=='. ###PSLOC '@ \ No newline at end of file diff --git a/PSSwagger/PSSwagger.psm1 b/PSSwagger/PSSwagger.psm1 index c4308d4..3c94963 100644 --- a/PSSwagger/PSSwagger.psm1 +++ b/PSSwagger/PSSwagger.psm1 @@ -1114,6 +1114,14 @@ function Get-HeaderContent { } } + # Escape block comment character sequence, if any, using the PowerShell escape character, grave-accent(`). + $HeaderContent = $HeaderContent.Replace('<#', '<`#').Replace('#>', '#`>') + + if ($HeaderContent -match '--') { + Write-Warning -Message $LocalizedData.HeaderContentTwoHyphenWarning + $HeaderContent = $HeaderContent.Replace('--', '==') + } + return $HeaderContent } diff --git a/Tests/PSSwagger.Unit.Tests.ps1 b/Tests/PSSwagger.Unit.Tests.ps1 index 8428b1f..31e272a 100644 --- a/Tests/PSSwagger.Unit.Tests.ps1 +++ b/Tests/PSSwagger.Unit.Tests.ps1 @@ -266,6 +266,24 @@ Describe "PSSwagger Unit Tests" -Tag @('BVT', 'DRT', 'UnitTest', 'P0') { It "Get-HeaderContent should return MICROSOFT_APACHE_NO_CODEGEN header content with '-Header MICROSOFT_APACHE_NO_CODEGEN'" { Get-HeaderContent -SwaggerDict @{Info = @{Header = 'MICROSOFT_APACHE_NO_CODEGEN'}} | Should BeExactly $MicrosoftApacheLicenseHeader } + + It 'Get-HeaderContent should escape <#' { + Get-HeaderContent -SwaggerDict @{Info = @{Header = 'Header content with <#'}} | Should BeExactly 'Header content with <`#' + } + + It 'Get-HeaderContent should escape #>' { + Get-HeaderContent -SwaggerDict @{Info = @{Header = 'Header content with #>'}} | Should BeExactly 'Header content with #`>' + } + + It 'Get-HeaderContent should replace -- with ==' { + Get-HeaderContent -SwaggerDict @{Info = @{Header = 'Header content with --'}} -WarningVariable wv -WarningAction SilentlyContinue | Should BeExactly 'Header content with ==' + $wv | Should not BeNullOrEmpty + $wv.Message -match '==' | Should Be $true + } + + It "Get-HeaderContent should escape '<#' and '#>', and replace '--' with '=='" { + Get-HeaderContent -SwaggerDict @{Info = @{Header = 'Header content with <# PS comment #> and --.'}} -WarningAction SilentlyContinue | Should BeExactly 'Header content with <`# PS comment #`> and ==.' + } } Context "Get-CSharpModelName Unit Tests" {