fix(cpp-httplib-server): generate compilable models for uuid/uri/set and free-form properties
Ā
ST
3h ago+159+159+012345678901234567890123456789-17-17-01234567890123456789
Several schema shapes made cpp-httplib-server emit model code that does not compile.
Type mappings
format: uuid,format: uriandtype: string, format: decimalwere emitted as the Java type namesUUID,URIandBigDecimalinherited fromDefaultCodegen('UUID' does not name a type). They now map tostd::string, like the otherstd::-based C++ generators.uniqueItems: truearrays were already declared asstd::vector, but theSetimport produced#include "Set.h".setnow maps tostd::vector(ā#include <vector>).
Member initialization
- The constructor initializer was derived from
isNullable, so every nullable member gotstd::nullopt, although nullable members are declared unwrapped. This broke free-form{}properties (nlohmann::json optAny(std::nullopt);DefaultCodegenmarks AnyType as nullable) and nullable model references (Child c(std::nullopt)). The initializer is now derived from the declared member type;nlohmann::jsonand model members default-construct (JSONnull/ default object). - Optional nullable enums:
dataTypewas already wrapped by the nullable handling, so thestd::optionalwrap for optional enums was skipped and the member stayedEnumwhile being initialized withstd::nullopt. They now take the samestd::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 (previouslyFoo::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; allCppHttplibServer*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 todocs/generators/cpp-httplib-server.md.- Compiled the generated models for the spec above (plus all nullable primitive/container variants and an
allOfinheritance 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 enumnullā empty optional).
PR checklist
- Read the contribution guidelines.
- Run the following to build the project and update samples:
(For Windows users, please run the script in WSL)./mvnw clean package || exit ./bin/generate-samples.sh ./bin/configs/*.yaml || exit ./bin/utils/export_docs_generators.sh || exit
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, anddecimalstring formats now map tostd::stringinstead of the Java type namesUUID,URI, andBigDecimalinherited fromDefaultCodegen.uniqueItems: truearrays resolve thesetmapping tostd::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 getstd::nullopton unwrapped declarations such asnlohmann::jsonand model references. - Optional nullable enums now take the same
std::optional<Enum>path as other optional enums instead of being initialized withstd::nulloptwhile 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.