OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
20.55k stars 6.27k forks source link

Invalid Powershell code generated #8502

Open condorcorde opened 3 years ago

condorcorde commented 3 years ago
Description

I have tried the powershell client generator with two openAPI input files and the generated code does not build for (at least) two different issues.

The first is backtick characters surrounding name of parameters in many throw statements, e.g.

       if (!([bool]($JsonParameters.PSobject.Properties.name -match "url"))) {
            throw "Error! JSON cannot be serialized due to the required property `url` missing."
        } else {

Most occurrences are ignored, but `u is interpreted as the beginning of a Unicode sequence and raises an error message:

Line |
  64 |  … nctionsToExport = $FunctionPath | Get-ChildItem -Filter *.ps1 | Get-F …
     |                                      ~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | At /tmp/src/CWManage/Model/ConnectWiseHostedSetup.ps1:217 char:82 + … or!
     | JSON cannot be serialized due to the required property `url` miss … +                                               
     | ~~ The Unicode escape sequence is not valid. A valid sequence is `u{ followed by one to six hex digits and a closing
     | '}'.

The second error is a parameter being declared twice with the same name ($Metadata):

<#
.SYNOPSIS

No summary available.

.DESCRIPTION

.PARAMETER Extras
No description available.
.PARAMETER MetaData
No description available.
.PARAMETER Metadata
No description available.
.PARAMETER Operator
No description available.
.PARAMETER Values
No description available.
.OUTPUTS

Criterion<PSCustomObject>
#>

function Initialize-Criterion {
    [CmdletBinding()]
    Param (
        [Parameter(Position = 0, ValueFromPipelineByPropertyName = $true)]
        [PSCustomObject]
        ${Extras},
        [Parameter(Position = 1, ValueFromPipelineByPropertyName = $true)]
        [PSCustomObject]
        ${MetaData},
        [Parameter(Position = 2, ValueFromPipelineByPropertyName = $true)]
        [PSCustomObject]
        ${Metadata},
        [Parameter(Position = 3, ValueFromPipelineByPropertyName = $true)]

Causing the error below

Line |
  65 |  … nctionsToExport = $FunctionPath | Get-ChildItem -Filter *.ps1 | Get-F …
     |                                      ~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | At /tmp/src/InsightVM/Model/Criterion.ps1:44 char:9 +        
     | ${Metadata}, +         ~~~~~~~~~~~ Duplicate parameter $Metadata in parameter list. At
     | /Users/amiotto/Documents/API/InsightVM/insightvm-3.0/src/InsightVM/Model/Criterion.ps1:62 char:13 +            
     | "metadata" = ${Metadata} +             ~~~~~~~~~~ Duplicate keys 'metadata' are not allowed in hash literals. At
     | /Users/amiotto/Documents/API/InsightVM/insightvm-3.0/src/InsightVM/Model/Criterion.ps1:142 char:13 +            
     | "metadata" = ${Metadata} +             ~~~~~~~~~~ Duplicate keys 'metadata' are not allowed in hash literals.
openapi-generator version

5.0.0

OpenAPI declaration file content or url

Both errors can be reproduced with this file:

https://help.rapid7.com/insightvm/en-us/api/api.json

Command line used for generation

openapi-generator generate --generator-name powershell --input-spec V3/api.json --additional-properties=packageName=InsightVM,packageVersion=3.0 --output insightvm-3.0 --skip-validate-spec

Steps to reproduce
  1. Generate the code with openapi-generator
  2. Launch Build.ps1
Related issues/PRs

I could not find anything in open issues.

Suggest a fix/enhancement

The backtick in the throw string should be replaced by a quote character.

wing328 commented 3 years ago

First issue already fixed by https://github.com/OpenAPITools/openapi-generator/pull/8321

wing328 commented 3 years ago
"metaData":{"$ref":"#/definitions/IMetaData"},"metadata":{"$ref":"#/definitions/MetadataContainer"}

One way to address this is to add an option to use the original naming in the model (without conforming to PowerShell's variable naming convention)