Skip to content

fix(cpp-qt-client): #23702 - required members are always present in output of toJsonObject#23703

Open
TheZlodziej wants to merge 3 commits intoOpenAPITools:masterfrom
TheZlodziej:master
Open

fix(cpp-qt-client): #23702 - required members are always present in output of toJsonObject#23703
TheZlodziej wants to merge 3 commits intoOpenAPITools:masterfrom
TheZlodziej:master

Conversation

@TheZlodziej
Copy link
Copy Markdown

@TheZlodziej TheZlodziej commented May 6, 2026

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-client generator always emits required model fields in JSON (asJsonObject), even when unset, to avoid missing-required-key errors. Fixes #23702.

  • Bug Fixes
    • Updated model-body.mustache so asJsonObject inserts required properties unconditionally; optional fields still check isSet/size()>0.
    • Regenerated petstore cpp-qt and cpp-qt-addDownloadProgress samples to reflect the change.

Written for commit 8b8702c. 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 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}} {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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>

@wing328
Copy link
Copy Markdown
Member

wing328 commented May 6, 2026

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)

@wing328 wing328 added this to the 7.23.0 milestone May 6, 2026
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 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) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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>

Copy link
Copy Markdown
Author

@TheZlodziej TheZlodziej May 8, 2026

Choose a reason for hiding this comment

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

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: Category

and this the check true && m_name_isSet is correct (you can safely omit this variable according to the schema)

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.

@TheZlodziej can we avoid the true && with the following instead?

``
{{#required}}
...
{{/required}}
{{^required}}
...
{{/required}}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for the feedback! I've saved this as a new learning to improve future reviews.

Copy link
Copy Markdown
Author

@TheZlodziej TheZlodziej May 8, 2026

Choose a reason for hiding this comment

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

@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.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG][cpp-qt-client] Required array member is not outputted to JSON

2 participants