Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
Fix code in CPlusPlusRenderer that was detecting the C++ type incorrectly, as it was doing it in a way where optional types will never have constraints applied. The code was assuming the
cppType
function would only return a single string, but when an optional value is provided it actually returns an array such as[ 'std::optional', '<', 'int64_t', '>']
. Comparing that against a string will always fail, and whether or not it's optional doesn't actually matter in this case anyway as we're just usingcppType
to detect which kind of constraints to apply to the underlying type.Fix code in CPlusPlusRenderer where constraints with a value of zero (such as a minimum value of 0) would not get applied because the code was just checking for truthiness. Code now checks for truthiness or if the value is explicitly zero, so null and undefined should still be excluded correctly.
Motivation and Context
Generating C++ code from a JSON schema that had constraints would generate code to handle the constraints, but set all the constraint values to null (thus bypassing them entirely).
Previous Behaviour / Output
Given this JSON Schema as input:
Quicktype would generate the following C++ class initializer (details vary depending on settings, but the symptom is always the same):
Note how the constraint objects are initialized entirely with "none" values, or in the case of
test_prop_string_constraint
the 0 is ignored (in the string's case, a minimum length check of 0 isn't actually that useful, but it shows another hidden bug that I can't really demonstrate on the other types in the original code: that if a value of 0 is specified, it's ignored due to only checking for truthiness)New Behaviour / Output
Given the same input above, quicktype now generates the following:
Note that the constraints are actually correctly filled out this time.
How Has This Been Tested?
Tested using the above test schema, as well as the actual schema for my project (which I can't post publicly, sorry).