Skip to content

Commit

Permalink
[PS] check if JSON properties is defined (#6419)
Browse files Browse the repository at this point in the history
* check if json properties not defined

* add new files
  • Loading branch information
wing328 authored May 25, 2020
1 parent c000eae commit f4897ea
Show file tree
Hide file tree
Showing 10 changed files with 105 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,14 @@ function ConvertFrom-{{{apiNamePrefix}}}JsonTo{{{classname}}} {
$JsonParameters = ConvertFrom-Json -InputObject $Json
# check if Json contains properties not defined in {{{apiNamePrefix}}}{{{classname}}}
$AllProperties = ({{#allVars}}"{{{baseName}}}"{{^-last}}, {{/-last}}{{/allVars}})
foreach ($name in $JsonParameters.PsObject.Properties.Name) {
if (!($AllProperties.Contains($name))) {
throw "Error! JSON key '$name' not found in the properties: $($AllProperties)"
}
}

{{#requiredVars}}
{{#-first}}
If ([string]::IsNullOrEmpty($Json) -or $Json -eq "{}") { # empty json
Expand Down
33 changes: 33 additions & 0 deletions samples/client/petstore/powershell/.openapi-generator/FILES
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
Build.ps1
README.md
appveyor.yml
docs/ApiResponse.md
docs/Category.md
docs/InlineObject.md
docs/InlineObject1.md
docs/Order.md
docs/PSPetApi.md
docs/PSStoreApi.md
docs/PSUserApi.md
docs/Pet.md
docs/Tag.md
docs/User.md
src/PSPetstore/Api/PSPetApi.ps1
src/PSPetstore/Api/PSStoreApi.ps1
src/PSPetstore/Api/PSUserApi.ps1
src/PSPetstore/Client/PSConfiguration.ps1
src/PSPetstore/Model/ApiResponse.ps1
src/PSPetstore/Model/Category.ps1
src/PSPetstore/Model/InlineObject.ps1
src/PSPetstore/Model/InlineObject1.ps1
src/PSPetstore/Model/Order.ps1
src/PSPetstore/Model/Pet.ps1
src/PSPetstore/Model/Tag.ps1
src/PSPetstore/Model/User.ps1
src/PSPetstore/PSPetstore.psm1
src/PSPetstore/Private/Get-CommonParameters.ps1
src/PSPetstore/Private/Out-DebugParameter.ps1
src/PSPetstore/Private/PSApiClient.ps1
src/PSPetstore/Private/PSHttpSignatureAuth.ps1
src/PSPetstore/Private/PSRSAEncryptionProvider.cs
src/PSPetstore/en-US/about_PSPetstore.help.txt
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,14 @@ function ConvertFrom-PSJsonToApiResponse {

$JsonParameters = ConvertFrom-Json -InputObject $Json

# check if Json contains properties not defined in PSApiResponse
$AllProperties = ("code", "type", "message")
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 "code"))) { #optional property not found
$Code = $null
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,14 @@ function ConvertFrom-PSJsonToCategory {

$JsonParameters = ConvertFrom-Json -InputObject $Json

# check if Json contains properties not defined in PSCategory
$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
$Id = $null
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,14 @@ function ConvertFrom-PSJsonToInlineObject {

$JsonParameters = ConvertFrom-Json -InputObject $Json

# check if Json contains properties not defined in PSInlineObject
$AllProperties = ("name", "status")
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 "name"))) { #optional property not found
$Name = $null
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,14 @@ function ConvertFrom-PSJsonToInlineObject1 {

$JsonParameters = ConvertFrom-Json -InputObject $Json

# check if Json contains properties not defined in PSInlineObject1
$AllProperties = ("additionalMetadata", "file")
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 "additionalMetadata"))) { #optional property not found
$AdditionalMetadata = $null
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,14 @@ function ConvertFrom-PSJsonToOrder {

$JsonParameters = ConvertFrom-Json -InputObject $Json

# check if Json contains properties not defined in PSOrder
$AllProperties = ("id", "petId", "quantity", "shipDate", "status", "complete")
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
$Id = $null
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,14 @@ function ConvertFrom-PSJsonToPet {

$JsonParameters = ConvertFrom-Json -InputObject $Json

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

If ([string]::IsNullOrEmpty($Json) -or $Json -eq "{}") { # empty json
throw "Error! Empty JSON cannot be serialized due to the required property `name` missing."
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,14 @@ function ConvertFrom-PSJsonToTag {

$JsonParameters = ConvertFrom-Json -InputObject $Json

# check if Json contains properties not defined in PSTag
$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
$Id = $null
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,14 @@ function ConvertFrom-PSJsonToUser {

$JsonParameters = ConvertFrom-Json -InputObject $Json

# check if Json contains properties not defined in PSUser
$AllProperties = ("id", "username", "firstName", "lastName", "email", "password", "phone", "userStatus")
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
$Id = $null
} else {
Expand Down

0 comments on commit f4897ea

Please sign in to comment.