Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG]: Getters for optional values in C++ should return a const reference #2674

Open
flounderpinto opened this issue Jan 14, 2025 · 0 comments · May be fixed by #2675
Open

[BUG]: Getters for optional values in C++ should return a const reference #2674

flounderpinto opened this issue Jan 14, 2025 · 0 comments · May be fixed by #2675
Labels

Comments

@flounderpinto
Copy link

flounderpinto commented Jan 14, 2025

Currently, the get_xyz() methods return a copy of optional values. Something like:
boost::optional<std::vector<Coordinate>> get_coordinates() const { return coordinates; }
Which makes this code not behave as expected:
for (auto coordinate: test.get_coordinates()) std::cout << "latitude" << coordinate.get_latitude() << std::endl;
Required getters work as expected:
'const std::vector & get_coordinates_required() const { return coordinates_required; }'

Issue Type

This is an issue with quicktype C++ code generation.

Context (Environment, Version, Language)

Input Format: Json Scheam
Output Language: C++

CLI, npm, or app.quicktype.io:
Version: Current

Description

Returning a copy of the boost::optional means that user's can't iterate through the vector as they might initially expect, leading to bugs. It's also presumably less efficient that returning a const reference to the underlying object.

Input Data

{ { "$schema": "http://json-schema.org/draft-06/schema#", "$ref": "#/definitions/foo", "definitions": { "coordinate": { "type": "object", "required": [ "latitude", "longitude" ], "additionalProperties": false, "properties": { "latitude": { "type": "number" }, "longitude": { "type": "number" } } }, "coordinates": { "type": "array", "items": { "$ref": "#/definitions/coordinate" } }, "foo": { "type": "object", "required": [ "coordinates-required" ], "additionalProperties": false, "properties": { "coordinates": { "$ref": "#/definitions/coordinates" }, "coordinates-required": { "$ref": "#/definitions/coordinates" }, "bar" : { "type": "number" } } } } }

Expected Behaviour / Output

const boost::optional<std::vector<Coordinate>>& get_coordinates() const { return coordinates; }

Current Behaviour / Output

boost::optional<std::vector<Coordinate>> get_coordinates() const { return coordinates; }

Steps to Reproduce

Using above schema;
quicktype --lang c++ -s schema --out ./test.h ./test.schema.json
(or insert into app.quicktype.io)

Possible Solution

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant