Skip to content

Commit 824b2aa

Browse files
[PHP] Allow selecting Content-Type (#13769)
* [PHP] allow selecting Content-Type * [AUTOGENERATED] update samples * [PHP] Fixed tests for HeaderSelector * [PHP] revert Content-Type validation * [AUTOGENERATED] update samples
1 parent a66874f commit 824b2aa

11 files changed

Lines changed: 1219 additions & 1043 deletions

File tree

Lines changed: 12 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
/**
3-
* ApiException
3+
* HeaderSelector
44
* PHP version 7.4
55
*
66
* @category Class
@@ -18,10 +18,8 @@
1818

1919
namespace {{invokerPackage}};
2020

21-
use \Exception;
22-
2321
/**
24-
* ApiException Class Doc Comment
22+
* HeaderSelector Class Doc Comment
2523
*
2624
* @category Class
2725
* @package {{invokerPackage}}
@@ -32,31 +30,25 @@ class HeaderSelector
3230
{
3331
/**
3432
* @param string[] $accept
35-
* @param string[] $contentTypes
36-
* @return array
33+
* @param string $contentType
34+
* @param bool $isMultipart
35+
* @return string[]
3736
*/
38-
public function selectHeaders($accept, $contentTypes)
37+
public function selectHeaders(array $accept, string $contentType, bool $isMultipart): array
3938
{
4039
$headers = [];
4140
4241
$accept = $this->selectAcceptHeader($accept);
4342
if ($accept !== null) {
4443
$headers['Accept'] = $accept;
4544
}
45+
if(!$isMultipart) {
46+
if($contentType === '') {
47+
$contentType = 'application/json';
48+
}
49+
$headers['Content-Type'] = $contentType;
50+
}
4651

47-
$headers['Content-Type'] = $this->selectContentTypeHeader($contentTypes);
48-
return $headers;
49-
}
50-
51-
/**
52-
* @param string[] $accept
53-
* @return array
54-
*/
55-
public function selectHeadersForMultipart($accept)
56-
{
57-
$headers = $this->selectHeaders($accept, []);
58-
59-
unset($headers['Content-Type']);
6052
return $headers;
6153
}
6254

@@ -77,22 +69,4 @@ class HeaderSelector
7769
return implode(',', $accept);
7870
}
7971
}
80-
81-
/**
82-
* Return the content type based on an array of content-type provided
83-
*
84-
* @param string[] $contentType Array fo content-type
85-
*
86-
* @return string Content-Type (e.g. application/json)
87-
*/
88-
private function selectContentTypeHeader($contentType)
89-
{
90-
if (count($contentType) === 0 || (count($contentType) === 1 && $contentType[0] === '')) {
91-
return 'application/json';
92-
} elseif (preg_grep("/application\/json/i", $contentType)) {
93-
return 'application/json';
94-
} else {
95-
return implode(',', $contentType);
96-
}
97-
}
9872
}

modules/openapi-generator/src/main/resources/php/api.mustache

