You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[Optional] Sponsorship to speed up the bug fix or feature request (example)
Description
When a response object contains a property that is:
type: string
nullable: true
Must conform to some defined pattern
Is required in the object
Then, the generated code for that particular model has the following logic (taken from the generated code in file lib/Model/Foo.php of the example below):
/** * Sets bar * * @param string $bar bar * * @return self */publicfunctionsetBar($bar)
{
if ((!preg_match("/^0[xX][a-fA-F0-9]{40}$/", $bar))) {
thrownew \InvalidArgumentException("invalid value for \$bar when calling Foo., must conform to the pattern /^0[xX][a-fA-F0-9]{40}$/.");
}
if (is_null($bar)) {
array_push($this->openAPINullablesSetToNull, 'bar');
} else {
$nullablesSetToNull = $this->getOpenAPINullablesSetToNull();
$index = array_search('bar', $nullablesSetToNull);
if ($index !== FALSE) {
unset($nullablesSetToNull[$index]);
$this->setOpenAPINullablesSetToNull($nullablesSetToNull);
}
}
$this->container['bar'] = $bar;
return$this;
}
Here, even though bar is nullable, when bar == null there is no check for it before the isNullable logic and there is an attempt to preg_match the string with the defined pattern, which throws an exception.
@jebentier@dkarlovi@mandrean@jfastnacht@ybelenko@renepardon
I would like to propose that I can provide the fix for this issue and open a PR. I would appreciate your input on what path I should take regarding the Suggested Fix I stated in the issue.
Bug Report Checklist
Description
When a response object contains a property that is:
type: string
nullable: true
pattern
required
in the objectThen, the generated code for that particular model has the following logic (taken from the generated code in file
lib/Model/Foo.php
of the example below):Here, even though
bar
isnullable
, whenbar == null
there is no check for it before theisNullable
logic and there is an attempt topreg_match
the string with the defined pattern, which throws an exception.openapi-generator version
6.2.1
OpenAPI declaration file content or url
phpbug.yaml
Generation Details
openapi-generator-cli generate -g php -i phpbug.yaml -o phpclient
Suggest a fix
I noticed looking at https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/resources/php/model_generic.mustache#L388 that the main problem here is that the case where a property can be both
nullable
andrequired
, which is completely valid in the specs, isn't handled well in template. I propose that the logic ofisNullable
should be moved to the top of the function, before pattern matching and everything else, or therequired
section in lines like https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/resources/php/model_generic.mustache#L435 should be removed so there is an!is_null
check before trying to match patterns or anything else.The text was updated successfully, but these errors were encountered: