Skip to content

[BUG]: C++ generation of variant types #2756

Open
@Jman0519

Description

@Jman0519

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions