fix(cpp-httplib-server): generate compilable models for uuid/uri/set and free-form properties

ST
3h ago
+159-17

Several schema shapes made cpp-httplib-server emit model code that does not compile.

Type mappings

  • format: uuid, format: uri and type: string, format: decimal were emitted as the Java type names UUID, URI and BigDecimal inherited from DefaultCodegen ('UUID' does not name a type). They now map to std::string, like the other std::-based C++ generators.
  • uniqueItems: true arrays were already declared as std::vector, but the Set import produced #include "Set.h". set now maps to std::vector (→ #include <vector>).

Member initialization

  • The constructor initializer was derived from isNullable, so every nullable member got std::nullopt, although nullable members are declared unwrapped. This broke free-form {} properties (nlohmann::json optAny(std::nullopt); DefaultCodegen marks AnyType as nullable) and nullable model references (Child c(std::nullopt)). The initializer is now derived from the declared member type; nlohmann::json and model members default-construct (JSON null / default object).
  • Optional nullable enums: dataType was already wrapped by the nullable handling, so the std::optional wrap for optional enums was skipped and the member stayed Enum while being initialized with std::nullopt. They now take the same std::optional<Enum> path as other optional enums.
  • The constructor initializer list is built in Java (memberInitializers), so the leading : is emitted even when the first member has no initializer (previously Foo::Foo() , x("")).

Spec to reproduce (any of these properties fails to compile on master):

components:
  schemas:
    Child:
      type: object
      properties:
        a: { type: string }
    Repro:
      type: object
      properties:
        id:       { type: string, format: uuid }
        link:     { type: string, format: uri }
        amount:   { type: string, format: decimal }
        tags:     { type: array, uniqueItems: true, items: { type: string } }
        anything: {}
        child:    { allOf: [ { $ref: '#/components/schemas/Child' } ], nullable: true }
        state:    { type: string, enum: [a, b], nullable: true }

Testing

  • Added unit tests in CppHttplibServerCodegenModelTest; all CppHttplibServer* tests pass.
  • Regenerated bin/configs/cpp-httplib-server-*.yaml: no changes to the existing samples (only this generator is touched).
  • export_docs_generators.sh: no change to docs/generators/cpp-httplib-server.md.
  • Compiled the generated models for the spec above (plus all nullable primitive/container variants and an allOf inheritance case) with gcc 13 -std=c++17 -Wall -Wextra: no errors or warnings; JSON round-trips behave as expected (free-form values incl. null, nullable enum null → empty optional).

PR checklist

  • Read the contribution guidelines.
  • 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.
  • If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.

C++ technical committee: @ravinikam @stkrwork @etherealjoy @martindelille @muttleyxd @aminya

šŸ¤– Generated with Claude Code


Summary by cubic

Fixes cpp-httplib-server so schema shapes that previously generated uncompilable model code now produce valid C++.

Bug fixes

  • uuid, uri, and decimal string formats now map to std::string instead of the Java type names UUID, URI, and BigDecimal inherited from DefaultCodegen.
  • uniqueItems: true arrays resolve the set mapping to std::vector, so the generated include is <vector> instead of "Set.h".
  • Constructor initializers are now derived from the declared C++ member type rather than isNullable, so nullable members no longer get std::nullopt on unwrapped declarations such as nlohmann::json and model references.
  • Optional nullable enums now take the same std::optional<Enum> path as other optional enums instead of being initialized with std::nullopt while declared unwrapped.
  • The constructor initializer list is now built in Java, so the leading : is emitted even when the first member has no initializer.

Unit tests were added, and regenerating the cpp-httplib-server samples produced no changes.