Skip to content

Commit f4897ea

Browse files
authored
[PS] check if JSON properties is defined (#6419)
* check if json properties not defined * add new files
1 parent c000eae commit f4897ea

File tree

10 files changed

+105
-0
lines changed

10 files changed

+105
-0
lines changed

modules/openapi-generator/src/main/resources/powershell/model_simple.mustache

+8
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,14 @@ function ConvertFrom-{{{apiNamePrefix}}}JsonTo{{{classname}}} {
130130
131131
$JsonParameters = ConvertFrom-Json -InputObject $Json
132132
133+
# check if Json contains properties not defined in {{{apiNamePrefix}}}{{{classname}}}
134+
$AllProperties = ({{#allVars}}"{{{baseName}}}"{{^-last}}, {{/-last}}{{/allVars}})
135+
foreach ($name in $JsonParameters.PsObject.Properties.Name) {
136+
if (!($AllProperties.Contains($name))) {
137+
throw "Error! JSON key '$name' not found in the properties: $($AllProperties)"
138+
}
139+
}
140+
133141
{{#requiredVars}}
134142
{{#-first}}
135143
If ([string]::IsNullOrEmpty($Json) -or $Json -eq "{}") { # empty json
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
Build.ps1
2+
README.md
3+
appveyor.yml
4+
docs/ApiResponse.md
5+
docs/Category.md
6+
docs/InlineObject.md
7+
docs/InlineObject1.md
8+
docs/Order.md
9+
docs/PSPetApi.md
10+
docs/PSStoreApi.md
11+
docs/PSUserApi.md
12+
docs/Pet.md
13+
docs/Tag.md
14+
docs/User.md
15+
src/PSPetstore/Api/PSPetApi.ps1
16+
src/PSPetstore/Api/PSStoreApi.ps1
17+
src/PSPetstore/Api/PSUserApi.ps1
18+
src/PSPetstore/Client/PSConfiguration.ps1
19+
src/PSPetstore/Model/ApiResponse.ps1
20+
src/PSPetstore/Model/Category.ps1
21+
src/PSPetstore/Model/InlineObject.ps1
22+
src/PSPetstore/Model/InlineObject1.ps1
23+
src/PSPetstore/Model/Order.ps1
24+
src/PSPetstore/Model/Pet.ps1
25+
src/PSPetstore/Model/Tag.ps1
26+
src/PSPetstore/Model/User.ps1
27+
src/PSPetstore/PSPetstore.psm1
28+
src/PSPetstore/Private/Get-CommonParameters.ps1
29+
src/PSPetstore/Private/Out-DebugParameter.ps1
30+
src/PSPetstore/Private/PSApiClient.ps1
31+
src/PSPetstore/Private/PSHttpSignatureAuth.ps1
32+
src/PSPetstore/Private/PSRSAEncryptionProvider.cs
33+
src/PSPetstore/en-US/about_PSPetstore.help.txt

samples/client/petstore/powershell/src/PSPetstore/Model/ApiResponse.ps1

+8
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,14 @@ function ConvertFrom-PSJsonToApiResponse {
8585

8686
$JsonParameters = ConvertFrom-Json -InputObject $Json
8787

88+
# check if Json contains properties not defined in PSApiResponse
89+
$AllProperties = ("code", "type", "message")
90+
foreach ($name in $JsonParameters.PsObject.Properties.Name) {
91+
if (!($AllProperties.Contains($name))) {
92+
throw "Error! JSON key '$name' not found in the properties: $($AllProperties)"
93+
}
94+
}
95+
8896
if (!([bool]($JsonParameters.PSobject.Properties.name -match "code"))) { #optional property not found
8997
$Code = $null
9098
} else {

samples/client/petstore/powershell/src/PSPetstore/Model/Category.ps1

+8
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,14 @@ function ConvertFrom-PSJsonToCategory {
7979

8080
$JsonParameters = ConvertFrom-Json -InputObject $Json
8181

82+
# check if Json contains properties not defined in PSCategory
83+
$AllProperties = ("id", "name")
84+
foreach ($name in $JsonParameters.PsObject.Properties.Name) {
85+
if (!($AllProperties.Contains($name))) {
86+
throw "Error! JSON key '$name' not found in the properties: $($AllProperties)"
87+
}
88+
}
89+
8290
if (!([bool]($JsonParameters.PSobject.Properties.name -match "id"))) { #optional property not found
8391
$Id = $null
8492
} else {

samples/client/petstore/powershell/src/PSPetstore/Model/InlineObject.ps1

+8
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,14 @@ function ConvertFrom-PSJsonToInlineObject {
7878

7979
$JsonParameters = ConvertFrom-Json -InputObject $Json
8080

81+
# check if Json contains properties not defined in PSInlineObject
82+
$AllProperties = ("name", "status")
83+
foreach ($name in $JsonParameters.PsObject.Properties.Name) {
84+
if (!($AllProperties.Contains($name))) {
85+
throw "Error! JSON key '$name' not found in the properties: $($AllProperties)"
86+
}
87+
}
88+
8189
if (!([bool]($JsonParameters.PSobject.Properties.name -match "name"))) { #optional property not found
8290
$Name = $null
8391
} else {

samples/client/petstore/powershell/src/PSPetstore/Model/InlineObject1.ps1

+8
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,14 @@ function ConvertFrom-PSJsonToInlineObject1 {
7878

7979
$JsonParameters = ConvertFrom-Json -InputObject $Json
8080

81+
# check if Json contains properties not defined in PSInlineObject1
82+
$AllProperties = ("additionalMetadata", "file")
83+
foreach ($name in $JsonParameters.PsObject.Properties.Name) {
84+
if (!($AllProperties.Contains($name))) {
85+
throw "Error! JSON key '$name' not found in the properties: $($AllProperties)"
86+
}
87+
}
88+
8189
if (!([bool]($JsonParameters.PSobject.Properties.name -match "additionalMetadata"))) { #optional property not found
8290
$AdditionalMetadata = $null
8391
} else {

samples/client/petstore/powershell/src/PSPetstore/Model/Order.ps1

+8
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,14 @@ function ConvertFrom-PSJsonToOrder {
107107

108108
$JsonParameters = ConvertFrom-Json -InputObject $Json
109109

110+
# check if Json contains properties not defined in PSOrder
111+
$AllProperties = ("id", "petId", "quantity", "shipDate", "status", "complete")
112+
foreach ($name in $JsonParameters.PsObject.Properties.Name) {
113+
if (!($AllProperties.Contains($name))) {
114+
throw "Error! JSON key '$name' not found in the properties: $($AllProperties)"
115+
}
116+
}
117+
110118
if (!([bool]($JsonParameters.PSobject.Properties.name -match "id"))) { #optional property not found
111119
$Id = $null
112120
} else {

samples/client/petstore/powershell/src/PSPetstore/Model/Pet.ps1

+8
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,14 @@ function ConvertFrom-PSJsonToPet {
115115

116116
$JsonParameters = ConvertFrom-Json -InputObject $Json
117117

118+
# check if Json contains properties not defined in PSPet
119+
$AllProperties = ("id", "category", "name", "photoUrls", "tags", "status")
120+
foreach ($name in $JsonParameters.PsObject.Properties.Name) {
121+
if (!($AllProperties.Contains($name))) {
122+
throw "Error! JSON key '$name' not found in the properties: $($AllProperties)"
123+
}
124+
}
125+
118126
If ([string]::IsNullOrEmpty($Json) -or $Json -eq "{}") { # empty json
119127
throw "Error! Empty JSON cannot be serialized due to the required property `name` missing."
120128
}

samples/client/petstore/powershell/src/PSPetstore/Model/Tag.ps1

+8
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,14 @@ function ConvertFrom-PSJsonToTag {
7878

7979
$JsonParameters = ConvertFrom-Json -InputObject $Json
8080

81+
# check if Json contains properties not defined in PSTag
82+
$AllProperties = ("id", "name")
83+
foreach ($name in $JsonParameters.PsObject.Properties.Name) {
84+
if (!($AllProperties.Contains($name))) {
85+
throw "Error! JSON key '$name' not found in the properties: $($AllProperties)"
86+
}
87+
}
88+
8189
if (!([bool]($JsonParameters.PSobject.Properties.name -match "id"))) { #optional property not found
8290
$Id = $null
8391
} else {

samples/client/petstore/powershell/src/PSPetstore/Model/User.ps1

+8
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,14 @@ function ConvertFrom-PSJsonToUser {
121121

122122
$JsonParameters = ConvertFrom-Json -InputObject $Json
123123

124+
# check if Json contains properties not defined in PSUser
125+
$AllProperties = ("id", "username", "firstName", "lastName", "email", "password", "phone", "userStatus")
126+
foreach ($name in $JsonParameters.PsObject.Properties.Name) {
127+
if (!($AllProperties.Contains($name))) {
128+
throw "Error! JSON key '$name' not found in the properties: $($AllProperties)"
129+
}
130+
}
131+
124132
if (!([bool]($JsonParameters.PSobject.Properties.name -match "id"))) { #optional property not found
125133
$Id = $null
126134
} else {

0 commit comments

Comments
 (0)