Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ use GuzzleHttp\Client;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Exception\ConnectException;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Psr7\MultipartStream;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\RequestOptions;
use GuzzleHttp\Promise\PromiseInterface;
Expand All @@ -32,7 +31,6 @@ use Psr\Http\Message\ResponseInterface;
use {{invokerPackage}}\ApiException;
use {{invokerPackage}}\Configuration;
use {{invokerPackage}}\HeaderSelector;
use {{invokerPackage}}\FormDataProcessor;
use {{invokerPackage}}\ObjectSerializer;

/**
Expand Down Expand Up @@ -631,7 +629,8 @@ use {{invokerPackage}}\ObjectSerializer;
$variables = array_key_exists('variables', $associative_array) ? $associative_array['variables'] : [];
{{/servers.0}}
$contentType = $associative_array['contentType'] ?? self::contentTypes['{{{operationId}}}'][0];
{{/exts.x-group-parameters}}{{#allParams}}
{{/exts.x-group-parameters}}
{{#allParams}}
{{#required}}
// verify the required parameter '{{paramName}}' is set
if (${{paramName}} === null || (is_array(${{paramName}}) && count(${{paramName}}) === 0)) {
Expand Down Expand Up @@ -676,11 +675,16 @@ use {{invokerPackage}}\ObjectSerializer;
throw new InvalidArgumentException('invalid value for "${{paramName}}" when calling {{classname}}.{{operationId}}, number of items must be greater than or equal to {{minItems}}.');
}
{{/minItems}}
{{/hasValidation}}{{/allParams}}
{{/hasValidation}}
{{/allParams}}

$resourcePath = '{{{path}}}';
{{#hasBodyOrFormParams}}
$formParams = [];
{{/hasBodyOrFormParams}}
{{#hasQueryParams}}
$queryParams = [];
{{/hasQueryParams}}
$headerParams = [];
$httpBody = '';
$multipart = false;
Expand Down Expand Up @@ -728,7 +732,7 @@ use {{invokerPackage}}\ObjectSerializer;
{{#formParams}}
{{#-first}}
// form params
$formDataProcessor = new FormDataProcessor();
$formDataProcessor = new \{{invokerPackage}}\FormDataProcessor();

$formData = $formDataProcessor->prepare([
{{/-first}}
Expand All @@ -747,6 +751,7 @@ use {{invokerPackage}}\ObjectSerializer;
$multipart
);

{{#hasBodyOrFormParams}}
// for model (json/xml)
{{#bodyParams}}
if (isset(${{paramName}})) {
Expand All @@ -773,7 +778,7 @@ use {{invokerPackage}}\ObjectSerializer;
}
}
// for HTTP post (form)
$httpBody = new MultipartStream($multipartContents);
$httpBody = new \GuzzleHttp\Psr7\MultipartStream($multipartContents);

} elseif (stripos($headers['Content-Type'], 'application/json') !== false) {
# if Content-Type contains "application/json", json_encode the form parameters
Expand All @@ -783,6 +788,7 @@ use {{invokerPackage}}\ObjectSerializer;
$httpBody = ObjectSerializer::buildQuery($formParams);
}
}
{{/hasBodyOrFormParams}}

{{#authMethods}}
{{#isApiKey}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,4 +215,63 @@ public void testDifferentResponseSchemasWithEmpty() throws IOException {
Assert.assertListContains(modelContent, a -> a.equals("): int|string|null"), "Expected to find nullable return type declaration.");
Assert.assertListNotContains(modelContent, a -> a.equals("): ?int|string"), "Expected to not find invalid union type with '?'.");
}

@Test
public void testFormParamsBlockOnlyEmittedWhenBodyOrFormParamsExist() throws IOException {
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
output.deleteOnExit();

OpenAPI openAPI = new OpenAPIParser()
.readLocation("src/test/resources/3_0/php-nextgen/form-body-params.yaml", null, new ParseOptions())
.getOpenAPI();

codegen.setOutputDir(output.getAbsolutePath());
ClientOptInput input = new ClientOptInput()
.openAPI(openAPI)
.config(codegen);

DefaultGenerator generator = new DefaultGenerator();
Map<String, File> files = generator.opts(input).generate().stream()
.collect(Collectors.toMap(File::getName, Function.identity()));

String apiContent = Files.readString(files.get("DefaultApi.php").toPath());

// Each operation produces a "*Request" helper holding its own httpBody handling.
String noBodyNoForm = extractMethod(apiContent, "noBodyNoFormRequest");
String bodyParam = extractMethod(apiContent, "bodyParamRequest");
String formParam = extractMethod(apiContent, "formParamRequest");

// No body, no form: the dead form-params block must not be generated at all.
Assert.assertFalse(noBodyNoForm.contains("if (count($formParams) > 0)"),
"Operation without body or form params must not emit the form-params block");
Assert.assertFalse(noBodyNoForm.contains("// for model (json/xml)"),
"Operation without body or form params must not emit the json/xml comment");

// Body param: emits the body block followed by the elseif form-params branch.
Assert.assertTrue(bodyParam.contains("if (isset($thing)) {"),
"Body param operation must emit the body serialization block");
Assert.assertTrue(bodyParam.contains("} elseif (count($formParams) > 0) {"),
"Body param operation must keep the elseif form-params branch");

// Form param: emits the standalone form-params block.
Assert.assertTrue(formParam.contains("if (count($formParams) > 0) {"),
"Form param operation must emit the standalone form-params block");
Assert.assertFalse(formParam.contains("} elseif (count($formParams) > 0) {"),
"Form-only operation must not have an elseif (no body branch precedes it)");
}

/**
* Extracts the source of a single PHP method (from its declaration up to, but not
* including, the next method declaration) so per-operation assertions don't leak
* across operations sharing the same generated file.
*/
private static String extractMethod(String content, String methodName) {
int start = content.indexOf("function " + methodName + "(");
Assert.assertTrue(start >= 0, "Expected to find method " + methodName + " in generated API");
int next = content.indexOf("\n public function ", start + 1);
if (next < 0) {
next = content.indexOf("\n protected function ", start + 1);
}
return next < 0 ? content.substring(start) : content.substring(start, next);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
openapi: 3.0.0
info:
title: Form and body params test
version: 1.0.0
paths:
/no-body-no-form:
get:
operationId: noBodyNoForm
parameters:
- name: filter
in: query
schema:
type: string
responses:
'200':
description: OK
/body-param:
post:
operationId: bodyParam
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/Thing'
responses:
'200':
description: OK
/form-param:
post:
operationId: formParam
requestBody:
content:
application/x-www-form-urlencoded:
schema:
type: object
properties:
name:
type: string
count:
type: integer
responses:
'200':
description: OK
components:
schemas:
Thing:
type: object
properties:
id:
type: integer
name:
type: string
56 changes: 0 additions & 56 deletions samples/client/echo_api/php-nextgen-streaming/src/Api/AuthApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Exception\ConnectException;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Psr7\MultipartStream;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\RequestOptions;
use GuzzleHttp\Promise\PromiseInterface;
Expand All @@ -41,7 +40,6 @@
use OpenAPI\Client\ApiException;
use OpenAPI\Client\Configuration;
use OpenAPI\Client\HeaderSelector;
use OpenAPI\Client\FormDataProcessor;
use OpenAPI\Client\ObjectSerializer;

/**
Expand Down Expand Up @@ -319,10 +317,7 @@ public function testAuthHttpBasicRequest(
): Request
{


$resourcePath = '/auth/http/basic';
$formParams = [];
$queryParams = [];
$headerParams = [];
$httpBody = '';
$multipart = false;
Expand All @@ -337,30 +332,6 @@ public function testAuthHttpBasicRequest(
$multipart
);

// for model (json/xml)
if (count($formParams) > 0) {
if ($multipart) {
$multipartContents = [];
foreach ($formParams as $formParamName => $formParamValue) {
$formParamValueItems = is_array($formParamValue) ? $formParamValue : [$formParamValue];
foreach ($formParamValueItems as $formParamValueItem) {
$multipartContents[] = [
'name' => $formParamName,
'contents' => $formParamValueItem
];
}
}
// for HTTP post (form)
$httpBody = new MultipartStream($multipartContents);

} elseif (stripos($headers['Content-Type'], 'application/json') !== false) {
# if Content-Type contains "application/json", json_encode the form parameters
$httpBody = \GuzzleHttp\Utils::jsonEncode($formParams);
} else {
// for HTTP post (form)
$httpBody = ObjectSerializer::buildQuery($formParams);
}
}

// this endpoint requires HTTP basic authentication
if (!empty($this->config->getUsername()) || !(empty($this->config->getPassword()))) {
Expand Down Expand Up @@ -578,10 +549,7 @@ public function testAuthHttpBearerRequest(
): Request
{


$resourcePath = '/auth/http/bearer';
$formParams = [];
$queryParams = [];
$headerParams = [];
$httpBody = '';
$multipart = false;
Expand All @@ -596,30 +564,6 @@ public function testAuthHttpBearerRequest(
$multipart
);

// for model (json/xml)
if (count($formParams) > 0) {
if ($multipart) {
$multipartContents = [];
foreach ($formParams as $formParamName => $formParamValue) {
$formParamValueItems = is_array($formParamValue) ? $formParamValue : [$formParamValue];
foreach ($formParamValueItems as $formParamValueItem) {
$multipartContents[] = [
'name' => $formParamName,
'contents' => $formParamValueItem
];
}
}
// for HTTP post (form)
$httpBody = new MultipartStream($multipartContents);

} elseif (stripos($headers['Content-Type'], 'application/json') !== false) {
# if Content-Type contains "application/json", json_encode the form parameters
$httpBody = \GuzzleHttp\Utils::jsonEncode($formParams);
} else {
// for HTTP post (form)
$httpBody = ObjectSerializer::buildQuery($formParams);
}
}

// this endpoint requires Bearer authentication (access token)
if (!empty($this->config->getAccessToken())) {
Expand Down
Loading
Loading