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

[BUG][powershell] Reusable enum schemas generates invalid .ps1 files #8316

Open
4 of 6 tasks
dordadush opened this issue Jan 4, 2021 · 3 comments
Open
4 of 6 tasks

Comments

@dordadush
Copy link
Contributor

dordadush commented Jan 4, 2021

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

I'm defining a reusable enum schema in my OpenAPI 3 spec file and reuse this schema in other schema fields to avoid writing the same enum values multiple times.

This results in generated .ps1 files with an empty expression which PowerShell does not accept:

function ConvertFrom-JsonToApplicationType {
    Param(
        [AllowEmptyString()]
        [string]$Json
    )

    Process {
        'Converting JSON to PSCustomObject: PSOpenAPITools => ApplicationType' | Write-Debug
        $PSBoundParameters | Out-DebugParameter | Write-Debug

        $JsonParameters = ConvertFrom-Json -InputObject $Json

        # check if Json contains properties not defined in ApplicationType
        $AllProperties = ()
        foreach ($name in $JsonParameters.PsObject.Properties.Name) {
            if (!($AllProperties.Contains($name))) {
                throw "Error! JSON key '$name' not found in the properties: $($AllProperties)"
            }
        }

        $PSO = [PSCustomObject]@{
        }

        return $PSO
    }

}

The line of $AllProperties = () generates an error in the Build.ps1 process:

Get-ChildItem : At C:\XXXXX\Model\ApplicationType.ps1:70 char:27
+         $AllProperties = ()
+                           ~
An expression was expected after '('.
openapi-generator version

version 5.0.0

OpenAPI declaration file content or url
components:
  schemas:
    ApplicationType:
      type: string
      enum:
        - direct
        - hosted

##### Generation Details

using the powershell generator without any flags or template configurations.
@auto-labeler
Copy link

auto-labeler bot commented Jan 4, 2021

👍 Thanks for opening this issue!
🏷 I have applied any labels matching special text in your issue.

The team will review the labels and make any necessary changes.

@dordadush dordadush reopened this Jan 4, 2021
@vaindil
Copy link

vaindil commented May 11, 2021

I'm not a PowerShell expert, but the fix seems to be using @() instead of () when generating the variable. This shorthand works both when values are declared and when none are declared.

@Viajaz
Copy link

Viajaz commented Nov 4, 2021

Try executing this workaround in the root directory (where Build.ps1 is) to fix the bugged generated PowerShell code:

Get-ChildItem . *.ps1 -Recurse | Foreach-Object {
	$Code = $_ | Get-Content -Raw
	if($Code.Contains('$AllProperties = ()')) {
		Write-Host $_
		$Code = $Code -Replace '\$AllProperties = \(\)','$AllProperties = @()'
		[IO.File]::WriteAllText($_.FullName, $Code)
	}
}

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

3 participants