Skip to content

[POWERSHELL] fix: single-quote DTO property names to prevent $-variable interpolation (fixes #23535)#23622

Closed
gaurav0107 wants to merge 2 commits intoOpenAPITools:masterfrom
gaurav0107:fix/23535-powershell-dollar-property-names
Closed

[POWERSHELL] fix: single-quote DTO property names to prevent $-variable interpolation (fixes #23535)#23622
gaurav0107 wants to merge 2 commits intoOpenAPITools:masterfrom
gaurav0107:fix/23535-powershell-dollar-property-names

Conversation

@gaurav0107
Copy link
Copy Markdown
Contributor

@gaurav0107 gaurav0107 commented Apr 25, 2026

Description

Fix for #23535.

PowerShell treats $ inside double-quoted strings as the sigil for variable interpolation, so a generated hash literal like

$PSO = [PSCustomObject]@{
    "$type" = ${Type}
}

is rewritten at runtime to <value-of-$type> = <value-of-Type>, producing an invalid key. The same interpolation happens in $AllProperties = (...), in -match "<baseName>", and in .Properties["<baseName>"] — every site where model_simple.mustache emits a user-supplied property name inside a double-quoted PowerShell string literal.

This is not theoretical: $type is the default discriminator property C# produces for polymorphic serialization, and $ref / $schema appear routinely in JSON Schema payloads. A full reproduction repository is linked from the issue.

Change summary: swap all eight "{{{baseName}}}" / "<<baseName>>" emissions in model_simple.mustache to single-quoted literals. Single-quoted strings in PowerShell are verbatim, so baseNames containing $ (or any other interpolation meta-character) are preserved. For names without meta-characters the behaviour is identical — there is no functional change for the existing test suite.

Maintainer sign-off from @wing328 on 2026-04-16 explicitly asked for a PR with this exact fix.

A new PowerShellClientCodegenTest exercises the regression using the existing shared 3_0/dollar-in-names-pull14359.yaml fixture (already used by the Python, PHP, and protobuf codegen tests).

Note on sample regeneration. I was not able to run ./bin/generate-samples.sh bin/configs/powershell.yaml on my host (no JDK/Maven available locally). The committed template change is minimal and deterministic — every affected sample file will differ only in that its double-quoted property-name literals flip to single-quoted. Happy to regenerate once I have JDK access, or if a maintainer prefers to run the sample refresh directly I will rebase on top.

PR checklist

  • Read the contribution guidelines.
  • Pull Request title clearly describes the work in the pull request and Pull Request description provides details about how to validate the work.
  • Run the following to build the project and update samples — deferred (no local JDK available; see note above).
  • File the PR against the correct branch: master.
  • If your PR solves a reported issue, reference it using GitHub's linking syntax (fixes #23535).
  • If your PR is targeting a particular programming language, @mention the technical committee members — cc @wing328 (PowerShell tech committee).

Summary by cubic

Single-quote emitted PowerShell DTO property names to prevent $-variable interpolation, fixing invalid keys and regex checks for names like $type, $ref, and $schema. Fixes #23535.

  • Bug Fixes
    • Switch all property-name emissions in model_simple.mustache to single-quoted literals: PSCustomObject keys (incl. ConvertFrom hash), $AllProperties array, -match checks, and .Properties[...] indexers.
    • Extend regression test PowerShellClientCodegenTest to assert single quotes at every emission site (Initialize hash, ConvertFrom hash, $AllProperties, .Properties['…'], and -match) and ensure no double-quoted emissions remain.

Written for commit 4eba061. Summary will update on new commits.

…le interpolation

PowerShell treats `$` inside double-quoted strings as the sigil for variable
interpolation, so `"$foo" = ${Foo}` inside a hash literal becomes `<value-of-$foo> = <value-of-Foo>`.
When an OpenAPI property name starts with (or contains) `$` — e.g. `$type` from
C# polymorphic payloads or `$ref` / `$schema` from JSON Schema — the generated
Initialize- and ConvertFrom- commandlets emit invalid PSCustomObject hash keys
and empty regex patterns, breaking DTO (de)serialization.

Swap all user-property-name emissions in `model_simple.mustache` from
double-quoted to single-quoted literals so baseName is preserved verbatim:

- `$PSO = [PSCustomObject]@{ '<baseName>' = ${<name>} }` (3 sites)
- `$AllProperties = ('<baseName>', ...)` (1 site)
- `-match '<baseName>'` and `.Properties['<baseName>']` (4 sites)

Fixes: OpenAPITools#23535
Co-Authored-By: Claude <noreply@anthropic.com>
@gaurav0107 gaurav0107 marked this pull request as ready for review April 25, 2026 14:41
Extends the regression test to cover every place model_simple.mustache
embeds a user-supplied property name: the Initialize- hash literal, the
ConvertFrom-…JsonTo… hash literal, the $AllProperties allow-list, the
.Properties[…] indexer, and the -match presence check. All five must be
single-quoted so PowerShell preserves `$` literally.

Co-Authored-By: Claude <noreply@anthropic.com>
Copy link
Copy Markdown
Contributor

@cubic-dev-ai cubic-dev-ai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 issue found across 2 files

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="modules/openapi-generator/src/main/resources/powershell/model_simple.mustache">

<violation number="1" location="modules/openapi-generator/src/main/resources/powershell/model_simple.mustache:125">
P1: Single-quoted PowerShell literals use `baseName` without escaping apostrophes, so valid property names containing `'` can generate invalid code.</violation>
</file>

Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.

@@ -122,7 +122,7 @@ function Initialize-{{{apiNamePrefix}}}{{{classname}}} {
$PSO = [PSCustomObject]@{
Copy link
Copy Markdown
Contributor

@cubic-dev-ai cubic-dev-ai Bot Apr 25, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1: Single-quoted PowerShell literals use baseName without escaping apostrophes, so valid property names containing ' can generate invalid code.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At modules/openapi-generator/src/main/resources/powershell/model_simple.mustache, line 125:

<comment>Single-quoted PowerShell literals use `baseName` without escaping apostrophes, so valid property names containing `'` can generate invalid code.</comment>

<file context>
@@ -122,7 +122,7 @@ function Initialize-{{{apiNamePrefix}}}{{{classname}}} {
             {{=<< >>=}}
             <<#allVars>>
-            "<<baseName>>" = ${<<name>>}
+            '<<baseName>>' = ${<<name>>}
             <</allVars>>
             <<={{ }}=>>
</file context>
Fix with Cubic

@@ -0,0 +1,100 @@
/*
* Copyright 2018 OpenAPI-Generator Contributors (https://openapi-generator.tech)
* Copyright 2018 SmartBear Software
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please remove this line.

@wing328
Copy link
Copy Markdown
Member

wing328 commented Apr 25, 2026

thanks for the PR. please follow step 3 to update the samples

@wing328
Copy link
Copy Markdown
Member

wing328 commented Apr 26, 2026

merged via #23624 (you as co-author) with updated samples.

thanks again for the contribution.

@wing328 wing328 closed this Apr 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG][POWERSHELL] Properties which start with $ lead to invalid DTO commandlets

2 participants