Skip to content

Conversation

liuzicheng1987
Copy link
Contributor

No description provided.

Copy link

Summary of Changes

Hello @liuzicheng1987, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request resolves an issue where the JSON schema generation logic failed to correctly identify optional types when they were nested within rfl::Description or rfl::Validated wrappers. The core change modifies the is_optional utility function to recursively check for optionality through these wrapper types, ensuring that the generated schemas accurately represent the intended optional status of fields. A new comprehensive test case has been introduced to confirm the correct behavior and prevent regressions.

Highlights

  • Fix for Optional Type Detection: The is_optional function in src/rfl/json/to_schema.cpp has been updated to correctly identify types as optional even when they are wrapped by rfl::Description or rfl::Validated types. Previously, these wrappers could obscure the underlying optionality.
  • Improved JSON Schema Generation: This change ensures that the generated JSON schemas accurately reflect the optionality of fields, especially for described or validated optional types, by correctly propagating the optional status through Description and Validated wrappers.
  • New Test Case: A new test file, tests/json/test_json_schema5.cpp, has been added to validate this fix. It demonstrates that fields like rfl::Description<std::optional<std::string>> and rfl::Validator<std::optional<unsigned int>> are now correctly recognized as optional in the generated schema.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request correctly extends the is_optional function to recursively check for optional types within Description and Validated wrappers. This ensures that fields using these wrappers with an optional underlying type are not incorrectly marked as 'required' in the generated JSON schema. The new test case test_json_schema5 effectively validates this fix.

The implementation is clean and uses if constexpr for compile-time branching, which is efficient. I have one minor suggestion to improve the code.

On a related note, I observed that the schema generated for the age field in the new test, which is a Validator over an std::optional, does not seem to include null as a possible type. This might be a separate issue worth investigating to ensure the generated schema fully reflects the optionality of validated fields.

Comment on lines +35 to +46
return _t.variant_.visit([&](const auto& _v) -> bool {
using T = std::remove_cvref_t<decltype(_v)>;
return std::is_same<T, parsing::schema::Type::Optional>();
};
return rfl::visit(handle, _t.variant_);
if constexpr (std::is_same_v<T, parsing::schema::Type::Description>) {
return is_optional(*_v.type_);

} else if constexpr (std::is_same_v<T, parsing::schema::Type::Validated>) {
return is_optional(*_v.type_);

} else {
return std::is_same_v<T, parsing::schema::Type::Optional>;
}
});

Choose a reason for hiding this comment

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

medium

The lambda capture [&] is not necessary here as the lambda body does not access any variables from the enclosing scope. The recursive call to is_optional is a call to a free function and does not require a capture. It's good practice to use a non-capturing lambda [] when no captures are needed as it can sometimes lead to better compiler optimizations.

Suggested change
return _t.variant_.visit([&](const auto& _v) -> bool {
using T = std::remove_cvref_t<decltype(_v)>;
return std::is_same<T, parsing::schema::Type::Optional>();
};
return rfl::visit(handle, _t.variant_);
if constexpr (std::is_same_v<T, parsing::schema::Type::Description>) {
return is_optional(*_v.type_);
} else if constexpr (std::is_same_v<T, parsing::schema::Type::Validated>) {
return is_optional(*_v.type_);
} else {
return std::is_same_v<T, parsing::schema::Type::Optional>;
}
});
return _t.variant_.visit([](const auto& _v) -> bool {
using T = std::remove_cvref_t<decltype(_v)>;
if constexpr (std::is_same_v<T, parsing::schema::Type::Description>) {
return is_optional(*_v.type_);
} else if constexpr (std::is_same_v<T, parsing::schema::Type::Validated>) {
return is_optional(*_v.type_);
} else {
return std::is_same_v<T, parsing::schema::Type::Optional>;
}
});

@liuzicheng1987 liuzicheng1987 merged commit ff9cc71 into main Oct 4, 2025
292 checks passed
@liuzicheng1987 liuzicheng1987 deleted the f/fix_optional_description branch October 4, 2025 06:02
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.

1 participant