Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ function Initialize-{{{apiNamePrefix}}}{{{classname}}} {
$PSO = [PSCustomObject]@{
{{=<< >>=}}
<<#allVars>>
"<<baseName>>" = ${<<name>>}
'<<baseName>>' = ${<<name>>}
<</allVars>>
<<={{ }}=>>
}
Expand Down Expand Up @@ -184,7 +184,7 @@ function Initialize-{{{apiNamePrefix}}}{{{classname}}} {
{{=<< >>=}}
<<#allVars>>
<<^isReadOnly>>
"<<baseName>>" = ${<<name>>}
'<<baseName>>' = ${<<name>>}
<</isReadOnly>>
<</allVars>>
<<={{ }}=>>
Expand Down Expand Up @@ -228,7 +228,7 @@ function ConvertFrom-{{{apiNamePrefix}}}JsonTo{{{classname}}} {
{{/isAdditionalPropertiesTrue}}

# check if Json contains properties not defined in {{{apiNamePrefix}}}{{{classname}}}
$AllProperties = ({{#allVars}}"{{{baseName}}}"{{^-last}}, {{/-last}}{{/allVars}})
$AllProperties = ({{#allVars}}'{{{baseName}}}'{{^-last}}, {{/-last}}{{/allVars}})
foreach ($name in $JsonParameters.PsObject.Properties.Name) {
{{^isAdditionalPropertiesTrue}}
if (!($AllProperties.Contains($name))) {
Expand All @@ -250,29 +250,29 @@ function ConvertFrom-{{{apiNamePrefix}}}JsonTo{{{classname}}} {
}

{{/-first}}
if (!([bool]($JsonParameters.PSobject.Properties.name -match "{{{baseName}}}"))) {
if (!([bool]($JsonParameters.PSobject.Properties.name -match '{{{baseName}}}'))) {
throw "Error! JSON cannot be serialized due to the required property '{{{baseName}}}' missing."
} else {
${{name}} = $JsonParameters.PSobject.Properties["{{{baseName}}}"].value
${{name}} = $JsonParameters.PSobject.Properties['{{{baseName}}}'].value
}

{{/requiredVars}}
{{#optionalVars}}
if (!([bool]($JsonParameters.PSobject.Properties.name -match "{{{baseName}}}"))) { #optional property not found
if (!([bool]($JsonParameters.PSobject.Properties.name -match '{{{baseName}}}'))) { #optional property not found
${{name}} = $null
} else {
${{name}} = $JsonParameters.PSobject.Properties["{{{baseName}}}"].value
${{name}} = $JsonParameters.PSobject.Properties['{{{baseName}}}'].value
}

{{/optionalVars}}
$PSO = [PSCustomObject]@{
{{=<< >>=}}
<<#allVars>>
"<<baseName>>" = ${<<name>>}
'<<baseName>>' = ${<<name>>}
<</allVars>>
<<={{ }}=>>
{{#isAdditionalPropertiesTrue}}
"AdditionalProperties" = ${{{apiNamePrefix}}}{{{classname}}}AdditionalProperties
'AdditionalProperties' = ${{{apiNamePrefix}}}{{{classname}}}AdditionalProperties
{{/isAdditionalPropertiesTrue}}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
* Copyright 2018 OpenAPI-Generator Contributors (https://openapi-generator.tech)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.openapitools.codegen.powershell;

import io.swagger.parser.OpenAPIParser;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.parser.core.models.ParseOptions;
import org.openapitools.codegen.ClientOptInput;
import org.openapitools.codegen.CodegenConstants;
import org.openapitools.codegen.DefaultGenerator;
import org.openapitools.codegen.languages.PowerShellClientCodegen;
import org.testng.annotations.Test;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

import static org.openapitools.codegen.TestUtils.assertFileContains;
import static org.openapitools.codegen.TestUtils.assertFileNotContains;

public class PowerShellClientCodegenTest {

/**
* Regression test for <a href="https://github.com/OpenAPITools/openapi-generator/issues/23535">#23535</a>.
*
* PowerShell treats {@code $} inside double-quoted strings as the sigil for
* variable interpolation, so hash-table keys emitted as {@code "$foo"} get
* rewritten at runtime to the value of {@code $foo} (usually empty). This
* produces invalid DTO commandlets whenever an OpenAPI property name starts
* with (or contains) {@code $}. The {@code model_simple.mustache} template
* now emits single-quoted keys so the literal baseName is preserved.
*/
@Test
public void dollarSignPropertyNamesAreSingleQuoted() throws IOException {
File output = Files.createTempDirectory("test-powershell-23535").toFile().getCanonicalFile();
output.deleteOnExit();
String outputPath = output.getAbsolutePath().replace('\\', '/');

OpenAPI openAPI = new OpenAPIParser()
.readLocation("src/test/resources/3_0/dollar-in-names-pull14359.yaml", null, new ParseOptions())
.getOpenAPI();

PowerShellClientCodegen codegen = new PowerShellClientCodegen();
codegen.setOutputDir(output.getAbsolutePath());

ClientOptInput input = new ClientOptInput();
input.openAPI(openAPI);
input.config(codegen);

DefaultGenerator generator = new DefaultGenerator();
generator.setGeneratorPropertyDefault(CodegenConstants.MODELS, "true");
generator.setGeneratorPropertyDefault(CodegenConstants.MODEL_TESTS, "false");
generator.setGeneratorPropertyDefault(CodegenConstants.MODEL_DOCS, "false");
generator.setGeneratorPropertyDefault(CodegenConstants.APIS, "false");
generator.setGeneratorPropertyDefault(CodegenConstants.SUPPORTING_FILES, "false");
generator.opts(input).generate();

// The PowerShell codegen sanitises "$DollarModel$" to "DollarModel",
// so the emitted model lives at src/<package>/Model/DollarModel.ps1.
java.nio.file.Path dollarModelPs1 = Paths.get(outputPath + "/src/PSOpenAPITools/Model/DollarModel.ps1");

// Every site where model_simple.mustache emits a user-supplied baseName
// must use single-quoted PowerShell strings so `$` is treated literally:
//
// 1. The `Initialize-…` hash literal: `'$dollarValue$' = ${DollarValue}`
// 2. The JSON round-trip hash literal in `ConvertFrom-…JsonTo…`
// 3. The property-allowlist array: `$AllProperties = ('$dollarValue$')`
// 4. The property-indexer lookup: `$JsonParameters.PSobject.Properties['$dollarValue$'].value`
// 5. The presence-check regex: `-match '$dollarValue$'`
assertFileContains(dollarModelPs1,
"'$dollarValue$' = ${DollarValue}",
"$AllProperties = ('$dollarValue$')",
"$JsonParameters.PSobject.Properties['$dollarValue$'].value",
"-match '$dollarValue$'");

// The previous double-quoted emissions must no longer appear anywhere in
// the generated model file.
assertFileNotContains(dollarModelPs1,
"\"$dollarValue$\" = ",
"$AllProperties = (\"$dollarValue$\")",
"$JsonParameters.PSobject.Properties[\"$dollarValue$\"]",
"-match \"$dollarValue$\"");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ function Initialize-Bird {


$PSO = [PSCustomObject]@{
"size" = ${Size}
"color" = ${Color}
'size' = ${Size}
'color' = ${Color}
}


Expand Down Expand Up @@ -80,28 +80,28 @@ function ConvertFrom-JsonToBird {
$JsonParameters = ConvertFrom-Json -InputObject $Json

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

if (!([bool]($JsonParameters.PSobject.Properties.name -match "size"))) { #optional property not found
if (!([bool]($JsonParameters.PSobject.Properties.name -match 'size'))) { #optional property not found
$Size = $null
} else {
$Size = $JsonParameters.PSobject.Properties["size"].value
$Size = $JsonParameters.PSobject.Properties['size'].value
}

if (!([bool]($JsonParameters.PSobject.Properties.name -match "color"))) { #optional property not found
if (!([bool]($JsonParameters.PSobject.Properties.name -match 'color'))) { #optional property not found
$Color = $null
} else {
$Color = $JsonParameters.PSobject.Properties["color"].value
$Color = $JsonParameters.PSobject.Properties['color'].value
}

$PSO = [PSCustomObject]@{
"size" = ${Size}
"color" = ${Color}
'size' = ${Size}
'color' = ${Color}
}

return $PSO
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ function Initialize-Category {


$PSO = [PSCustomObject]@{
"id" = ${Id}
"name" = ${Name}
'id' = ${Id}
'name' = ${Name}
}


Expand Down Expand Up @@ -80,28 +80,28 @@ function ConvertFrom-JsonToCategory {
$JsonParameters = ConvertFrom-Json -InputObject $Json

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

if (!([bool]($JsonParameters.PSobject.Properties.name -match "id"))) { #optional property not found
if (!([bool]($JsonParameters.PSobject.Properties.name -match 'id'))) { #optional property not found
$Id = $null
} else {
$Id = $JsonParameters.PSobject.Properties["id"].value
$Id = $JsonParameters.PSobject.Properties['id'].value
}

if (!([bool]($JsonParameters.PSobject.Properties.name -match "name"))) { #optional property not found
if (!([bool]($JsonParameters.PSobject.Properties.name -match 'name'))) { #optional property not found
$Name = $null
} else {
$Name = $JsonParameters.PSobject.Properties["name"].value
$Name = $JsonParameters.PSobject.Properties['name'].value
}

$PSO = [PSCustomObject]@{
"id" = ${Id}
"name" = ${Name}
'id' = ${Id}
'name' = ${Name}
}

return $PSO
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@ function Initialize-DataQuery {


$PSO = [PSCustomObject]@{
"id" = ${Id}
"outcomes" = ${Outcomes}
"suffix" = ${Suffix}
"text" = ${Text}
"date" = ${Date}
'id' = ${Id}
'outcomes' = ${Outcomes}
'suffix' = ${Suffix}
'text' = ${Text}
'date' = ${Date}
}


Expand Down Expand Up @@ -99,49 +99,49 @@ function ConvertFrom-JsonToDataQuery {
$JsonParameters = ConvertFrom-Json -InputObject $Json

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

if (!([bool]($JsonParameters.PSobject.Properties.name -match "id"))) { #optional property not found
if (!([bool]($JsonParameters.PSobject.Properties.name -match 'id'))) { #optional property not found
$Id = $null
} else {
$Id = $JsonParameters.PSobject.Properties["id"].value
$Id = $JsonParameters.PSobject.Properties['id'].value
}

if (!([bool]($JsonParameters.PSobject.Properties.name -match "outcomes"))) { #optional property not found
if (!([bool]($JsonParameters.PSobject.Properties.name -match 'outcomes'))) { #optional property not found
$Outcomes = $null
} else {
$Outcomes = $JsonParameters.PSobject.Properties["outcomes"].value
$Outcomes = $JsonParameters.PSobject.Properties['outcomes'].value
}

if (!([bool]($JsonParameters.PSobject.Properties.name -match "suffix"))) { #optional property not found
if (!([bool]($JsonParameters.PSobject.Properties.name -match 'suffix'))) { #optional property not found
$Suffix = $null
} else {
$Suffix = $JsonParameters.PSobject.Properties["suffix"].value
$Suffix = $JsonParameters.PSobject.Properties['suffix'].value
}

if (!([bool]($JsonParameters.PSobject.Properties.name -match "text"))) { #optional property not found
if (!([bool]($JsonParameters.PSobject.Properties.name -match 'text'))) { #optional property not found
$Text = $null
} else {
$Text = $JsonParameters.PSobject.Properties["text"].value
$Text = $JsonParameters.PSobject.Properties['text'].value
}

if (!([bool]($JsonParameters.PSobject.Properties.name -match "date"))) { #optional property not found
if (!([bool]($JsonParameters.PSobject.Properties.name -match 'date'))) { #optional property not found
$Date = $null
} else {
$Date = $JsonParameters.PSobject.Properties["date"].value
$Date = $JsonParameters.PSobject.Properties['date'].value
}

$PSO = [PSCustomObject]@{
"id" = ${Id}
"outcomes" = ${Outcomes}
"suffix" = ${Suffix}
"text" = ${Text}
"date" = ${Date}
'id' = ${Id}
'outcomes' = ${Outcomes}
'suffix' = ${Suffix}
'text' = ${Text}
'date' = ${Date}
}

return $PSO
Expand Down
Loading
Loading