Skip to content

Conversation

@BigMichi1
Copy link

@BigMichi1 BigMichi1 commented Jan 23, 2026

Add OneOf/discriminator support for C++ Qt client generator

Summary

Adds support for OpenAPI oneOf schemas with discriminators in the C++ Qt client generator. This implementation uses C++17 std::variant to represent polymorphic types in a type-safe manner.

Changes Made

New Template Files

  • model-oneof.mustache - Header template for oneOf models with discriminator

    • Declares variant type containing all mapped model types
    • Provides type checking methods (isModelName()) and conversion methods (asModelName())
    • Uses std::variant with std::monostate for uninitialized state
  • model-oneof-body.mustache - Implementation template for oneOf models

    • Implements discriminator-based deserialization from JSON
    • Type-safe variant handling using std::visit
    • Automatic discriminator value setting when storing instances
    • Validation that delegates to the actual instance

Modified Template Files

  • model-body.mustache

    • Added conditional logic to use oneOf template for models with discriminators
    • Added #include <QJsonValue> for JSON handling
  • model-header.mustache

    • Added conditional logic to use oneOf header template for models with discriminators
    • Ensures proper template routing based on discriminator presence

Key Features

  • ✅ Type-safe polymorphic model representation using std::variant
  • ✅ Automatic discriminator field handling during serialization/deserialization
  • ✅ Runtime type checking with isModelName() methods
  • ✅ Safe type conversion with asModelName() methods returning shared_ptr
  • ✅ Proper validation delegation to actual instance types
  • ✅ C++17 compliant implementation

Technical Details

  • Uses std::variant<std::monostate, shared_ptr<Type1>, shared_ptr<Type2>, ...> for type safety
  • Discriminator property is automatically included in JSON output
  • Utilizes std::visit for variant pattern matching
  • Falls back to warning log for unknown discriminator values

Related Issue

@aminya

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

Adds oneOf/discriminator support to the C++ Qt client generator using C++17 std::variant. Enables type-safe polymorphic models with automatic discriminator handling during JSON (de)serialization. Relates to #6378.

  • New Features

    • Generate oneOf model templates (model-oneof.mustache, model-oneof-body.mustache).
    • Use std::variant<std::monostate, std::shared_ptr<...>> to store mapped models.
    • Deserialize by discriminator value; serialize with discriminator included.
    • Add isModelName() and asModelName() helpers for runtime type checks.
    • Delegate validation to the active instance.
  • Refactors

    • Route models with discriminators to oneOf templates in model-header.mustache and model-body.mustache.
    • Add QJsonValue include for JSON handling.

Written for commit bfce96b. Summary will update on new commits.

@aminya
Copy link
Contributor

aminya commented Jan 23, 2026

The tests need to be updated @BigMichi1

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