File tree 12 files changed +159
-74
lines changed
modules/openapi-generator/src/main/resources/php
samples/client/petstore/php
12 files changed +159
-74
lines changed Original file line number Diff line number Diff line change
1
+ RUN apt update && apt install -y maven
Original file line number Diff line number Diff line change @@ -76,7 +76,7 @@ class HeaderSelector
76
76
}
77
77
78
78
# If none of the available Accept headers is of type "json", then just use all them
79
- $headersWithJson = preg_grep('~(?i)^(application/json|[^;/ \t]+/[^;/ \t]+[+]json)[ \t]*(;.*)?$~', $accept);
79
+ $headersWithJson = $this->selectJsonMimeList( $accept);
80
80
if (count($headersWithJson) === 0) {
81
81
return implode(' ,' , $accept );
82
82
}
@@ -86,6 +86,34 @@ class HeaderSelector
86
86
return $this->getAcceptHeaderWithAdjustedWeight($accept, $headersWithJson);
87
87
}
88
88
89
+ /**
90
+ * Detects whether a string contains a valid JSON mime type
91
+ *
92
+ * @param string $searchString
93
+ * @return bool
94
+ */
95
+ public function isJsonMime(string $searchString): bool
96
+ {
97
+ return preg_match(' ~^application/(json|[\w !#$&.+-^_]+\+ json)\s *(;|$)~' , $searchString ) === 1;
98
+ }
99
+
100
+ /**
101
+ * Select all items from a list containing a JSON mime type
102
+ *
103
+ * @param array $mimeList
104
+ * @return array
105
+ */
106
+ private function selectJsonMimeList(array $mimeList): array {
107
+ $jsonMimeList = [];
108
+ foreach ($mimeList as $mime ) {
109
+ if ($this -> isJsonMime ($mime )) {
110
+ $jsonMimeList [] = $mime ;
111
+ }
112
+ }
113
+ return $jsonMimeList;
114
+ }
115
+
116
+
89
117
/**
90
118
* Create an Accept header string from the given "Accept" headers array, recalculating all weights
91
119
*
Original file line number Diff line number Diff line change @@ -613,7 +613,7 @@ use function sprintf;
613
613
// for model (json/xml)
614
614
{ {#bodyParams} }
615
615
if (isset(${ {paramName} })) {
616
- if ($headers [' Content-Type' ] === ' application/json ' ) {
616
+ if ($this -> headerSelector -> isJsonMime ( $ headers [' Content-Type' ]) ) {
617
617
$httpBody = json_encode(ObjectSerializer::sanitizeForSerialization(${{paramName} }));
618
618
} else {
619
619
$httpBody = ${{paramName} };
@@ -637,7 +637,7 @@ use function sprintf;
637
637
// for HTTP post (form)
638
638
$httpBody = new MultipartStream($multipartContents);
639
639
640
- } elseif ($headers['Content-Type'] === 'application/json' ) {
640
+ } elseif ($this->headerSelector->isJsonMime($ headers['Content-Type']) ) {
641
641
$httpBody = json_encode($formParams );
642
642
643
643
} else {
Original file line number Diff line number Diff line change @@ -85,7 +85,7 @@ private function selectAcceptHeader(array $accept): ?string
85
85
}
86
86
87
87
# If none of the available Accept headers is of type "json", then just use all them
88
- $ headersWithJson = preg_grep ( ' ~(?i)^(application/json|[^;/ \t]+/[^;/ \t]+[+]json)[ \t]*(;.*)?$~ ' , $ accept );
88
+ $ headersWithJson = $ this -> selectJsonMimeList ( $ accept );
89
89
if (count ($ headersWithJson ) === 0 ) {
90
90
return implode (', ' , $ accept );
91
91
}
@@ -95,6 +95,34 @@ private function selectAcceptHeader(array $accept): ?string
95
95
return $ this ->getAcceptHeaderWithAdjustedWeight ($ accept , $ headersWithJson );
96
96
}
97
97
98
+ /**
99
+ * Detects whether a string contains a valid JSON mime type
100
+ *
101
+ * @param string $searchString
102
+ * @return bool
103
+ */
104
+ public function isJsonMime (string $ searchString ): bool
105
+ {
106
+ return preg_match ('~^application/(json|[\w!#$&.+-^_]+\+json)\s*(;|$)~ ' , $ searchString ) === 1 ;
107
+ }
108
+
109
+ /**
110
+ * Select all items from a list containing a JSON mime type
111
+ *
112
+ * @param array $mimeList
113
+ * @return array
114
+ */
115
+ private function selectJsonMimeList (array $ mimeList ): array {
116
+ $ jsonMimeList = [];
117
+ foreach ($ mimeList as $ mime ) {
118
+ if ($ this ->isJsonMime ($ mime )) {
119
+ $ jsonMimeList [] = $ mime ;
120
+ }
121
+ }
122
+ return $ jsonMimeList ;
123
+ }
124
+
125
+
98
126
/**
99
127
* Create an Accept header string from the given "Accept" headers array, recalculating all weights
100
128
*
Original file line number Diff line number Diff line change @@ -376,7 +376,7 @@ public function call123TestSpecialTagsRequest($client)
376
376
377
377
// for model (json/xml)
378
378
if (isset ($ client )) {
379
- if ($ headers ['Content-Type ' ] === ' application/json ' ) {
379
+ if ($ this -> headerSelector -> isJsonMime ( $ headers ['Content-Type ' ]) ) {
380
380
$ httpBody = json_encode (ObjectSerializer::sanitizeForSerialization ($ client ));
381
381
} else {
382
382
$ httpBody = $ client ;
@@ -396,7 +396,7 @@ public function call123TestSpecialTagsRequest($client)
396
396
// for HTTP post (form)
397
397
$ httpBody = new MultipartStream ($ multipartContents );
398
398
399
- } elseif ($ headers ['Content-Type ' ] === ' application/json ' ) {
399
+ } elseif ($ this -> headerSelector -> isJsonMime ( $ headers ['Content-Type ' ]) ) {
400
400
$ httpBody = json_encode ($ formParams );
401
401
402
402
} else {
Original file line number Diff line number Diff line change @@ -371,7 +371,7 @@ public function fooGetRequest()
371
371
// for HTTP post (form)
372
372
$ httpBody = new MultipartStream ($ multipartContents );
373
373
374
- } elseif ($ headers ['Content-Type ' ] === ' application/json ' ) {
374
+ } elseif ($ this -> headerSelector -> isJsonMime ( $ headers ['Content-Type ' ]) ) {
375
375
$ httpBody = json_encode ($ formParams );
376
376
377
377
} else {
You can’t perform that action at this time.
0 commit comments