From f668253f36b45aefbbbff640b393419933bf6e22 Mon Sep 17 00:00:00 2001
From: William Cheng <wing328hk@gmail.com>
Date: Fri, 10 Apr 2020 22:03:02 +0800
Subject: [PATCH] consolidate header selection functions

---
 .../api_client.mustache                       | 43 ++++++-------------
 .../src/PSPetstore/Private/PSApiClient.ps1    | 43 ++++++-------------
 2 files changed, 24 insertions(+), 62 deletions(-)

diff --git a/modules/openapi-generator/src/main/resources/powershell-experimental/api_client.mustache b/modules/openapi-generator/src/main/resources/powershell-experimental/api_client.mustache
index 0c559727be9..2d785b840d1 100644
--- a/modules/openapi-generator/src/main/resources/powershell-experimental/api_client.mustache
+++ b/modules/openapi-generator/src/main/resources/powershell-experimental/api_client.mustache
@@ -49,12 +49,12 @@ function Invoke-{{{apiNamePrefix}}}ApiClient {
     }
 
     # accept, content-type headers
-    $Accept = SelectAcceptHeaders -Accepts $Accepts
+    $Accept = SelectHeaders -Headers $Accepts
     if ($Accept) {
         $HeaderParameters['Accept'] = $Accept
     }
 
-    $ContentType= SelectContentTypeHeaders -ContentTypes $ContentTypes
+    $ContentType= SelectHeaders -Headers $ContentTypes
     if ($ContentType) {
         $HeaderParameters['Content-Type'] = $ContentType
     }
@@ -114,43 +114,24 @@ function Invoke-{{{apiNamePrefix}}}ApiClient {
     }
 }
 
-function SelectAcceptHeaders {
+# Select JSON MIME if present, otherwise choose the first one if available
+function SelectHeaders {
     Param(
         [Parameter(Mandatory)]
         [AllowEmptyCollection()]
-        [String[]]$Accepts
+        [String[]]$Headers
     )
 
-    foreach ($Accept in $Accepts) {
-        if (IsJsonMIME -MIME $Accept) {
-            return $Accept
+    foreach ($Header in $Headers) {
+        if (IsJsonMIME -MIME $Header) {
+            return $Header
         }
     }
 
-    if (!($Accepts) -or $Accepts.Count -eq 0) {
+    if (!($Headers) -or $Headers.Count -eq 0) {
         return $null
     } else {
-        return $Accepts[0] # return the first one
-    }
-}
-
-function SelectContentTypeHeaders {
-    Param(
-        [Parameter(Mandatory)]
-        [AllowEmptyCollection()]
-        [String[]]$ContentTypes
-    )
-
-    foreach ($ContentType in $ContentTypes) {
-        if (IsJsonMIME -MIME $ContentType) {
-            return $ContentType
-        }
-    }
-
-    if (!($ContentTypes) -or $ContentTypes.Count -eq 0) {
-        return $null
-    } else {
-        return $ContentTypes[0] # return the first one
+        return $Headers[0] # return the first one
     }
 }
 
@@ -190,12 +171,12 @@ function DeserializeResponse {
         if ($ContentTypes) {
             $ContentType = $null
             if ($ContentTypes.Count -gt 1) {
-                $ContentType = SelectContentTypeHeaders -ContentTypes $ContentTypes
+                $ContentType = SelectHeaders -Headers $ContentTypes
             } else {
                 $ContentType = $ContentTypes[0]
             }
 
-            if (IsJsonMIME -MIME $ContentType) {  # JSON
+            if (IsJsonMIME -MIME $ContentType) { # JSON
                 return ConvertFrom-Json $Response
             } else { # XML, file, etc
                 return $Response
diff --git a/samples/client/petstore/powershell-experimental/src/PSPetstore/Private/PSApiClient.ps1 b/samples/client/petstore/powershell-experimental/src/PSPetstore/Private/PSApiClient.ps1
index d7092365099..e965758e23a 100644
--- a/samples/client/petstore/powershell-experimental/src/PSPetstore/Private/PSApiClient.ps1
+++ b/samples/client/petstore/powershell-experimental/src/PSPetstore/Private/PSApiClient.ps1
@@ -55,12 +55,12 @@ function Invoke-PSApiClient {
     }
 
     # accept, content-type headers
-    $Accept = SelectAcceptHeaders -Accepts $Accepts
+    $Accept = SelectHeaders -Headers $Accepts
     if ($Accept) {
         $HeaderParameters['Accept'] = $Accept
     }
 
-    $ContentType= SelectContentTypeHeaders -ContentTypes $ContentTypes
+    $ContentType= SelectHeaders -Headers $ContentTypes
     if ($ContentType) {
         $HeaderParameters['Content-Type'] = $ContentType
     }
@@ -120,43 +120,24 @@ function Invoke-PSApiClient {
     }
 }
 
-function SelectAcceptHeaders {
+# Select JSON MIME if present, otherwise choose the first one if available
+function SelectHeaders {
     Param(
         [Parameter(Mandatory)]
         [AllowEmptyCollection()]
-        [String[]]$Accepts
+        [String[]]$Headers
     )
 
-    foreach ($Accept in $Accepts) {
-        if (IsJsonMIME -MIME $Accept) {
-            return $Accept
+    foreach ($Header in $Headers) {
+        if (IsJsonMIME -MIME $Header) {
+            return $Header
         }
     }
 
-    if (!($Accepts) -or $Accepts.Count -eq 0) {
+    if (!($Headers) -or $Headers.Count -eq 0) {
         return $null
     } else {
-        return $Accepts[0] # return the first one
-    }
-}
-
-function SelectContentTypeHeaders {
-    Param(
-        [Parameter(Mandatory)]
-        [AllowEmptyCollection()]
-        [String[]]$ContentTypes
-    )
-
-    foreach ($ContentType in $ContentTypes) {
-        if (IsJsonMIME -MIME $ContentType) {
-            return $ContentType
-        }
-    }
-
-    if (!($ContentTypes) -or $ContentTypes.Count -eq 0) {
-        return $null
-    } else {
-        return $ContentTypes[0] # return the first one
+        return $Headers[0] # return the first one
     }
 }
 
@@ -196,12 +177,12 @@ function DeserializeResponse {
         if ($ContentTypes) {
             $ContentType = $null
             if ($ContentTypes.Count -gt 1) {
-                $ContentType = SelectContentTypeHeaders -ContentTypes $ContentTypes
+                $ContentType = SelectHeaders -Headers $ContentTypes
             } else {
                 $ContentType = $ContentTypes[0]
             }
 
-            if (IsJsonMIME -MIME $ContentType) {  # JSON
+            if (IsJsonMIME -MIME $ContentType) { # JSON
                 return ConvertFrom-Json $Response
             } else { # XML, file, etc
                 return $Response
-- 
GitLab