feat(password): add isPassword codegen property (#13982)

Expose `isPassword` codegen property. This property can be used in the
mustache templates to handle specific generation use case. _e.g._:
* hiding the string value of password fields in generated toString()
methods
* use more specific types like `char[]` instead of `String` in
[Java](https://stackoverflow.com/a/8881376)

Closes https://github.com/OpenAPITools/openapi-generator/issues/9124
This commit is contained in:
Romain Bioteau 2023-03-13 16:23:48 +01:00 committed by GitHub
parent df58ee3829
commit 59be28cc8a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 38 additions and 4 deletions

View File

@ -35,7 +35,7 @@ public class CodegenParameter implements IJsonSchemaValidationProperties {
public String example; // example value (x-example) public String example; // example value (x-example)
public String jsonSchema; public String jsonSchema;
public boolean isString, isNumeric, isInteger, isLong, isNumber, isFloat, isDouble, isDecimal, isByteArray, isBinary, public boolean isString, isNumeric, isInteger, isLong, isNumber, isFloat, isDouble, isDecimal, isByteArray, isBinary,
isBoolean, isDate, isDateTime, isUuid, isUri, isEmail, isFreeFormObject, isAnyType, isShort, isUnboundedInteger; isBoolean, isDate, isDateTime, isUuid, isUri, isEmail, isPassword, isFreeFormObject, isAnyType, isShort, isUnboundedInteger;
public boolean isArray, isMap; public boolean isArray, isMap;
public boolean isFile; public boolean isFile;
public boolean isEnum; public boolean isEnum;
@ -229,6 +229,7 @@ public class CodegenParameter implements IJsonSchemaValidationProperties {
output.isUuid = this.isUuid; output.isUuid = this.isUuid;
output.isUri = this.isUri; output.isUri = this.isUri;
output.isEmail = this.isEmail; output.isEmail = this.isEmail;
output.isPassword = this.isPassword;
output.isFreeFormObject = this.isFreeFormObject; output.isFreeFormObject = this.isFreeFormObject;
output.isAnyType = this.isAnyType; output.isAnyType = this.isAnyType;
output.isArray = this.isArray; output.isArray = this.isArray;
@ -244,7 +245,7 @@ public class CodegenParameter implements IJsonSchemaValidationProperties {
@Override @Override
public int hashCode() { 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, defaultValue, enumDefaultValue, enumName, style, isDeepObject, isAllowEmptyValue, example, jsonSchema, isString, isNumeric, isInteger, isLong, isNumber, isFloat, isDouble, isDecimal, isByteArray, isBinary, isBoolean, isDate, isDateTime, isUuid, isUri, isEmail, isFreeFormObject, isAnyType, isArray, isMap, isFile, isEnum, _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, 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, defaultValue, enumDefaultValue, enumName, style, isDeepObject, isAllowEmptyValue, example, jsonSchema, isString, isNumeric, isInteger, isLong, isNumber, isFloat, isDouble, isDecimal, isByteArray, isBinary, isBoolean, isDate, isDateTime, isUuid, isUri, isEmail, isPassword, isFreeFormObject, isAnyType, isArray, isMap, isFile, isEnum, _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, additionalPropertiesIsAnyType, hasVars, hasRequired, isShort, isUnboundedInteger, hasDiscriminatorWithNonEmptyMapping, composedSchemas, hasMultipleTypes, schema, content, requiredVarsMap, ref, uniqueItemsBoolean, schemaIsFromAdditionalProperties);
} }
@Override @Override
@ -281,6 +282,7 @@ public class CodegenParameter implements IJsonSchemaValidationProperties {
isUuid == that.isUuid && isUuid == that.isUuid &&
isUri == that.isUri && isUri == that.isUri &&
isEmail == that.isEmail && isEmail == that.isEmail &&
isPassword == that.isPassword &&
isFreeFormObject == that.isFreeFormObject && isFreeFormObject == that.isFreeFormObject &&
isAnyType == that.isAnyType && isAnyType == that.isAnyType &&
isArray == that.isArray && isArray == that.isArray &&
@ -394,6 +396,7 @@ public class CodegenParameter implements IJsonSchemaValidationProperties {
sb.append(", isUuid=").append(isUuid); sb.append(", isUuid=").append(isUuid);
sb.append(", isUri=").append(isUri); sb.append(", isUri=").append(isUri);
sb.append(", isEmail=").append(isEmail); sb.append(", isEmail=").append(isEmail);
sb.append(", isPassword=").append(isPassword);
sb.append(", isFreeFormObject=").append(isFreeFormObject); sb.append(", isFreeFormObject=").append(isFreeFormObject);
sb.append(", isAnyType=").append(isAnyType); sb.append(", isAnyType=").append(isAnyType);
sb.append(", isArray=").append(isArray); sb.append(", isArray=").append(isArray);

View File

@ -134,6 +134,7 @@ public class CodegenProperty implements Cloneable, IJsonSchemaValidationProperti
public boolean isUuid; public boolean isUuid;
public boolean isUri; public boolean isUri;
public boolean isEmail; public boolean isEmail;
public boolean isPassword;
public boolean isNull; public boolean isNull;
/** /**
* The type is a free-form object, i.e. it is a map of string to values with no declared properties. * The type is a free-form object, i.e. it is a map of string to values with no declared properties.
@ -1051,6 +1052,7 @@ public class CodegenProperty implements Cloneable, IJsonSchemaValidationProperti
sb.append(", isUuid=").append(isUuid); sb.append(", isUuid=").append(isUuid);
sb.append(", isUri=").append(isUri); sb.append(", isUri=").append(isUri);
sb.append(", isEmail=").append(isEmail); sb.append(", isEmail=").append(isEmail);
sb.append(", isPassword=").append(isPassword);
sb.append(", isFreeFormObject=").append(isFreeFormObject); sb.append(", isFreeFormObject=").append(isFreeFormObject);
sb.append(", isArray=").append(isArray); sb.append(", isArray=").append(isArray);
sb.append(", isMap=").append(isMap); sb.append(", isMap=").append(isMap);
@ -1142,6 +1144,7 @@ public class CodegenProperty implements Cloneable, IJsonSchemaValidationProperti
isUuid == that.isUuid && isUuid == that.isUuid &&
isUri == that.isUri && isUri == that.isUri &&
isEmail == that.isEmail && isEmail == that.isEmail &&
isPassword == that.isPassword &&
isFreeFormObject == that.isFreeFormObject && isFreeFormObject == that.isFreeFormObject &&
isArray == that.isArray && isArray == that.isArray &&
isMap == that.isMap && isMap == that.isMap &&
@ -1231,7 +1234,7 @@ public class CodegenProperty implements Cloneable, IJsonSchemaValidationProperti
exclusiveMinimum, exclusiveMaximum, required, deprecated, exclusiveMinimum, exclusiveMaximum, required, deprecated,
hasMoreNonReadOnly, isPrimitiveType, isModel, isContainer, isString, isNumeric, hasMoreNonReadOnly, isPrimitiveType, isModel, isContainer, isString, isNumeric,
isInteger, isLong, isNumber, isFloat, isDouble, isDecimal, isByteArray, isBinary, isFile, isInteger, isLong, isNumber, isFloat, isDouble, isDecimal, isByteArray, isBinary, isFile,
isBoolean, isDate, isDateTime, isUuid, isUri, isEmail, isFreeFormObject, isBoolean, isDate, isDateTime, isUuid, isUri, isEmail, isPassword, isFreeFormObject,
isArray, isMap, isEnum, isInnerEnum, isEnumRef, isAnyType, isReadOnly, isWriteOnly, isNullable, isShort, isArray, isMap, isEnum, isInnerEnum, isEnumRef, isAnyType, isReadOnly, isWriteOnly, isNullable, isShort,
isUnboundedInteger, isSelfReference, isCircularReference, isDiscriminator, isNew, _enum, isUnboundedInteger, isSelfReference, isCircularReference, isDiscriminator, isNew, _enum,
allowableValues, items, mostInnerItems, additionalProperties, vars, requiredVars, allowableValues, items, mostInnerItems, additionalProperties, vars, requiredVars,

View File

@ -50,6 +50,7 @@ public class CodegenResponse implements IJsonSchemaValidationProperties {
public boolean isDateTime; public boolean isDateTime;
public boolean isUuid; public boolean isUuid;
public boolean isEmail; public boolean isEmail;
public boolean isPassword;
public boolean isModel; public boolean isModel;
public boolean isFreeFormObject; public boolean isFreeFormObject;
public boolean isAnyType; public boolean isAnyType;
@ -98,7 +99,7 @@ public class CodegenResponse implements IJsonSchemaValidationProperties {
public int hashCode() { public int hashCode() {
return Objects.hash(headers, code, message, examples, dataType, baseType, containerType, hasHeaders, return Objects.hash(headers, code, message, examples, dataType, baseType, containerType, hasHeaders,
isString, isNumeric, isInteger, isLong, isNumber, isFloat, isDouble, isDecimal, isByteArray, isBoolean, isDate, isString, isNumeric, isInteger, isLong, isNumber, isFloat, isDouble, isDecimal, isByteArray, isBoolean, isDate,
isDateTime, isUuid, isEmail, isModel, isFreeFormObject, isAnyType, isDefault, simpleType, primitiveType, isDateTime, isUuid, isEmail, isPassword, isModel, isFreeFormObject, isAnyType, isDefault, simpleType, primitiveType,
isMap, isArray, isBinary, isFile, schema, jsonSchema, vendorExtensions, items, additionalProperties, isMap, isArray, isBinary, isFile, schema, jsonSchema, vendorExtensions, items, additionalProperties,
vars, requiredVars, isNull, hasValidation, isShort, isUnboundedInteger, vars, requiredVars, isNull, hasValidation, isShort, isUnboundedInteger,
getMaxProperties(), getMinProperties(), uniqueItems, getMaxItems(), getMinItems(), getMaxLength(), getMaxProperties(), getMinProperties(), uniqueItems, getMaxItems(), getMinItems(), getMaxLength(),
@ -130,6 +131,7 @@ public class CodegenResponse implements IJsonSchemaValidationProperties {
isDateTime == that.isDateTime && isDateTime == that.isDateTime &&
isUuid == that.isUuid && isUuid == that.isUuid &&
isEmail == that.isEmail && isEmail == that.isEmail &&
isPassword == that.isPassword &&
isModel == that.isModel && isModel == that.isModel &&
isFreeFormObject == that.isFreeFormObject && isFreeFormObject == that.isFreeFormObject &&
isAnyType == that.isAnyType && isAnyType == that.isAnyType &&
@ -568,6 +570,7 @@ public class CodegenResponse implements IJsonSchemaValidationProperties {
sb.append(", isDateTime=").append(isDateTime); sb.append(", isDateTime=").append(isDateTime);
sb.append(", isUuid=").append(isUuid); sb.append(", isUuid=").append(isUuid);
sb.append(", isEmail=").append(isEmail); sb.append(", isEmail=").append(isEmail);
sb.append(", isPassword=").append(isPassword);
sb.append(", isModel=").append(isModel); sb.append(", isModel=").append(isModel);
sb.append(", isFreeFormObject=").append(isFreeFormObject); sb.append(", isFreeFormObject=").append(isFreeFormObject);
sb.append(", isAnyType=").append(isAnyType); sb.append(", isAnyType=").append(isAnyType);

View File

@ -3680,6 +3680,8 @@ public class DefaultCodegen implements CodegenConfig {
property.isUri = true; property.isUri = true;
} else if (ModelUtils.isEmailSchema(p)) { } else if (ModelUtils.isEmailSchema(p)) {
property.isEmail = true; property.isEmail = true;
} else if (ModelUtils.isPasswordSchema(p)) {
property.isPassword = true;
} else if (ModelUtils.isDateSchema(p)) { // date format } else if (ModelUtils.isDateSchema(p)) { // date format
property.setIsString(false); // for backward compatibility with 2.x property.setIsString(false); // for backward compatibility with 2.x
property.isDate = true; property.isDate = true;
@ -4738,6 +4740,8 @@ public class DefaultCodegen implements CodegenConfig {
} else if (ModelUtils.isStringSchema(responseSchema)) { } else if (ModelUtils.isStringSchema(responseSchema)) {
if (ModelUtils.isEmailSchema(responseSchema)) { if (ModelUtils.isEmailSchema(responseSchema)) {
r.isEmail = true; r.isEmail = true;
} else if (ModelUtils.isPasswordSchema(responseSchema)) {
r.isPassword = true;
} else if (ModelUtils.isUUIDSchema(responseSchema)) { } else if (ModelUtils.isUUIDSchema(responseSchema)) {
r.isUuid = true; r.isUuid = true;
} else if (ModelUtils.isByteArraySchema(responseSchema)) { } else if (ModelUtils.isByteArraySchema(responseSchema)) {
@ -6286,6 +6290,8 @@ public class DefaultCodegen implements CodegenConfig {
} }
if (Boolean.TRUE.equals(property.isEmail) && Boolean.TRUE.equals(property.isString)) { if (Boolean.TRUE.equals(property.isEmail) && Boolean.TRUE.equals(property.isString)) {
parameter.isEmail = true; parameter.isEmail = true;
} else if (Boolean.TRUE.equals(property.isPassword) && Boolean.TRUE.equals(property.isPassword)) {
parameter.isPassword = true;
} else if (Boolean.TRUE.equals(property.isUuid) && Boolean.TRUE.equals(property.isString)) { } else if (Boolean.TRUE.equals(property.isUuid) && Boolean.TRUE.equals(property.isString)) {
parameter.isUuid = true; parameter.isUuid = true;
} else if (Boolean.TRUE.equals(property.isByteArray)) { } else if (Boolean.TRUE.equals(property.isByteArray)) {
@ -6801,6 +6807,8 @@ public class DefaultCodegen implements CodegenConfig {
} else if (ModelUtils.isStringSchema(ps)) { } else if (ModelUtils.isStringSchema(ps)) {
if (ModelUtils.isEmailSchema(ps)) { if (ModelUtils.isEmailSchema(ps)) {
codegenParameter.isEmail = true; codegenParameter.isEmail = true;
} else if (ModelUtils.isPasswordSchema(ps)) {
codegenParameter.isPassword = true;
} else if (ModelUtils.isUUIDSchema(ps)) { } else if (ModelUtils.isUUIDSchema(ps)) {
codegenParameter.isUuid = true; codegenParameter.isUuid = true;
} else if (ModelUtils.isByteArraySchema(ps)) { } else if (ModelUtils.isByteArraySchema(ps)) {

View File

@ -247,6 +247,8 @@ public interface IJsonSchemaValidationProperties {
; ;
} else if (ModelUtils.isEmailSchema(p)) { } else if (ModelUtils.isEmailSchema(p)) {
; ;
} else if (ModelUtils.isPasswordSchema(p)) {
;
} else if (ModelUtils.isDateSchema(p)) { } else if (ModelUtils.isDateSchema(p)) {
; ;
} else if (ModelUtils.isDateTimeSchema(p)) { } else if (ModelUtils.isDateTimeSchema(p)) {

View File

@ -2473,6 +2473,10 @@ public class DefaultCodegenTest {
assertTrue(names.contains("password")); assertTrue(names.contains("password"));
assertTrue(names.contains("passwordConfirmation")); assertTrue(names.contains("passwordConfirmation"));
assertTrue(names.contains("oldPassword")); assertTrue(names.contains("oldPassword"));
Optional<CodegenParameter> passwordParameter = operation.formParams.stream().filter(p -> "password".equals(p.paramName)).findFirst();
assertTrue(passwordParameter.isPresent());
assertTrue(passwordParameter.get().isPassword);
} }
@Test @Test

View File

@ -1125,6 +1125,17 @@ public class JavaModelTest {
Assert.assertEquals(cp.maxLength, Integer.valueOf(10)); Assert.assertEquals(cp.maxLength, Integer.valueOf(10));
Assert.assertEquals(cp.pattern, "^[A-Z]+$"); Assert.assertEquals(cp.pattern, "^[A-Z]+$");
} }
@Test(description = "convert string property with password format")
public void stringPropertyPasswordFormatTest() {
OpenAPI openAPI = TestUtils.createOpenAPI();
final Schema property = new StringSchema().format("password");
final DefaultCodegen codegen = new JavaClientCodegen();
codegen.setOpenAPI(openAPI);
final CodegenProperty cp = codegen.fromProperty("somePropertyWithPasswordFormat", property);
Assert.assertEquals(cp.isPassword, true);
}
@Test(description = "convert string property in an object") @Test(description = "convert string property in an object")
public void stringPropertyInObjectTest() { public void stringPropertyInObjectTest() {