Skip to content

Commit

Permalink
[C++] Error making the cpprestsdk generated files
Browse files Browse the repository at this point in the history
   - for cpp-pistache-server part already, some problem
  • Loading branch information
mlebihan committed May 16, 2024
1 parent 7e94c87 commit 52f5493
Show file tree
Hide file tree
Showing 8 changed files with 10,649 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ public class CodegenModel implements IJsonSchemaValidationProperties {
public boolean isArray;
public boolean hasChildren;
public boolean isMap;
/** datatype is the generic inner parameter of a std::optional for C++, or Optional (Java) */
public boolean isOptional;
public boolean isNull;
public boolean isVoid = false;
/**
Expand Down Expand Up @@ -741,6 +743,16 @@ public void setIsMap(boolean isMap) {
this.isMap = isMap;
}

@Override
public boolean getIsOptional() {
return isOptional;
}

@Override
public void setIsOptional(boolean isOptional) {
this.isOptional = isOptional;
}

@Override
public boolean getIsArray() {
return isArray;
Expand Down Expand Up @@ -1128,6 +1140,7 @@ public boolean equals(Object o) {
isArray == that.isArray &&
hasChildren == that.hasChildren &&
isMap == that.isMap &&
isOptional == that.isOptional &&
isDeprecated == that.isDeprecated &&
hasReadOnly == that.hasReadOnly &&
hasOnlyReadOnly == that.hasOnlyReadOnly &&
Expand Down Expand Up @@ -1221,7 +1234,7 @@ public int hashCode() {
getVars(), getAllVars(), getNonNullableVars(), getRequiredVars(), getOptionalVars(), getReadOnlyVars(), getReadWriteVars(),
getParentVars(), getAllowableValues(), getMandatory(), getAllMandatory(), getImports(), hasVars,
isEmptyVars(), hasMoreModels, hasEnums, isEnum, isNullable, hasRequired, hasOptional, isArray,
hasChildren, isMap, isDeprecated, hasReadOnly, hasOnlyReadOnly, getExternalDocumentation(), getVendorExtensions(),
hasChildren, isMap, isOptional, isDeprecated, hasReadOnly, hasOnlyReadOnly, getExternalDocumentation(), getVendorExtensions(),
getAdditionalPropertiesType(), getMaxProperties(), getMinProperties(), getUniqueItems(), getMaxItems(),
getMinItems(), getMaxLength(), getMinLength(), getExclusiveMinimum(), getExclusiveMaximum(), getMinimum(),
getMaximum(), getPattern(), getMultipleOf(), getItems(), getAdditionalProperties(), getIsModel(),
Expand Down Expand Up @@ -1296,6 +1309,7 @@ public String toString() {
sb.append(", isArray=").append(isArray);
sb.append(", hasChildren=").append(hasChildren);
sb.append(", isMap=").append(isMap);
sb.append(", isOptional=").append(isOptional);
sb.append(", isDeprecated=").append(isDeprecated);
sb.append(", hasReadOnly=").append(hasReadOnly);
sb.append(", hasOnlyReadOnly=").append(hasOnlyReadOnly);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ public class CodegenParameter implements IJsonSchemaValidationProperties {
public boolean isString, isNumeric, isInteger, isLong, isNumber, isFloat, isDouble, isDecimal, isByteArray, isBinary,
isBoolean, isDate, isDateTime, isUuid, isUri, isEmail, isPassword, isFreeFormObject, isAnyType, isShort, isUnboundedInteger;
public boolean isArray, isMap;
/** datatype is the generic inner parameter of a std::optional for C++, or Optional (Java) */
public boolean isOptional;
public boolean isFile;
public boolean isEnum;
public boolean isEnumRef; // true if the enum is a ref (model) but not defined inline
Expand Down Expand Up @@ -261,6 +263,7 @@ public CodegenParameter copy() {
output.isAnyType = this.isAnyType;
output.isArray = this.isArray;
output.isMap = this.isMap;
output.isOptional = this.isOptional;
output.isExplode = this.isExplode;
output.style = this.style;
output.isDeepObject = this.isDeepObject;
Expand All @@ -280,7 +283,7 @@ public int hashCode() {
enumDefaultValue, enumName, style, isDeepObject, isMatrix, isAllowEmptyValue, example, examples,
jsonSchema, isString, isNumeric, isInteger, isLong, isNumber, isFloat, isDouble, isDecimal,
isByteArray, isBinary, isBoolean, isDate, isDateTime, isUuid, isUri, isEmail, isPassword,
isFreeFormObject, isAnyType, isArray, isMap, isFile, isEnum, isEnumRef, _enum, allowableValues,
isFreeFormObject, isAnyType, isArray, isMap, isOptional, isFile, isEnum, isEnumRef, _enum, allowableValues,
items, mostInnerItems, additionalProperties, vars, requiredVars, vendorExtensions, hasValidation,
getMaxProperties(), getMinProperties(), isNullable, isDeprecated, required, getMaximum(),
getExclusiveMaximum(), getMinimum(), getExclusiveMinimum(), getMaxLength(), getMinLength(),
Expand Down Expand Up @@ -330,6 +333,7 @@ public boolean equals(Object o) {
isAnyType == that.isAnyType &&
isArray == that.isArray &&
isMap == that.isMap &&
isOptional == that.isOptional &&
isFile == that.isFile &&
isEnum == that.isEnum &&
isEnumRef == that.isEnumRef &&
Expand Down Expand Up @@ -463,6 +467,7 @@ public String toString() {
sb.append(", isAnyType=").append(isAnyType);
sb.append(", isArray=").append(isArray);
sb.append(", isMap=").append(isMap);
sb.append(", isOptional=").append(isOptional);
sb.append(", isFile=").append(isFile);
sb.append(", isEnum=").append(isEnum);
sb.append(", isEnumRef=").append(isEnumRef);
Expand Down Expand Up @@ -755,6 +760,16 @@ public void setIsMap(boolean isMap) {
this.isMap = isMap;
}

@Override
public boolean getIsOptional() {
return isOptional;
}

@Override
public void setIsOptional(boolean isOptional) {
this.isOptional = isOptional;
}

@Override
public boolean getIsArray() {
return isArray;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,8 @@ public class CodegenProperty implements Cloneable, IJsonSchemaValidationProperti
public boolean isAnyType;
public boolean isArray;
public boolean isMap;
/** datatype is the generic inner parameter of a std::optional for C++, or Optional (Java) */
public boolean isOptional;
public boolean isEnum; // true if the enum is defined inline
public boolean isInnerEnum; // Enums declared inline will be located inside the generic model, changing how the enum is referenced in some cases.
public boolean isEnumRef; // true if it's a reference to an enum
Expand Down Expand Up @@ -625,6 +627,16 @@ public void setIsMap(boolean isMap) {
this.isMap = isMap;
}

@Override
public boolean getIsOptional() {
return isOptional;
}

@Override
public void setIsOptional(boolean isOptional) {
this.isOptional = isOptional;
}

@Override
public boolean getIsArray() {
return isArray;
Expand Down Expand Up @@ -1200,6 +1212,7 @@ public String toString() {
sb.append(", isFreeFormObject=").append(isFreeFormObject);
sb.append(", isArray=").append(isArray);
sb.append(", isMap=").append(isMap);
sb.append(", isOptional=").append(isOptional);
sb.append(", isEnum=").append(isEnum);
sb.append(", isInnerEnum=").append(isInnerEnum);
sb.append(", isEnumRef=").append(isEnumRef);
Expand Down Expand Up @@ -1295,6 +1308,7 @@ public boolean equals(Object o) {
isFreeFormObject == that.isFreeFormObject &&
isArray == that.isArray &&
isMap == that.isMap &&
isOptional == that.isOptional &&
isEnum == that.isEnum &&
isInnerEnum == that.isInnerEnum &&
isEnumRef == that.isEnumRef &&
Expand Down Expand Up @@ -1386,7 +1400,7 @@ public int hashCode() {
hasMoreNonReadOnly, isPrimitiveType, isModel, isContainer, isString, isNumeric,
isInteger, isLong, isNumber, isFloat, isDouble, isDecimal, isByteArray, isBinary, isFile,
isBoolean, isDate, isDateTime, isUuid, isUri, isEmail, isPassword, isFreeFormObject,
isArray, isMap, isEnum, isInnerEnum, isEnumRef, isAnyType, isReadOnly, isWriteOnly, isNullable, isShort,
isArray, isMap, isOptional, isEnum, isInnerEnum, isEnumRef, isAnyType, isReadOnly, isWriteOnly, isNullable, isShort,
isUnboundedInteger, isSelfReference, isCircularReference, isDiscriminator, isNew, isOverridden, _enum,
allowableValues, items, mostInnerItems, additionalProperties, vars, requiredVars,
vendorExtensions, hasValidation, isInherited, discriminatorValue, nameInPascalCase, nameInCamelCase,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ public class CodegenResponse implements IJsonSchemaValidationProperties {
public boolean simpleType;
public boolean primitiveType;
public boolean isMap;
/** datatype is the generic inner parameter of a std::optional for C++, or Optional (Java) */
public boolean isOptional;
public boolean isArray;
public boolean isBinary = false;
public boolean isFile = false;
Expand Down Expand Up @@ -103,7 +105,7 @@ public int hashCode() {
return Objects.hash(headers, code, message, examples, dataType, baseType, containerType, containerTypeMapped, hasHeaders,
isString, isNumeric, isInteger, isLong, isNumber, isFloat, isDouble, isDecimal, isByteArray, isBoolean, isDate,
isDateTime, isUuid, isEmail, isPassword, isModel, isFreeFormObject, isAnyType, isDefault, simpleType, primitiveType,
isMap, isArray, isBinary, isFile, schema, jsonSchema, vendorExtensions, items, additionalProperties,
isMap, isOptional, isArray, isBinary, isFile, schema, jsonSchema, vendorExtensions, items, additionalProperties,
vars, requiredVars, isNull, isVoid, hasValidation, isShort, isUnboundedInteger,
getMaxProperties(), getMinProperties(), uniqueItems, getMaxItems(), getMinItems(), getMaxLength(),
getMinLength(), exclusiveMinimum, exclusiveMaximum, getMinimum(), getMaximum(), getPattern(),
Expand Down Expand Up @@ -142,6 +144,7 @@ public boolean equals(Object o) {
simpleType == that.simpleType &&
primitiveType == that.primitiveType &&
isMap == that.isMap &&
isOptional == that.isOptional &&
isArray == that.isArray &&
isBinary == that.isBinary &&
isFile == that.isFile &&
Expand Down Expand Up @@ -503,6 +506,16 @@ public void setIsMap(boolean isMap) {
this.isMap = isMap;
}

@Override
public boolean getIsOptional() {
return isOptional;
}

@Override
public void setIsOptional(boolean isOptional) {
this.isOptional = isOptional;
}

@Override
public CodegenProperty getAdditionalProperties() {
return additionalProperties;
Expand Down Expand Up @@ -594,6 +607,7 @@ public String toString() {
sb.append(", simpleType=").append(simpleType);
sb.append(", primitiveType=").append(primitiveType);
sb.append(", isMap=").append(isMap);
sb.append(", isOptional=").append(isOptional);
sb.append(", isArray=").append(isArray);
sb.append(", isBinary=").append(isBinary);
sb.append(", isFile=").append(isFile);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,18 @@ public interface IJsonSchemaValidationProperties {

void setIsMap(boolean isMap);

/**
* Tells if the datatype is a generic inner parameter of a <code>std::optional</code> for C++, or <code>Optional</code> (Java)<br>
* to resolve cases (detected in issue #6726) where :<br>
* - <code>categoryOneOf</code> is a parameter of class <code>GetAccountVideos_categoryOneOf_parameter</code>, a model parameter that correctly prefixed by its namespace: <code>org::openapitools::server::model::GetAccountVideos_categoryOneOf_parameter</code><br>
* - but that <code>GetAccountVideos_categoryOneOf_parameter</code> class is inside an <code>std::optional</code><br>
* <br>
* Then a correct generation of that parameter can be (for C++) <code>const org::openapitools::server::model::std::optional<org::openapitools::server::model::GetAccountVideos_categoryOneOf_parameter> &categoryOneOf</code><br>
* but using #isModel alone without #isOptional in mustache might produce <code>const org::openapitools::server::model::std::optional<org::openapitools::server::model::GetAccountVideos_categoryOneOf_parameter> &categoryOneOf</code> instead, that do not compile.
*/
boolean getIsOptional();
void setIsOptional(boolean isOptional);

boolean getIsArray();

void setIsArray(boolean isShort);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -356,13 +356,17 @@ private void postProcessSingleOperation(OperationMap operations, CodegenOperatio
* data types for Header and Query parameters.
* @param param CodegenParameter to be modified.
*/
private static void postProcessSingleParam(CodegenParameter param) {
private void postProcessSingleParam(CodegenParameter param) {
//TODO: This changes the info about the real type but it is needed to parse the header params
if (param.isHeaderParam) {
param.dataType = "std::optional<Pistache::Http::Header::Raw>";
param.baseType = "std::optional<Pistache::Http::Header::Raw>";
} else if (param.isQueryParam) {
param.dataType = "std::optional<" + param.dataType + ">";
String dataTypeWithNamespace = param.isPrimitiveType ? param.dataType : prefixWithNameSpaceIfNeeded(param.dataType);

param.dataType = "std::optional<" + dataTypeWithNamespace + ">";
param.isOptional = true;

if (!param.isPrimitiveType) {
param.baseType = "std::optional<" + param.baseType + ">";
}
Expand Down Expand Up @@ -434,8 +438,17 @@ public String getTypeDeclaration(Schema p) {
return toModelName(openAPIType);
}

return prefixWithNameSpaceIfNeeded(openAPIType);
}

/**
* Prefix an open API type with a namespace or not, depending of its current type and if it is on a list to avoid it.
* @param openAPIType Open API Type.
* @return type prefixed with the namespace or not.
*/
private String prefixWithNameSpaceIfNeeded(String openAPIType) {
// Some types might not support namespace
if (this.openAPITypesWithoutModelNamespace.contains(openAPIType)) {
if (this.openAPITypesWithoutModelNamespace.contains(openAPIType) || openAPIType.startsWith("std::")) {
return openAPIType;
}
else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public class ObjectAnyTypeSetTest extends AbstractGeneratorsTest {

/** A Petstore inputspec with abstract properties added in the Pet */
private static final String INPUT_SPEC = "src/test/resources/3_0/issues-anytype-object-set-petstore-everything.yaml";
private static final String ISSUE_6726 = "src/test/resources/3_0/issue_6726.yaml";

/** Soft assert to check all the generators before eventually failing a test */
private final SoftAssert softAssert = new SoftAssert();
Expand All @@ -44,19 +45,20 @@ public class ObjectAnyTypeSetTest extends AbstractGeneratorsTest {
public void testSomeWithPetstoreWithAbstract() throws IOException {
// assertGeneratedFiles("c");
// assertGeneratedFiles("cpp-restsdk");
generateFiles("cpp-pistache-server");
generateFiles("cpp-pistache-server", ISSUE_6726);
// assertGeneratedFiles("typescript");
this.softAssert.assertAll();
}

/**
* Asserts that a generator has produced some files
* @param generatorName The generator name to test
* @param inputSpec The inputspec to use.
* @return List of files generated
* @throws IOException if the test folder cannot be created
*/
private List<File> generateFiles(String generatorName) throws IOException {
private List<File> generateFiles(String generatorName, String inputSpec) throws IOException {
Objects.requireNonNull(generatorName, "A generator name is expected for this assertion");
return oneWith(generatorName, INPUT_SPEC);
return oneWith(generatorName, inputSpec);
}
}
Loading

0 comments on commit 52f5493

Please sign in to comment.