Lines changed: 37 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,16 @@ use {{invokerPackage}}\ObjectSerializer;
6060
*/
6161
protected $hostIndex;
6262
63-
/**
63+
/** @var string[] $contentTypes **/
64+
public const contentTypes = [{{#operation}}
65+
'{{{operationId}}}' => [{{#consumes}}
66+
'{{{mediaType}}}',{{/consumes}}
67+
{{^consumes}}
68+
'application/json',
69+
{{/consumes}} ],{{/operation}}
70+
];
71+
72+
/**
6473
* @param ClientInterface $client
6574
* @param Configuration $config
6675
* @param HeaderSelector $selector
@@ -151,6 +160,7 @@ use {{invokerPackage}}\ObjectSerializer;
151160
* @param array $variables Associative array of variables to pass to the host. Defaults to empty array.
152161
{{/-first}}
153162
{{/servers}}
163+
* @param string $contentType The value for the Content-Type header. Check self::contentTypes['{{{operationId}}}'] to see the possible values for this operation
154164
*
155165
* @throws \{{invokerPackage}}\ApiException on non-2xx response
156166
* @throws \InvalidArgumentException
@@ -159,9 +169,9 @@ use {{invokerPackage}}\ObjectSerializer;
159169
* @deprecated
160170
{{/isDeprecated}}
161171
*/
162-
public function {{operationId}}({{^vendorExtensions.x-group-parameters}}{{#allParams}}${{paramName}}{{^required}} = {{#defaultValue}}{{{.}}}{{/defaultValue}}{{^defaultValue}}null{{/defaultValue}}{{/required}}{{^-last}}, {{/-last}}{{/allParams}}{{#servers}}{{#-first}}{{#allParams}}{{#-first}}, {{/-first}}{{/allParams}}?int $hostIndex = null, array $variables = []{{/-first}}{{/servers}}{{/vendorExtensions.x-group-parameters}}{{#vendorExtensions.x-group-parameters}}$associative_array{{/vendorExtensions.x-group-parameters}})
172+
public function {{operationId}}({{^vendorExtensions.x-group-parameters}}{{#allParams}}${{paramName}}{{^required}} = {{#defaultValue}}{{{.}}}{{/defaultValue}}{{^defaultValue}}null{{/defaultValue}}{{/required}}, {{/allParams}}{{#servers}}{{#-first}}?int $hostIndex = null, array $variables = [], {{/-first}}{{/servers}}string $contentType = self::contentTypes['{{{operationId}}}'][0]{{/vendorExtensions.x-group-parameters}}{{#vendorExtensions.x-group-parameters}}$associative_array{{/vendorExtensions.x-group-parameters}})
163173
{
164-
{{#returnType}}list($response) = {{/returnType}}$this->{{operationId}}WithHttpInfo({{^vendorExtensions.x-group-parameters}}{{#allParams}}${{paramName}}{{^-last}}, {{/-last}}{{/allParams}}{{#servers}}{{#-first}}{{#allParams}}{{#-first}}, {{/-first}}{{/allParams}}$hostIndex, $variables{{/-first}}{{/servers}}{{/vendorExtensions.x-group-parameters}}{{#vendorExtensions.x-group-parameters}}$associative_array{{/vendorExtensions.x-group-parameters}});{{#returnType}}
174+
{{#returnType}}list($response) = {{/returnType}}$this->{{operationId}}WithHttpInfo({{^vendorExtensions.x-group-parameters}}{{#allParams}}${{paramName}}, {{/allParams}}{{#servers}}{{#-first}}$hostIndex, $variables, {{/-first}}{{/servers}}$contentType{{/vendorExtensions.x-group-parameters}}{{#vendorExtensions.x-group-parameters}}$associative_array{{/vendorExtensions.x-group-parameters}});{{#returnType}}
165175
return $response;{{/returnType}}
166176
}
167177

@@ -209,6 +219,7 @@ use {{invokerPackage}}\ObjectSerializer;
209219
* @param array $variables Associative array of variables to pass to the host. Defaults to empty array.
210220
{{/-first}}
211221
{{/servers}}
222+
* @param string $contentType The value for the Content-Type header. Check self::contentTypes['{{{operationId}}}'] to see the possible values for this operation
212223
*
213224
* @throws \{{invokerPackage}}\ApiException on non-2xx response
214225
* @throws \InvalidArgumentException
@@ -217,9 +228,9 @@ use {{invokerPackage}}\ObjectSerializer;
217228
* @deprecated
218229
{{/isDeprecated}}
219230
*/
220-
public function {{operationId}}WithHttpInfo({{^vendorExtensions.x-group-parameters}}{{#allParams}}${{paramName}}{{^required}} = {{#defaultValue}}{{{.}}}{{/defaultValue}}{{^defaultValue}}null{{/defaultValue}}{{/required}}{{^-last}}, {{/-last}}{{/allParams}}{{#servers}}{{#-first}}{{#allParams}}{{#-first}}, {{/-first}}{{/allParams}}?int $hostIndex = null, array $variables = []{{/-first}}{{/servers}}{{/vendorExtensions.x-group-parameters}}{{#vendorExtensions.x-group-parameters}}$associative_array{{/vendorExtensions.x-group-parameters}})
231+
public function {{operationId}}WithHttpInfo({{^vendorExtensions.x-group-parameters}}{{#allParams}}${{paramName}}{{^required}} = {{#defaultValue}}{{{.}}}{{/defaultValue}}{{^defaultValue}}null{{/defaultValue}}{{/required}}, {{/allParams}}{{#servers}}{{#-first}}?int $hostIndex = null, array $variables = [], {{/-first}}{{/servers}}string $contentType = self::contentTypes['{{{operationId}}}'][0]{{/vendorExtensions.x-group-parameters}}{{#vendorExtensions.x-group-parameters}}$associative_array{{/vendorExtensions.x-group-parameters}})
221232
{
222-
$request = $this->{{operationId}}Request({{^vendorExtensions.x-group-parameters}}{{#allParams}}${{paramName}}{{^-last}}, {{/-last}}{{/allParams}}{{#servers}}{{#-first}}{{#allParams}}{{#-first}}, {{/-first}}{{/allParams}}$hostIndex, $variables{{/-first}}{{/servers}}{{/vendorExtensions.x-group-parameters}}{{#vendorExtensions.x-group-parameters}}$associative_array{{/vendorExtensions.x-group-parameters}});
233+
$request = $this->{{operationId}}Request({{^vendorExtensions.x-group-parameters}}{{#allParams}}${{paramName}}, {{/allParams}}{{#servers}}{{#-first}}$hostIndex, $variables, {{/-first}}{{/servers}}$contentType{{/vendorExtensions.x-group-parameters}}{{#vendorExtensions.x-group-parameters}}$associative_array{{/vendorExtensions.x-group-parameters}});
223234

224235
try {
225236
$options = $this->createHttpClientOption();
@@ -367,16 +378,17 @@ use {{invokerPackage}}\ObjectSerializer;
367378
* @param array $variables Associative array of variables to pass to the host. Defaults to empty array.
368379
{{/-first}}
369380
{{/servers}}
381+
* @param string $contentType The value for the Content-Type header. Check self::contentTypes['{{{operationId}}}'] to see the possible values for this operation
370382
*
371383
* @throws \InvalidArgumentException
372384
* @return \GuzzleHttp\Promise\PromiseInterface
373385
{{#isDeprecated}}
374386
* @deprecated
375387
{{/isDeprecated}}
376388
*/
377-
public function {{operationId}}Async({{^vendorExtensions.x-group-parameters}}{{#allParams}}${{paramName}}{{^required}} = {{#defaultValue}}{{{.}}}{{/defaultValue}}{{^defaultValue}}null{{/defaultValue}}{{/required}}{{^-last}}, {{/-last}}{{/allParams}}{{#servers}}{{#-first}}{{#allParams}}{{#-first}}, {{/-first}}{{/allParams}}?int $hostIndex = null, array $variables = []{{/-first}}{{/servers}}{{/vendorExtensions.x-group-parameters}}{{#vendorExtensions.x-group-parameters}}$associative_array{{/vendorExtensions.x-group-parameters}})
389+
public function {{operationId}}Async({{^vendorExtensions.x-group-parameters}}{{#allParams}}${{paramName}}{{^required}} = {{#defaultValue}}{{{.}}}{{/defaultValue}}{{^defaultValue}}null{{/defaultValue}}{{/required}}, {{/allParams}}{{#servers}}{{#-first}}?int $hostIndex = null, array $variables = [], {{/-first}}{{/servers}}string $contentType = self::contentTypes['{{{operationId}}}'][0]{{/vendorExtensions.x-group-parameters}}{{#vendorExtensions.x-group-parameters}}$associative_array{{/vendorExtensions.x-group-parameters}})
378390
{
379-
return $this->{{operationId}}AsyncWithHttpInfo({{^vendorExtensions.x-group-parameters}}{{#allParams}}${{paramName}}{{^-last}}, {{/-last}}{{/allParams}}{{#servers}}{{#-first}}{{#allParams}}{{#-first}}, {{/-first}}{{/allParams}}$hostIndex, $variables{{/-first}}{{/servers}}{{/vendorExtensions.x-group-parameters}}{{#vendorExtensions.x-group-parameters}}$associative_array{{/vendorExtensions.x-group-parameters}})
391+
return $this->{{operationId}}AsyncWithHttpInfo({{^vendorExtensions.x-group-parameters}}{{#allParams}}${{paramName}}, {{/allParams}}{{#servers}}{{#-first}}$hostIndex, $variables, {{/-first}}{{/servers}}$contentType{{/vendorExtensions.x-group-parameters}}{{#vendorExtensions.x-group-parameters}}$associative_array{{/vendorExtensions.x-group-parameters}})
380392
->then(
381393
function ($response) {
382394
return $response[0];
@@ -428,17 +440,18 @@ use {{invokerPackage}}\ObjectSerializer;
428440
* @param array $variables Associative array of variables to pass to the host. Defaults to empty array.
429441
{{/-first}}
430442
{{/servers}}
443+
* @param string $contentType The value for the Content-Type header. Check self::contentTypes['{{{operationId}}}'] to see the possible values for this operation
431444
*
432445
* @throws \InvalidArgumentException
433446
* @return \GuzzleHttp\Promise\PromiseInterface
434447
{{#isDeprecated}}
435448
* @deprecated
436449
{{/isDeprecated}}
437450
*/
438-
public function {{operationId}}AsyncWithHttpInfo({{^vendorExtensions.x-group-parameters}}{{#allParams}}${{paramName}}{{^required}} = {{#defaultValue}}{{{.}}}{{/defaultValue}}{{^defaultValue}}null{{/defaultValue}}{{/required}}{{^-last}}, {{/-last}}{{/allParams}}{{#servers}}{{#-first}}{{#allParams}}{{#-first}}, {{/-first}}{{/allParams}}?int $hostIndex = null, array $variables = []{{/-first}}{{/servers}}{{/vendorExtensions.x-group-parameters}}{{#vendorExtensions.x-group-parameters}}$associative_array{{/vendorExtensions.x-group-parameters}})
451+
public function {{operationId}}AsyncWithHttpInfo({{^vendorExtensions.x-group-parameters}}{{#allParams}}${{paramName}}{{^required}} = {{#defaultValue}}{{{.}}}{{/defaultValue}}{{^defaultValue}}null{{/defaultValue}}{{/required}}, {{/allParams}}{{#servers}}{{#-first}}?int $hostIndex = null, array $variables = [], {{/-first}}{{/servers}}string $contentType = self::contentTypes['{{{operationId}}}'][0]{{/vendorExtensions.x-group-parameters}}{{#vendorExtensions.x-group-parameters}}$associative_array{{/vendorExtensions.x-group-parameters}})
439452
{
440453
$returnType = '{{{returnType}}}';
441-
$request = $this->{{operationId}}Request({{^vendorExtensions.x-group-parameters}}{{#allParams}}${{paramName}}{{^-last}}, {{/-last}}{{/allParams}}{{#servers}}{{#-first}}{{#allParams}}{{#-first}}, {{/-first}}{{/allParams}}$hostIndex, $variables{{/-first}}{{/servers}}{{/vendorExtensions.x-group-parameters}}{{#vendorExtensions.x-group-parameters}}$associative_array{{/vendorExtensions.x-group-parameters}});
454+
$request = $this->{{operationId}}Request({{^vendorExtensions.x-group-parameters}}{{#allParams}}${{paramName}}, {{/allParams}}{{#servers}}{{#-first}}$hostIndex, $variables, {{/-first}}{{/servers}}$contentType{{/vendorExtensions.x-group-parameters}}{{#vendorExtensions.x-group-parameters}}$associative_array{{/vendorExtensions.x-group-parameters}});
442455

443456
return $this->client
444457
->sendAsync($request, $this->createHttpClientOption())
@@ -517,14 +530,15 @@ use {{invokerPackage}}\ObjectSerializer;
517530
* @param array $variables Associative array of variables to pass to the host. Defaults to empty array.
518531
{{/-first}}
519532
{{/servers}}
533+
* @param string $contentType The value for the Content-Type header. Check self::contentTypes['{{{operationId}}}'] to see the possible values for this operation
520534
*
521535
* @throws \InvalidArgumentException
522536
* @return \GuzzleHttp\Psr7\Request
523537
{{#isDeprecated}}
524538
* @deprecated
525539
{{/isDeprecated}}
526540
*/
527-
public function {{operationId}}Request({{^vendorExtensions.x-group-parameters}}{{#allParams}}${{paramName}}{{^required}} = {{#defaultValue}}{{{.}}}{{/defaultValue}}{{^defaultValue}}null{{/defaultValue}}{{/required}}{{^-last}}, {{/-last}}{{/allParams}}{{#servers}}{{#-first}}{{#allParams}}{{#-first}}, {{/-first}}{{/allParams}}?int $hostIndex = null, array $variables = []{{/-first}}{{/servers}}{{/vendorExtensions.x-group-parameters}}{{#vendorExtensions.x-group-parameters}}$associative_array{{/vendorExtensions.x-group-parameters}})
541+
public function {{operationId}}Request({{^vendorExtensions.x-group-parameters}}{{#allParams}}${{paramName}}{{^required}} = {{#defaultValue}}{{{.}}}{{/defaultValue}}{{^defaultValue}}null{{/defaultValue}}{{/required}}, {{/allParams}}{{#servers}}{{#-first}}?int $hostIndex = null, array $variables = [], {{/-first}}{{/servers}}string $contentType = self::contentTypes['{{{operationId}}}'][0]{{/vendorExtensions.x-group-parameters}}{{#vendorExtensions.x-group-parameters}}$associative_array{{/vendorExtensions.x-group-parameters}})
528542
{
529543
{{#vendorExtensions.x-group-parameters}}
530544
// unbox the parameters from the associative array
@@ -533,7 +547,9 @@ use {{invokerPackage}}\ObjectSerializer;
533547
{{/allParams}}{{#servers.0}}
534548
$hostIndex = $associative_array['hostIndex'];
535549
$variables = array_key_exists('variables', $associative_array) ? $associative_array['variables'] : [];
536-
{{/servers.0}}{{/vendorExtensions.x-group-parameters}}{{#allParams}}
550+
{{/servers.0}}
551+
$contentType = $associative_array['contentType'] ?? self::contentTypes['{{{operationId}}}'][0];
552+
{{/vendorExtensions.x-group-parameters}}{{#allParams}}
537553
{{#required}}
538554
// verify the required parameter '{{paramName}}' is set
539555
if (${{paramName}} === null || (is_array(${{paramName}}) && count(${{paramName}}) === 0)) {
@@ -578,9 +594,7 @@ use {{invokerPackage}}\ObjectSerializer;
578594
throw new \InvalidArgumentException('invalid value for "${{paramName}}" when calling {{classname}}.{{operationId}}, number of items must be greater than or equal to {{minItems}}.');
579595
}
580596
{{/minItems}}
581-
582-
{{/hasValidation}}
583-
{{/allParams}}
597+
{{/hasValidation}}{{/allParams}}
584598

585599
$resourcePath = '{{{path}}}';
586600
$formParams = [];
@@ -649,21 +663,17 @@ use {{invokerPackage}}\ObjectSerializer;
649663
}
650664
{{/formParams}}
651665

652-
if ($multipart) {
653-
$headers = $this->headerSelector->selectHeadersForMultipart(
654-
[{{#produces}}'{{{mediaType}}}'{{^-last}}, {{/-last}}{{/produces}}]
655-
);
656-
} else {
657-
$headers = $this->headerSelector->selectHeaders(
658-
[{{#produces}}'{{{mediaType}}}'{{^-last}}, {{/-last}}{{/produces}}],
659-
[{{#consumes}}'{{{mediaType}}}'{{^-last}}, {{/-last}}{{/consumes}}]
660-
);
661-
}
666+
$headers = $this->headerSelector->selectHeaders(
667+
[{{#produces}}'{{{mediaType}}}', {{/produces}}],
668+
$contentType,
669+
$multipart
670+
);
662671

663672
// for model (json/xml)
664673
{{#bodyParams}}
665674
if (isset(${{paramName}})) {
666-
if ($headers['Content-Type'] === 'application/json') {
675+
if (stripos($headers['Content-Type'], 'application/json') !== false) {
676+
# if Content-Type contains "application/json", json_encode the body
667677
$httpBody = \GuzzleHttp\json_encode(ObjectSerializer::sanitizeForSerialization(${{paramName}}));
668678
} else {
669679
$httpBody = ${{paramName}};
@@ -687,9 +697,9 @@ use {{invokerPackage}}\ObjectSerializer;
687697
// for HTTP post (form)
688698
$httpBody = new MultipartStream($multipartContents);
689699

690-
} elseif ($headers['Content-Type'] === 'application/json') {
700+
} elseif (stripos($headers['Content-Type'], 'application/json') !== false) {
701+
# if Content-Type contains "application/json", json_encode the form parameters
691702
$httpBody = \GuzzleHttp\json_encode($formParams);
692-
693703
} else {
694704
// for HTTP post (form)
695705
$httpBody = ObjectSerializer::buildQuery($formParams);

0 commit comments

Comments
 (0)