Skip to content

Commit

Permalink
Add name in various cases in property, parameter names (OpenAPITools#…
Browse files Browse the repository at this point in the history
…18139)

* create nameInPascalCase

* add back name in camel case

* update abstract cpp codegen

* update erlang templates, update samples

* update tests

* add param names in various cases

* clean up

* fix
  • Loading branch information
wing328 authored and zapodot committed Mar 21, 2024
1 parent fd918f7 commit 829b953
Show file tree
Hide file tree
Showing 76 changed files with 219 additions and 164 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ public class CodegenParameter implements IJsonSchemaValidationProperties {
collectionFormat, description, unescapedDescription, baseType, defaultValue, enumDefaultValue, enumName, style;

public String nameInLowerCase; // property name in lower case
public String nameInCamelCase; // property name in camel case (e.g. modifiedDate)
public String nameInPascalCase; // property name in pascal case (e.g. ModifiedDate)
public String nameInSnakeCase; // property name in upper snake case
public String example; // example value (x-example)
public Map<String, Example> examples;
public String jsonSchema;
Expand Down Expand Up @@ -184,6 +187,11 @@ public CodegenParameter copy() {
output.additionalProperties = this.additionalProperties;
output.isNull = this.isNull;
output.isVoid = this.isVoid;
output.nameInPascalCase = this.nameInPascalCase;
output.nameInCamelCase = this.nameInCamelCase;
output.nameInLowerCase = this.nameInLowerCase;
output.nameInSnakeCase = this.nameInSnakeCase;

output.setAdditionalPropertiesIsAnyType(this.getAdditionalPropertiesIsAnyType());
output.setHasVars(this.hasVars);
output.setHasRequired(this.hasRequired);
Expand Down Expand Up @@ -265,7 +273,22 @@ public CodegenParameter copy() {

@Override
public int hashCode() {
return Objects.hash(isFormParam, isQueryParam, isPathParam, isHeaderParam, isCookieParam, isBodyParam, isContainer, isCollectionFormatMulti, isPrimitiveType, isModel, isExplode, baseName, paramName, dataType, datatypeWithEnum, dataFormat, collectionFormat, description, unescapedDescription, baseType, containerType, containerTypeMapped, defaultValue, 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, items, mostInnerItems, additionalProperties, vars, requiredVars, vendorExtensions, hasValidation, getMaxProperties(), getMinProperties(), isNullable, isDeprecated, required, getMaximum(), getExclusiveMaximum(), getMinimum(), getExclusiveMinimum(), getMaxLength(), getMinLength(), getPattern(), getMaxItems(), getMinItems(), getUniqueItems(), contentType, multipleOf, isNull, isVoid, additionalPropertiesIsAnyType, hasVars, hasRequired, isShort, isUnboundedInteger, hasDiscriminatorWithNonEmptyMapping, composedSchemas, hasMultipleTypes, schema, content, requiredVarsMap, ref, uniqueItemsBoolean, schemaIsFromAdditionalProperties);
return Objects.hash(isFormParam, isQueryParam, isPathParam, isHeaderParam, isCookieParam,
isBodyParam, isContainer, isCollectionFormatMulti, isPrimitiveType, isModel, isExplode, baseName,
paramName, dataType, datatypeWithEnum, dataFormat, collectionFormat, description,
unescapedDescription, baseType, containerType, containerTypeMapped, defaultValue,
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,
items, mostInnerItems, additionalProperties, vars, requiredVars, vendorExtensions, hasValidation,
getMaxProperties(), getMinProperties(), isNullable, isDeprecated, required, getMaximum(),
getExclusiveMaximum(), getMinimum(), getExclusiveMinimum(), getMaxLength(), getMinLength(),
getPattern(), getMaxItems(), getMinItems(), getUniqueItems(), contentType, multipleOf, isNull,isVoid,
additionalPropertiesIsAnyType, hasVars, hasRequired, isShort, isUnboundedInteger,
hasDiscriminatorWithNonEmptyMapping, composedSchemas, hasMultipleTypes, schema, content,
requiredVarsMap, ref, uniqueItemsBoolean, schemaIsFromAdditionalProperties,
nameInPascalCase, nameInCamelCase, nameInLowerCase, nameInSnakeCase);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,10 @@ public class CodegenProperty implements Cloneable, IJsonSchemaValidationProperti
public boolean hasValidation; // true if pattern, maximum, etc are set (only used in the mustache template)
public boolean isInherited;
public String discriminatorValue;

public String nameInLowerCase; // property name in lower case
public String nameInCamelCase; // property name in camel case
public String nameInCamelCase; // property name in camel case (e.g. modifiedDate)
public String nameInPascalCase; // property name in pascal case (e.g. ModifiedDate)
public String nameInSnakeCase; // property name in upper snake case
// enum name based on the property name, usually use as a prefix (e.g. VAR_NAME) for enum name (e.g. VAR_NAME_VALUE1)
public String enumName;
Expand Down Expand Up @@ -697,6 +699,14 @@ public void setNameInCamelCase(String nameInCamelCase) {
this.nameInCamelCase = nameInCamelCase;
}

public String getNameInPascalCase() {
return nameInPascalCase;
}

public void setNameInPascalCase(String nameInPascalCase) {
this.nameInPascalCase = nameInPascalCase;
}

public String getNameInSnakeCase() {
return nameInSnakeCase;
}
Expand Down Expand Up @@ -1213,6 +1223,7 @@ public String toString() {
sb.append(", isInherited=").append(isInherited);
sb.append(", discriminatorValue='").append(discriminatorValue).append('\'');
sb.append(", nameInCamelCase='").append(nameInCamelCase).append('\'');
sb.append(", nameInPascalCase='").append(nameInPascalCase).append('\'');
sb.append(", nameInSnakeCase='").append(nameInSnakeCase).append('\'');
sb.append(", enumName='").append(enumName).append('\'');
sb.append(", maxItems=").append(maxItems);
Expand Down Expand Up @@ -1352,6 +1363,7 @@ public boolean equals(Object o) {
Objects.equals(vendorExtensions, that.vendorExtensions) &&
Objects.equals(discriminatorValue, that.discriminatorValue) &&
Objects.equals(nameInCamelCase, that.nameInCamelCase) &&
Objects.equals(nameInPascalCase, that.nameInPascalCase) &&
Objects.equals(nameInSnakeCase, that.nameInSnakeCase) &&
Objects.equals(enumName, that.enumName) &&
Objects.equals(maxItems, that.maxItems) &&
Expand All @@ -1376,7 +1388,7 @@ public int hashCode() {
isArray, isMap, 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, nameInCamelCase,
vendorExtensions, hasValidation, isInherited, discriminatorValue, nameInPascalCase, nameInCamelCase,
nameInSnakeCase, enumName, maxItems, minItems, isXmlAttribute, xmlPrefix, xmlName,
xmlNamespace, isXmlWrapped, isNull, isVoid, additionalPropertiesIsAnyType, hasVars, hasRequired,
hasDiscriminatorWithNonEmptyMapping, composedSchemas, hasMultipleTypes, requiredVarsMap,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
import org.openapitools.codegen.serializer.SerializerUtils;
import org.openapitools.codegen.templating.MustacheEngineAdapter;
import org.openapitools.codegen.templating.mustache.*;
import org.openapitools.codegen.utils.CamelizeOption;
import org.openapitools.codegen.utils.ModelUtils;
import org.openapitools.codegen.utils.OneOfImplementorAdditionalData;
import org.slf4j.Logger;
Expand All @@ -82,6 +83,7 @@
import java.util.stream.Stream;

import static org.openapitools.codegen.CodegenConstants.UNSUPPORTED_V310_SPEC_MSG;
import static org.openapitools.codegen.utils.CamelizeOption.LOWERCASE_FIRST_LETTER;
import static org.openapitools.codegen.utils.OnceLogger.once;
import static org.openapitools.codegen.utils.StringUtils.*;

Expand Down Expand Up @@ -4012,8 +4014,9 @@ public CodegenProperty fromProperty(String name, Schema p, boolean required, boo
} else {
property.openApiType = p.getType();
}
property.nameInCamelCase = camelize(property.name);
property.nameInSnakeCase = CaseFormat.UPPER_CAMEL.to(CaseFormat.UPPER_UNDERSCORE, property.nameInCamelCase);
property.nameInPascalCase = camelize(property.name);
property.nameInCamelCase = camelize(property.name, LOWERCASE_FIRST_LETTER);
property.nameInSnakeCase = CaseFormat.UPPER_CAMEL.to(CaseFormat.UPPER_UNDERSCORE, property.nameInPascalCase);
property.description = escapeText(p.getDescription());
property.unescapedDescription = p.getDescription();
property.title = p.getTitle();
Expand Down Expand Up @@ -5458,6 +5461,11 @@ public CodegenParameter fromParameter(Parameter parameter, Set<String> imports)
codegenParameter.isCollectionFormatMulti = true;
}
codegenParameter.paramName = toParamName(parameter.getName());
codegenParameter.nameInCamelCase = camelize(codegenParameter.paramName, LOWERCASE_FIRST_LETTER);
codegenParameter.nameInPascalCase = camelize(codegenParameter.paramName);
codegenParameter.nameInSnakeCase = CaseFormat.UPPER_CAMEL.to(CaseFormat.UPPER_UNDERSCORE, codegenParameter.nameInPascalCase);
codegenParameter.nameInLowerCase = codegenParameter.paramName.toLowerCase(Locale.ROOT);

// import
if (codegenProperty.complexType != null) {
imports.add(codegenProperty.complexType);
Expand Down Expand Up @@ -7185,6 +7193,11 @@ public CodegenParameter fromFormProperty(String name, Schema propertySchema, Set
codegenParameter.dataType = codegenProperty.dataType;
codegenParameter.baseName = codegenProperty.baseName;
codegenParameter.paramName = toParamName(codegenParameter.baseName);
codegenParameter.nameInCamelCase = camelize(codegenParameter.paramName, LOWERCASE_FIRST_LETTER);
codegenParameter.nameInPascalCase = camelize(codegenParameter.paramName);
codegenParameter.nameInSnakeCase = CaseFormat.UPPER_CAMEL.to(CaseFormat.UPPER_UNDERSCORE, codegenParameter.nameInPascalCase);
codegenParameter.nameInLowerCase = codegenParameter.paramName.toLowerCase(Locale.ROOT);

codegenParameter.dataFormat = codegenProperty.dataFormat;
// non-array/map
updateCodegenPropertyEnum(codegenProperty);
Expand Down Expand Up @@ -7569,6 +7582,11 @@ protected void updateRequestBodyForArray(CodegenParameter codegenParameter, Sche
codegenParameter.baseName = bodyParameterName;
}
codegenParameter.paramName = toArrayModelParamName(codegenParameter.baseName);
codegenParameter.nameInCamelCase = camelize(codegenParameter.paramName, LOWERCASE_FIRST_LETTER);
codegenParameter.nameInPascalCase = camelize(codegenParameter.paramName);
codegenParameter.nameInSnakeCase = CaseFormat.UPPER_CAMEL.to(CaseFormat.UPPER_UNDERSCORE, codegenParameter.nameInPascalCase);
codegenParameter.nameInLowerCase = codegenParameter.paramName.toLowerCase(Locale.ROOT);

codegenParameter.items = codegenProperty.items;
codegenParameter.mostInnerItems = codegenProperty.mostInnerItems;
codegenParameter.dataType = getTypeDeclaration(arraySchema);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -469,9 +469,9 @@ else if ("Integer".equals(datatype) || "Long".equals(datatype) ||
public CodegenProperty fromProperty(String name, Schema p, boolean required) {
CodegenProperty property = super.fromProperty(name, p, required);
if (property != null) {
String nameInCamelCase = property.nameInCamelCase;
nameInCamelCase = sanitizeName(nameInCamelCase);
property.nameInCamelCase = nameInCamelCase;
String nameInPascalCase = property.nameInPascalCase;
nameInPascalCase = sanitizeName(nameInPascalCase);
property.nameInPascalCase = nameInPascalCase;
}
return property;
}
Expand Down Expand Up @@ -650,10 +650,6 @@ public CodegenOperation fromOperation(String path, String httpMethod, Operation
}
}
}
for (CodegenProperty header : rsp.headers) {
header.nameInCamelCase = toModelName(header.baseName);
header.nameInLowerCase = header.baseName.toLowerCase(Locale.ROOT);
}
}

return op;
Expand Down Expand Up @@ -845,10 +841,6 @@ private void postProcessOperationWithModels(CodegenOperation op, List<ModelMap>
}
}
}
for (CodegenProperty header : rsp.headers) {
header.nameInCamelCase = toModelName(header.baseName);
header.nameInLowerCase = header.baseName.toLowerCase(Locale.ROOT);
}
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ public String toParamName(String name) {
@Override
public CodegenProperty fromProperty(String name, Schema p, boolean required) {
CodegenProperty property = super.fromProperty(name, p, required);
String nameInCamelCase = property.nameInCamelCase;
String nameInCamelCase = property.nameInPascalCase;
if (nameInCamelCase.length() > 1) {
nameInCamelCase = sanitizeName(Character.toLowerCase(nameInCamelCase.charAt(0)) + nameInCamelCase.substring(1));
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -648,7 +648,7 @@ public String toEnumVarName(String value, String datatype) {

@Override
public String toEnumName(CodegenProperty property) {
return property.nameInCamelCase;
return property.nameInPascalCase;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2062,7 +2062,7 @@ private PythonType getType(CodegenProperty cp) {
values.add((String) enumVar.get("value"));
}
}
return String.format(Locale.ROOT, "%sEnum", cp.nameInCamelCase);
return String.format(Locale.ROOT, "%sEnum", cp.nameInPascalCase);
} else*/

if (result == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1351,7 +1351,7 @@ private String getPydanticType(CodegenProperty cp,
values.add((String) enumVar.get("value"));
}
}
return String.format(Locale.ROOT, "%sEnum", cp.nameInCamelCase);
return String.format(Locale.ROOT, "%sEnum", cp.nameInPascalCase);
} else*/
if (cp.isArray) {
String constraints = "";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ public CodegenProperty fromProperty(String name, Schema p, boolean required) {
CodegenProperty prop = super.fromProperty(name, p, required);
String cc = camelize(prop.name, LOWERCASE_FIRST_LETTER);
if (isReservedWord(cc)) {
cc = escapeReservedWord(cc);
cc = escapeReservedWord(cc); // e.g. byte => byte_
}
prop.nameInCamelCase = cc;
return prop;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -550,20 +550,6 @@ public CodegenOperation fromOperation(String path, String httpMethod, Operation
}
}
}

for (CodegenProperty header : rsp.headers) {
header.nameInCamelCase = toModelName(header.baseName);
header.nameInLowerCase = header.baseName.toLowerCase(Locale.ROOT);
}
}

for (CodegenParameter header : op.headerParams) {
header.nameInLowerCase = header.baseName.toLowerCase(Locale.ROOT);
}

for (CodegenProperty header : op.responseHeaders) {
header.nameInCamelCase = toModelName(header.baseName);
header.nameInLowerCase = header.baseName.toLowerCase(Locale.ROOT);
}

return op;
Expand Down Expand Up @@ -636,15 +622,6 @@ private void postProcessOperationWithModels(CodegenOperation op) {
param.vendorExtensions.put("x-consumes-json", true);
}
}

for (CodegenParameter header : op.headerParams) {
header.nameInLowerCase = header.baseName.toLowerCase(Locale.ROOT);
}

for (CodegenProperty header : op.responseHeaders) {
header.nameInCamelCase = toModelName(header.baseName);
header.nameInLowerCase = header.baseName.toLowerCase(Locale.ROOT);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -773,7 +773,7 @@ public CodegenOperation fromOperation(String path, String httpMethod, Operation
if (uuidType.equals(header.dataType)) {
additionalProperties.put("apiUsesUuid", true);
}
header.nameInCamelCase = toModelName(header.baseName);
header.nameInPascalCase = toModelName(header.baseName);
header.nameInLowerCase = header.baseName.toLowerCase(Locale.ROOT);
}
}
Expand All @@ -786,7 +786,7 @@ public CodegenOperation fromOperation(String path, String httpMethod, Operation
if (uuidType.equals(header.dataType)) {
additionalProperties.put("apiUsesUuid", true);
}
header.nameInCamelCase = toModelName(header.baseName);
header.nameInPascalCase = toModelName(header.baseName);
header.nameInLowerCase = header.baseName.toLowerCase(Locale.ROOT);
}

Expand Down Expand Up @@ -881,7 +881,7 @@ private void postProcessOperationWithModels(CodegenOperation op, List<ModelMap>
if (uuidType.equals(header.dataType)) {
additionalProperties.put("apiUsesUuid", true);
}
header.nameInCamelCase = toModelName(header.baseName);
header.nameInPascalCase = toModelName(header.baseName);
header.nameInLowerCase = header.baseName.toLowerCase(Locale.ROOT);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1239,7 +1239,7 @@ public ExtendedCodegenProperty(CodegenProperty cp) {
this.isInherited = cp.isInherited;
this.discriminatorValue = cp.discriminatorValue;
this.nameInLowerCase = cp.nameInLowerCase;
this.nameInCamelCase = cp.nameInCamelCase;
this.nameInPascalCase = cp.nameInPascalCase;
this.nameInSnakeCase = cp.nameInSnakeCase;
this.enumName = cp.enumName;
this.maxItems = cp.maxItems;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public class {{classname}} {{#parent}}extends {{{.}}} {{/parent}}{{#vendorExtens
}
{{#isArray}}

public {{classname}} add{{nameInCamelCase}}Item({{{items.datatypeWithEnum}}} {{name}}Item) {
public {{classname}} add{{nameInPascalCase}}Item({{{items.datatypeWithEnum}}} {{name}}Item) {
{{#vendorExtensions.x-is-jackson-optional-nullable}}
if (this.{{name}} == null || !this.{{name}}.isPresent()) {
this.{{name}} = JsonNullable.<{{{datatypeWithEnum}}}>of({{{defaultValue}}}{{^defaultValue}}new {{#uniqueItems}}LinkedHashSet{{/uniqueItems}}{{^uniqueItems}}ArrayList{{/uniqueItems}}<>(){{/defaultValue}});
Expand All @@ -156,7 +156,7 @@ public class {{classname}} {{#parent}}extends {{{.}}} {{/parent}}{{#vendorExtens
{{/isArray}}
{{#isMap}}

public {{classname}} put{{nameInCamelCase}}Item(String key, {{{items.datatypeWithEnum}}} {{name}}Item) {
public {{classname}} put{{nameInPascalCase}}Item(String key, {{{items.datatypeWithEnum}}} {{name}}Item) {
{{#vendorExtensions.x-is-jackson-optional-nullable}}
if (this.{{name}} == null || !this.{{name}}.isPresent()) {
this.{{name}} = JsonNullable.<{{{datatypeWithEnum}}}>of({{{defaultValue}}}{{^defaultValue}}new HashMap<>(){{/defaultValue}});
Expand Down
Loading

0 comments on commit 829b953

Please sign in to comment.