From f8d27d240b2c6d503faeaf779af51669346cec2f Mon Sep 17 00:00:00 2001 From: Patrick Lavigne Date: Tue, 4 Jun 2024 09:15:40 -0400 Subject: [PATCH 1/2] Fix generating constraints on CPP output - 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 - 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 falsyness rather than whether a value was undefined. --- .../language/CPlusPlus/CPlusPlusRenderer.ts | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/packages/quicktype-core/src/language/CPlusPlus/CPlusPlusRenderer.ts b/packages/quicktype-core/src/language/CPlusPlus/CPlusPlusRenderer.ts index 4b70339f2..8c2869c77 100644 --- a/packages/quicktype-core/src/language/CPlusPlus/CPlusPlusRenderer.ts +++ b/packages/quicktype-core/src/language/CPlusPlus/CPlusPlusRenderer.ts @@ -926,23 +926,27 @@ export class CPlusPlusRenderer extends ConvenienceRenderer { }, true, false, - property.isOptional + // Since we're only generating this to compare types, whether its optional doesn't matter - we just + // need cppType() to spit out the actual underlying type, without wrapping it in the optional<> + // container. + false ); + // Explicitly comparing the minMax values to undefined here allows them to have the valid value of zero res.set(jsonName, [ this.constraintMember(jsonName), "(", - minMax?.[0] && cppType === "int64_t" ? String(minMax[0]) : this._nulloptType, + minMax?.[0] !== undefined && cppType === "int64_t" ? String(minMax[0]) : this._nulloptType, ", ", - minMax?.[1] && cppType === "int64_t" ? String(minMax[1]) : this._nulloptType, + minMax?.[1] !== undefined && cppType === "int64_t" ? String(minMax[1]) : this._nulloptType, ", ", - minMax?.[0] && cppType === "double" ? String(minMax[0]) : this._nulloptType, + minMax?.[0] !== undefined && cppType === "double" ? String(minMax[0]) : this._nulloptType, ", ", - minMax?.[1] && cppType === "double" ? String(minMax[1]) : this._nulloptType, + minMax?.[1] !== undefined && cppType === "double" ? String(minMax[1]) : this._nulloptType, ", ", - minMaxLength?.[0] ? String(minMaxLength[0]) : this._nulloptType, + minMaxLength?.[0] !== undefined ? String(minMaxLength[0]) : this._nulloptType, ", ", - minMaxLength?.[1] ? String(minMaxLength[1]) : this._nulloptType, + minMaxLength?.[1] !== undefined ? String(minMaxLength[1]) : this._nulloptType, ", ", pattern === undefined ? this._nulloptType From 585afe0fe54291ab9cac66f97c8917484600a62b Mon Sep 17 00:00:00 2001 From: Patrick Lavigne Date: Tue, 4 Jun 2024 09:25:35 -0400 Subject: [PATCH 2/2] Fix generating constraints on CPP output - Explicitly check for truthy value or zero instead of comparing to undefined, as the value could possibly be null as well --- .../src/language/CPlusPlus/CPlusPlusRenderer.ts | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/packages/quicktype-core/src/language/CPlusPlus/CPlusPlusRenderer.ts b/packages/quicktype-core/src/language/CPlusPlus/CPlusPlusRenderer.ts index 8c2869c77..de41ae263 100644 --- a/packages/quicktype-core/src/language/CPlusPlus/CPlusPlusRenderer.ts +++ b/packages/quicktype-core/src/language/CPlusPlus/CPlusPlusRenderer.ts @@ -932,21 +932,21 @@ export class CPlusPlusRenderer extends ConvenienceRenderer { false ); - // Explicitly comparing the minMax values to undefined here allows them to have the valid value of zero + // Check the minMax values for truthiness or if they're equal to zero, as zero is a valid value too res.set(jsonName, [ this.constraintMember(jsonName), "(", - minMax?.[0] !== undefined && cppType === "int64_t" ? String(minMax[0]) : this._nulloptType, + (minMax?.[0] || minMax?.[0] === 0) && cppType === "int64_t" ? String(minMax[0]) : this._nulloptType, ", ", - minMax?.[1] !== undefined && cppType === "int64_t" ? String(minMax[1]) : this._nulloptType, + (minMax?.[1] || minMax?.[1] === 0) && cppType === "int64_t" ? String(minMax[1]) : this._nulloptType, ", ", - minMax?.[0] !== undefined && cppType === "double" ? String(minMax[0]) : this._nulloptType, + (minMax?.[0] || minMax?.[0] === 0) && cppType === "double" ? String(minMax[0]) : this._nulloptType, ", ", - minMax?.[1] !== undefined && cppType === "double" ? String(minMax[1]) : this._nulloptType, + (minMax?.[1] || minMax?.[1] === 0) && cppType === "double" ? String(minMax[1]) : this._nulloptType, ", ", - minMaxLength?.[0] !== undefined ? String(minMaxLength[0]) : this._nulloptType, + minMaxLength?.[0] || minMaxLength?.[0] === 0 ? String(minMaxLength[0]) : this._nulloptType, ", ", - minMaxLength?.[1] !== undefined ? String(minMaxLength[1]) : this._nulloptType, + minMaxLength?.[1] || minMaxLength?.[1] === 0 ? String(minMaxLength[1]) : this._nulloptType, ", ", pattern === undefined ? this._nulloptType