fix(cpp-qt-client): #23702 - required members are always present in output of toJsonObject#23703
fix(cpp-qt-client): #23702 - required members are always present in output of toJsonObject#23703TheZlodziej wants to merge 3 commits intoOpenAPITools:masterfrom
Conversation
…present in output of toJsonObject
There was a problem hiding this comment.
1 issue found across 1 file
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/cpp-qt-client/model-body.mustache">
<violation number="1" location="modules/openapi-generator/src/main/resources/cpp-qt-client/model-body.mustache:104">
P1: Required primitive fields may serialize uninitialized/indeterminate values after this change</violation>
</file>
Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.
| if (m_{{name}}.isSet()){{/complexType}}{{^complexType}} | ||
| if (m_{{name}}_isSet){{/complexType}} { | ||
| if (true{{^required}} && m_{{name}}.isSet(){{/required}}){{/complexType}}{{^complexType}} | ||
| if (true{{^required}} && m_{{name}}_isSet{{/required}}){{/complexType}} { |
There was a problem hiding this comment.
P1: Required primitive fields may serialize uninitialized/indeterminate values after this change
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/cpp-qt-client/model-body.mustache, line 104:
<comment>Required primitive fields may serialize uninitialized/indeterminate values after this change</comment>
<file context>
@@ -100,11 +100,11 @@ QString {{classname}}::asJson() const {
- if (m_{{name}}.isSet()){{/complexType}}{{^complexType}}
- if (m_{{name}}_isSet){{/complexType}} {
+ if (true{{^required}} && m_{{name}}.isSet(){{/required}}){{/complexType}}{{^complexType}}
+ if (true{{^required}} && m_{{name}}_isSet{{/required}}){{/complexType}} {
obj.insert(QString("{{baseName}}"), ::{{cppNamespace}}::toJsonValue(m_{{name}}));
}{{/isContainer}}{{#isContainer}}
</file context>
|
please follow step 3 to update the samples so as to fix https://github.com/OpenAPITools/openapi-generator/actions/runs/25431058267/job/74599299188?pr=23703 cc @ravinikam (2017/07) @stkrwork (2017/07) @etherealjoy (2018/02) @MartinDelille (2018/03) @muttleyxd (2019/08) @aminya (2025/05) |
There was a problem hiding this comment.
1 issue found across 14 files (changes from recent commits).
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="samples/client/petstore/cpp-qt-addDownloadProgress/client/PFXCategory.cpp">
<violation number="1" location="samples/client/petstore/cpp-qt-addDownloadProgress/client/PFXCategory.cpp:71">
P1: Required `name` is still guarded by `m_name_isSet`, so `toJsonObject()` can omit a schema-required field; `true &&` is a no-op and does not implement the required-member serialization fix.</violation>
</file>
Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.
| obj.insert(QString("id"), ::test_namespace::toJsonValue(m_id)); | ||
| } | ||
| if (m_name_isSet) { | ||
| if (true && m_name_isSet) { |
There was a problem hiding this comment.
P1: Required name is still guarded by m_name_isSet, so toJsonObject() can omit a schema-required field; true && is a no-op and does not implement the required-member serialization fix.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At samples/client/petstore/cpp-qt-addDownloadProgress/client/PFXCategory.cpp, line 71:
<comment>Required `name` is still guarded by `m_name_isSet`, so `toJsonObject()` can omit a schema-required field; `true &&` is a no-op and does not implement the required-member serialization fix.</comment>
<file context>
@@ -65,10 +65,10 @@ QString PFXCategory::asJson() const {
obj.insert(QString("id"), ::test_namespace::toJsonValue(m_id));
}
- if (m_name_isSet) {
+ if (true && m_name_isSet) {
obj.insert(QString("name"), ::test_namespace::toJsonValue(m_name));
}
</file context>
There was a problem hiding this comment.
true && ... convention is just to simplify the mustache template. The compiler will omit this check anyway since it always evaluates to true. In the Category yaml, 'name' is not a required member
Category:
title: Pet category
description: A category for a pet
type: object
properties:
id:
type: integer
format: int64
name:
type: string
xml:
name: Categoryand this the check true && m_name_isSet is correct (you can safely omit this variable according to the schema)
There was a problem hiding this comment.
@TheZlodziej can we avoid the true && with the following instead?
``
{{#required}}
...
{{/required}}
{{^required}}
...
{{/required}}
There was a problem hiding this comment.
Thanks for the feedback! I've saved this as a new learning to improve future reviews.
There was a problem hiding this comment.
@wing328 I tried adding the {{#required}} but it significantly affects readability of the .mustache file which should be the number one priority (since it's generated code). You would either need to rewrite the logic for setting complex/trivial types or add the required check 3 times per complexity type (one for each: if statement, formatting, closing bracket).
The only other acceptable/readable solution is to remove the condition completely and do the inserts inside a scope.
The result would look like following:
QJsonObject PFXPet::asJsonObject() const {
QJsonObject obj;
if (m_id_isSet) {
obj.insert(QString("id"), ::test_namespace::toJsonValue(m_id));
}
if (m_category.isSet()) {
obj.insert(QString("category"), ::test_namespace::toJsonValue(m_category));
}
{
obj.insert(QString("photoUrls"), ::test_namespace::toJsonValue(m_photo_urls));
}
if (m_tags.size() > 0) {
obj.insert(QString("tags"), ::test_namespace::toJsonValue(m_tags));
}
if (m_status_isSet) {
obj.insert(QString("status"), ::test_namespace::toJsonValue(m_status));
}
return obj;
}But this looks as if someone made a mistake removing the condition, if you asked me.
fixes issue mentioned in #23702
the toJsonObject should always contain required variables (even if they are not set)
Summary by cubic
Ensures the
cpp-qt-clientgenerator always emits required model fields in JSON (asJsonObject), even when unset, to avoid missing-required-key errors. Fixes #23702.model-body.mustachesoasJsonObjectinserts required properties unconditionally; optional fields still checkisSet/size()>0.petstorecpp-qtandcpp-qt-addDownloadProgresssamples to reflect the change.Written for commit 8b8702c. Summary will update on new commits.