quicktype icon indicating copy to clipboard operation
quicktype copied to clipboard

[BUG]: C++ generation of variant types

Open Jman0519 opened this issue 5 months ago • 0 comments

Issue Type

When generating C++ code from a schema that should be a variant of two types, quicktype generates one type full of optionals.

Context (Environment, Version, Language)

Input Format: Schema Output Language: C++

CLI, npm, or app.quicktype.io: all Version: 23.0.171

Command: quicktype --lang c++ --src-lang schema --out ./out.hpp --src ./test.schema.json -t Test --namespace Test--no-boost --source-style single-source --include-location global-include --member-style camel-case --type-style pascal-case-upper-acronyms --code-format with-struct --telemetry disable --no-combine-classes

Description

I am trying to parse an array of multiple (non-primitive) types. This bug causes my code to be unable to differentiate between the elements of the std::vector that quicktype generates. I could std::visit a vector defined as std::vector<std::variant<type1, type2, type3>> but not a std::vector<typeFullOfOptionals>

Input Data

Minimum reproducible example:{ "$schema": "http://json-schema.org/draft-06/schema#", "$ref": "#/$defs/vars", "$defs": { "person": { "type": "object", "additionalProperties": false, "properties": { "name": { "type": "string" }, "age": { "type": "integer" } }, "required": [ "name", "age"] }, "car": { "type": "object", "additionalProperties": false, "properties": { "brand": { "type": "string" }, "year": { "type": "integer" } }, "required": ["brand", "year"] }, "vars": { "anyOf": [ {"$ref": "#/$defs/person"}, {"$ref": "#/$defs/car"}] } } }

Expected Behaviour / Output

Generates: ``` struct Person = {std::string name, int age}; struct Car = {std::string brand, int year}; struct Vars = std::variant<Person, Car>;


## Current Behaviour / Output

<!--- Tell us what happens instead of the expected behavior / what output is currently generated -->
```#pragma once

#include <optional>

struct Coordinate {
    std::optional<int64_t> age;
    std::optional<std::string> name;
    std::optional<std::string> brand;
    std::optional<int64_t> year;
};

Steps to Reproduce

run the following bash command:

echo '{ "$schema": "http://json-schema.org/draft-06/schema#", "$ref": "#/$defs/vars", "$defs": { "person": { "type": "object", "additionalProperties": false, "properties": { "name": { "type": "string" }, "age": { "type": "integer" } }, "required": [ "name", "age"] }, "car": { "type": "object", "additionalProperties": false, "properties": { "brand": { "type": "string" }, "year": { "type": "integer" } }, "required": ["brand", "year"] }, "vars": { "anyOf": [ {"$ref": "#/$defs/person"}, {"$ref": "#/$defs/car"}] } }}' | quicktype --lang c++ --src-lang schema --out test.hpp --no-boost --just-types --code-format with-struct

Possible Solution

Jman0519 avatar May 20 '25 18:05 Jman0519