Skip to content

[php-nextgen] Fix validity checks for nullable properties that are required#23419

Open
JulianVennen wants to merge 2 commits intoOpenAPITools:masterfrom
JulianVennen:invalid-nullable
Open

[php-nextgen] Fix validity checks for nullable properties that are required#23419
JulianVennen wants to merge 2 commits intoOpenAPITools:masterfrom
JulianVennen:invalid-nullable

Conversation

@JulianVennen
Copy link
Copy Markdown
Contributor

@JulianVennen JulianVennen commented Apr 1, 2026

If you have properties that are required which are set to null, the listInvalidProperties method will return them as invalid, even if they are nullable and therefore valid.
This seemingly was fixed for nullable properties without validation in the main php generator, but when using the php-nextgen generator (or the php generator with properties that have validation), the problem persisted.

I also updated the message for nullable properties, as the previous one didn't really make sense.

PR checklist

  • Read the contribution guidelines.
  • Pull Request title clearly describes the work in the pull request and Pull Request description provides details about how to validate the work. Missing information here may result in delayed response from the community.
  • Run the following to build the project and update samples:
    ./mvnw clean package || exit
    ./bin/generate-samples.sh ./bin/configs/*.yaml || exit
    ./bin/utils/export_docs_generators.sh || exit
    
    (For Windows users, please run the script in WSL)
    Commit all changed files.
    This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
    These must match the expectations made by your contribution.
    You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example ./bin/generate-samples.sh bin/configs/java*.
    IMPORTANT: Do NOT purge/delete any folders/files (e.g. tests) when regenerating the samples as manually written tests may be removed.
  • File the PR against the correct branch: master (upcoming 7.x.0 minor release - breaking changes with fallbacks), 8.0.x (breaking changes without fallbacks)
  • If your PR solves a reported issue, reference it using GitHub's linking syntax (e.g., having "fixes #123" present in the PR description)
  • If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.

Summary by cubic

Fixes validation in generated PHP models so required nullable properties set to null are not flagged as invalid. Also clarifies the error for missing required nullable fields.

  • Bug Fixes
    • Respect isNullableSetToNull() for required nullable props; null set explicitly is valid.
    • Skip length/pattern/range/item checks when value is null and the field is nullable or not required.
    • Update message: non-nullable required → "can't be null"; nullable required and missing → "is required".
    • Apply the same logic to both php-nextgen and php templates.

Written for commit 5d1bc86. Summary will update on new commits.

Copy link
Copy Markdown
Contributor

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 issue found across 2 files

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="modules/openapi-generator/src/main/resources/php-nextgen/model_generic.mustache">

<violation number="1" location="modules/openapi-generator/src/main/resources/php-nextgen/model_generic.mustache:277">
P1: Required+nullable validation now relies on nullable-null tracking, but `ArrayAccess` setters/unsetters bypass that tracker, causing inconsistent and incorrect validation results.</violation>
</file>

Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.

{{#required}}
if ($this->container['{{name}}'] === null) {
$invalidProperties[] = "'{{name}}' can't be null";
if ($this->container['{{name}}'] === null{{#isNullable}} && !$this->isNullableSetToNull('{{name}}'){{/isNullable}}) {
Copy link
Copy Markdown
Contributor

@cubic-dev-ai cubic-dev-ai bot Apr 1, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1: Required+nullable validation now relies on nullable-null tracking, but ArrayAccess setters/unsetters bypass that tracker, causing inconsistent and incorrect validation results.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At modules/openapi-generator/src/main/resources/php-nextgen/model_generic.mustache, line 277:

<comment>Required+nullable validation now relies on nullable-null tracking, but `ArrayAccess` setters/unsetters bypass that tracker, causing inconsistent and incorrect validation results.</comment>

<file context>
@@ -274,8 +274,8 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}}{{/parentSchema}}{{^par
         {{#required}}
-        if ($this->container['{{name}}'] === null) {
-            $invalidProperties[] = "'{{name}}' can't be null";
+        if ($this->container['{{name}}'] === null{{#isNullable}} && !$this->isNullableSetToNull('{{name}}'){{/isNullable}}) {
+            $invalidProperties[] = "'{{name}}' {{^isNullable}}can't be null"{{/isNullable}}{{#isNullable}}is required"{{/isNullable}};
         }
</file context>
Fix with Cubic

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JulianVennen thanks for the PR

can you please review this feedback which seems to be valid?

Copy link
Copy Markdown
Contributor Author

@JulianVennen JulianVennen Apr 2, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using $model['key'] = null does indeed not update the openAPINullablesSetToNull. It also doesn't do any other verification on whether the value is even nullable or of the correct type. I can update it in there, but I' not sure whether I should throw an exception if the variable is not nullable, as that might be considered a breaking change.

Using the normal model setters ($model->setKey(null)) does update it and also validates the parameter type.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants