Adds hasValidation to all java core Schema classes (#8474)

* Adds hasValidation to IJsonSchemaValidationProperties

* Adds model validation examples for maxItems, minItems, minProperties, maxProperties, minLength, maxLength, multipleOf

* Adds schemas with pattern validation

* Adds minimum example schemas

* Adds maximum example schemas

* Adds ArrayWithUniqueItems

* Adds exclusiveMinimum schemas

* Adds exclusiveMaximum examples

* adds testModelGetHasValidation

* Adds testPropertyGetHasValidation

* Adds testQueryParametersGetHasValidation

* Uncomments out query parameters

* Adds testHeaderParametersGetHasValidation

* Adds testCookieParametersGetHasValidation

* Adds length assertions for properties and marameters

* Adds testBodyAndResponseGetHasValidation

* Improves validation setting

* Only sets exclusiveMinimum when minimum is set, only set exclusiveMaximum when maximum is set

* Adds fix for rust

* Fixes min and max setting for integers

* Regenerates python samples

* Updates code so python sample does not change
This commit is contained in:
Justin Black 2021-01-21 09:52:49 -08:00 committed by GitHub
parent 3d23b99242
commit 2331432cc0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 3072 additions and 150 deletions

View File

@ -79,7 +79,7 @@ public class CodegenModel implements IJsonSchemaValidationProperties {
public Set<String> allMandatory = new TreeSet<String>(); // with parent's required properties
public Set<String> imports = new TreeSet<String>();
public boolean hasVars, emptyVars, hasMoreModels, hasEnums, isEnum;
public boolean hasVars, emptyVars, hasMoreModels, hasEnums, isEnum, hasValidation;
/**
* Indicates the OAS schema specifies "nullable: true".
*/
@ -612,12 +612,11 @@ public class CodegenModel implements IJsonSchemaValidationProperties {
this.additionalProperties = additionalProperties;
}
// indicates if the model component has validation on the root level schema
// this will be true when minItems or minProperties is set
public boolean hasValidation() {
boolean val = (maxItems != null || minItems != null || minProperties != null || maxProperties != null || minLength != null || maxLength != null || multipleOf != null || pattern != null || minimum != null || maximum != null || Boolean.TRUE.equals(uniqueItems) || Boolean.TRUE.equals(exclusiveMaximum) || Boolean.TRUE.equals(exclusiveMinimum));
return val;
}
@Override
public boolean getHasValidation() { return hasValidation; }
@Override
public void setHasValidation(boolean hasValidation) { this.hasValidation = hasValidation; }
public List<CodegenProperty> getReadOnlyVars() {
return readOnlyVars;
@ -742,6 +741,7 @@ public class CodegenModel implements IJsonSchemaValidationProperties {
isDeprecated == that.isDeprecated &&
hasOnlyReadOnly == that.hasOnlyReadOnly &&
isNull == that.isNull &&
hasValidation == that.hasValidation &&
getUniqueItems() == that.getUniqueItems() &&
getExclusiveMinimum() == that.getExclusiveMinimum() &&
getExclusiveMaximum() == that.getExclusiveMaximum() &&
@ -806,7 +806,7 @@ public class CodegenModel implements IJsonSchemaValidationProperties {
getDescription(), getClassVarName(), getModelJson(), getDataType(), getXmlPrefix(), getXmlNamespace(),
getXmlName(), getClassFilename(), getUnescapedDescription(), getDiscriminator(), getDefaultValue(),
getArrayModelType(), isAlias, isString, isInteger, isLong, isNumber, isNumeric, isFloat, isDouble,
isDate, isDateTime, isNull,
isDate, isDateTime, isNull, hasValidation,
getVars(), getAllVars(), getRequiredVars(), getOptionalVars(), getReadOnlyVars(), getReadWriteVars(),
getParentVars(), getAllowableValues(), getMandatory(), getAllMandatory(), getImports(), hasVars,
isEmptyVars(), hasMoreModels, hasEnums, isEnum, isNullable, hasRequired, hasOptional, isArray,
@ -898,6 +898,7 @@ public class CodegenModel implements IJsonSchemaValidationProperties {
sb.append(", additionalProperties='").append(additionalProperties).append('\'');
sb.append(", isModel='").append(isModel).append('\'');
sb.append(", isNull='").append(isNull);
sb.append(", hasValidation='").append(hasValidation);
sb.append('}');
return sb.toString();
}

View File

@ -582,5 +582,11 @@ public class CodegenParameter implements IJsonSchemaValidationProperties {
public void setIsNull(boolean isNull) {
this.isNull = isNull;
}
@Override
public boolean getHasValidation() { return hasValidation; }
@Override
public void setHasValidation(boolean hasValidation) { this.hasValidation = hasValidation; }
}

View File

@ -687,6 +687,12 @@ public class CodegenProperty implements Cloneable, IJsonSchemaValidationProperti
this.isNull = isNull;
}
@Override
public boolean getHasValidation() { return hasValidation; }
@Override
public void setHasValidation(boolean hasValidation) { this.hasValidation = hasValidation; }
@Override
public String toString() {
final StringBuilder sb = new StringBuilder("CodegenProperty{");

View File

@ -78,6 +78,7 @@ public class CodegenResponse implements IJsonSchemaValidationProperties {
public CodegenProperty additionalProperties;
public List<CodegenProperty> vars = new ArrayList<CodegenProperty>(); // all properties (without parent's properties)
public List<CodegenProperty> requiredVars = new ArrayList<CodegenProperty>();
private boolean hasValidation;
@Override
public int hashCode() {
@ -85,7 +86,7 @@ public class CodegenResponse implements IJsonSchemaValidationProperties {
isString, isNumeric, isInteger, isLong, isNumber, isFloat, isDouble, isDecimal, isByteArray, isBoolean, isDate,
isDateTime, isUuid, isEmail, isModel, isFreeFormObject, isAnyType, isDefault, simpleType, primitiveType,
isMap, isArray, isBinary, isFile, schema, jsonSchema, vendorExtensions, items, additionalProperties,
vars, requiredVars, isNull,
vars, requiredVars, isNull, hasValidation,
getMaxProperties(), getMinProperties(), uniqueItems, getMaxItems(), getMinItems(), getMaxLength(),
getMinLength(), exclusiveMinimum, exclusiveMaximum, getMinimum(), getMaximum(), getPattern(),
is1xx, is2xx, is3xx, is4xx, is5xx);
@ -124,6 +125,7 @@ public class CodegenResponse implements IJsonSchemaValidationProperties {
items == that.items &&
additionalProperties == that.additionalProperties &&
isNull == that.isNull &&
hasValidation == that.hasValidation &&
is1xx == that.is1xx &&
is2xx == that.is2xx &&
is3xx == that.is3xx &&
@ -426,6 +428,7 @@ public class CodegenResponse implements IJsonSchemaValidationProperties {
sb.append(", vars='").append(vars).append('\'');
sb.append(", requiredVars='").append(requiredVars).append('\'');
sb.append(", isNull='").append(isNull);
sb.append(", hasValidation='").append(hasValidation);
sb.append('}');
return sb.toString();
}
@ -456,4 +459,10 @@ public class CodegenResponse implements IJsonSchemaValidationProperties {
public void setIsNull(boolean isNull) {
this.isNull = isNull;
}
@Override
public boolean getHasValidation() { return hasValidation; }
@Override
public void setHasValidation(boolean hasValidation) { this.hasValidation = hasValidation; }
}

View File

@ -2258,6 +2258,7 @@ public class DefaultCodegen implements CodegenConfig {
}
CodegenModel m = CodegenModelFactory.newInstance(CodegenModelType.MODEL);
ModelUtils.syncValidationProperties(schema, m);
if (reservedWords.contains(name)) {
m.name = escapeReservedWord(name);
@ -2305,7 +2306,6 @@ public class DefaultCodegen implements CodegenConfig {
m.setItems(arrayProperty.items);
m.arrayModelType = arrayProperty.complexType;
addParentContainer(m, name, schema);
ModelUtils.syncValidationProperties(schema, m);
} else if (ModelUtils.isNullType(schema)) {
m.isNull = true;
} else if (schema instanceof ComposedSchema) {
@ -2500,8 +2500,6 @@ public class DefaultCodegen implements CodegenConfig {
m.isMap = true;
} else if (ModelUtils.isIntegerSchema(schema)) { // integer type
// NOTE: Integral schemas as CodegenModel is a rare use case and may be removed at a later date.
// Sync of properties is done for consistency with other data types like CodegenParameter/CodegenProperty.
ModelUtils.syncValidationProperties(schema, m);
m.isNumeric = Boolean.TRUE;
if (ModelUtils.isLongSchema(schema)) { // int64/long format
@ -2511,23 +2509,15 @@ public class DefaultCodegen implements CodegenConfig {
}
} else if (ModelUtils.isDateTimeSchema(schema)) {
// NOTE: DateTime schemas as CodegenModel is a rare use case and may be removed at a later date.
// Sync of properties is done for consistency with other data types like CodegenParameter/CodegenProperty.
ModelUtils.syncValidationProperties(schema, m);
m.isDateTime = Boolean.TRUE;
} else if (ModelUtils.isDateSchema(schema)) {
// NOTE: Date schemas as CodegenModel is a rare use case and may be removed at a later date.
// Sync of properties is done for consistency with other data types like CodegenParameter/CodegenProperty.
ModelUtils.syncValidationProperties(schema, m);
m.isDate = Boolean.TRUE;
} else if (ModelUtils.isStringSchema(schema)) {
// NOTE: String schemas as CodegenModel is a rare use case and may be removed at a later date.
// Sync of properties is done for consistency with other data types like CodegenParameter/CodegenProperty.
ModelUtils.syncValidationProperties(schema, m);
m.isString = Boolean.TRUE;
} else if (ModelUtils.isNumberSchema(schema)) {
// NOTE: Number schemas as CodegenModel is a rare use case and may be removed at a later date.
// Sync of properties is done for consistency with other data types like CodegenParameter/CodegenProperty.
ModelUtils.syncValidationProperties(schema, m);
m.isNumeric = Boolean.TRUE;
if (ModelUtils.isFloatSchema(schema)) { // float
m.isFloat = Boolean.TRUE;
@ -2538,7 +2528,6 @@ public class DefaultCodegen implements CodegenConfig {
}
} else if (ModelUtils.isFreeFormObject(openAPI, schema)) {
addAdditionPropertiesToCodeGenModel(m, schema);
ModelUtils.syncValidationProperties(schema, m);
}
if (Boolean.TRUE.equals(schema.getNullable())) {
@ -3077,7 +3066,6 @@ public class DefaultCodegen implements CodegenConfig {
p = unaliasSchema(p, importMapping);
CodegenProperty property = CodegenModelFactory.newInstance(CodegenModelType.PROPERTY);
ModelUtils.syncValidationProperties(p, property);
property.name = toVarName(name);
@ -3144,27 +3132,6 @@ public class DefaultCodegen implements CodegenConfig {
property.isInteger = Boolean.TRUE;
}
if (p.getMinimum() != null) {
property.minimum = String.valueOf(p.getMinimum().longValue());
}
if (p.getMaximum() != null) {
property.maximum = String.valueOf(p.getMaximum().longValue());
}
if (p.getExclusiveMinimum() != null) {
property.exclusiveMinimum = p.getExclusiveMinimum();
}
if (p.getExclusiveMaximum() != null) {
property.exclusiveMaximum = p.getExclusiveMaximum();
}
if (p.getMultipleOf() != null) {
property.multipleOf = p.getMultipleOf();
}
// check if any validation rule defined
// exclusive* are noop without corresponding min/max
if (property.minimum != null || property.maximum != null || p.getMultipleOf() != null)
property.hasValidation = true;
} else if (ModelUtils.isBooleanSchema(p)) { // boolean type
property.isBoolean = true;
property.getter = toBooleanGetter(name);
@ -3177,27 +3144,6 @@ public class DefaultCodegen implements CodegenConfig {
property.isDateTime = true;
} else if (ModelUtils.isDecimalSchema(p)) { // type: string, format: number
property.isDecimal = true;
if (p.getMinimum() != null) {
property.minimum = String.valueOf(p.getMinimum());
}
if (p.getMaximum() != null) {
property.maximum = String.valueOf(p.getMaximum());
}
if (p.getExclusiveMinimum() != null) {
property.exclusiveMinimum = p.getExclusiveMinimum();
}
if (p.getExclusiveMaximum() != null) {
property.exclusiveMaximum = p.getExclusiveMaximum();
}
if (p.getMultipleOf() != null) {
property.multipleOf = p.getMultipleOf();
}
// check if any validation rule defined
// exclusive* are noop without corresponding min/max
if (property.minimum != null || property.maximum != null || p.getMultipleOf() != null) {
property.hasValidation = true;
}
} else if (ModelUtils.isStringSchema(p)) {
if (ModelUtils.isByteArraySchema(p)) {
property.isByteArray = true;
@ -3219,15 +3165,8 @@ public class DefaultCodegen implements CodegenConfig {
} else {
property.isString = true;
}
property.maxLength = p.getMaxLength();
property.minLength = p.getMinLength();
property.pattern = toRegularExpression(p.getPattern());
// check if any validation rule defined
if (property.pattern != null || property.minLength != null || property.maxLength != null)
property.hasValidation = true;
} else if (ModelUtils.isNumberSchema(p)) {
property.isNumeric = Boolean.TRUE;
if (ModelUtils.isFloatSchema(p)) { // float
@ -3238,28 +3177,6 @@ public class DefaultCodegen implements CodegenConfig {
property.isNumber = Boolean.TRUE;
}
if (p.getMinimum() != null) {
property.minimum = String.valueOf(p.getMinimum());
}
if (p.getMaximum() != null) {
property.maximum = String.valueOf(p.getMaximum());
}
if (p.getExclusiveMinimum() != null) {
property.exclusiveMinimum = p.getExclusiveMinimum();
}
if (p.getExclusiveMaximum() != null) {
property.exclusiveMaximum = p.getExclusiveMaximum();
}
if (p.getMultipleOf() != null) {
property.multipleOf = p.getMultipleOf();
}
// check if any validation rule defined
// exclusive* are noop without corresponding min/max
if (property.minimum != null || property.maximum != null || p.getMultipleOf() != null) {
property.hasValidation = true;
}
} else if (isFreeFormObject(p)) {
property.isFreeFormObject = true;
} else if (isAnyTypeSchema(p)) {
@ -3347,8 +3264,6 @@ public class DefaultCodegen implements CodegenConfig {
}
// handle inner property
property.maxItems = p.getMaxItems();
property.minItems = p.getMinItems();
String itemName = null;
if (p.getExtensions() != null && p.getExtensions().get("x-item-name") != null) {
itemName = p.getExtensions().get("x-item-name").toString();
@ -3365,6 +3280,7 @@ public class DefaultCodegen implements CodegenConfig {
property.isMap = true;
property.containerType = "map";
property.baseType = getSchemaType(p);
// TODO remove this hack in the future, code should use minProperties and maxProperties for object schemas
property.minItems = p.getMinProperties();
property.maxItems = p.getMaxProperties();
@ -3983,20 +3899,6 @@ public class DefaultCodegen implements CodegenConfig {
public CodegenResponse fromResponse(String responseCode, ApiResponse response) {
CodegenResponse r = CodegenModelFactory.newInstance(CodegenModelType.RESPONSE);
if (response.getContent() != null && response.getContent().size() > 0) {
// Ensure validation properties from a target schema are persisted on CodegenResponse.
// This ignores any edge case where different schemas have different validations because we don't
// have a way to indicate a preference for response schema and are effective 1:1.
Schema contentSchema = null;
for (MediaType mt : response.getContent().values()) {
if (contentSchema != null) break;
contentSchema = mt.getSchema();
}
if (contentSchema != null) {
ModelUtils.syncValidationProperties(contentSchema, r);
}
}
if ("default".equals(responseCode) || "defaultResponse".equals(responseCode)) {
r.code = "0";
r.isDefault = true;
@ -4030,9 +3932,12 @@ public class DefaultCodegen implements CodegenConfig {
responseSchema = ModelUtils.getSchemaFromResponse(response);
}
r.schema = responseSchema;
if (responseSchema != null && responseSchema.getPattern() != null) {
if (responseSchema != null) {
ModelUtils.syncValidationProperties(responseSchema, r);
if (responseSchema.getPattern() != null) {
r.setPattern(toRegularExpression(responseSchema.getPattern()));
}
}
r.message = escapeText(response.getDescription());
// TODO need to revise and test examples in responses
@ -4219,20 +4124,6 @@ public class DefaultCodegen implements CodegenConfig {
public CodegenParameter fromParameter(Parameter parameter, Set<String> imports) {
CodegenParameter codegenParameter = CodegenModelFactory.newInstance(CodegenModelType.PARAMETER);
if (parameter.getContent() != null && parameter.getContent().size() > 0) {
// Ensure validation properties from a target schema are persisted on CodegenParameter.
// This ignores any edge case where different schemas have different validations because we don't
// have a way to indicate a preference for parameter schema and are effective 1:1.
Schema contentSchema = null;
for (MediaType mt : parameter.getContent().values()) {
if (contentSchema != null) break;
contentSchema = mt.getSchema();
}
if (contentSchema != null) {
ModelUtils.syncValidationProperties(contentSchema, codegenParameter);
}
}
codegenParameter.baseName = parameter.getName();
codegenParameter.description = escapeText(parameter.getDescription());
codegenParameter.unescapedDescription = parameter.getDescription();
@ -4271,6 +4162,7 @@ public class DefaultCodegen implements CodegenConfig {
LOGGER.warn("warning! Schema not found for parameter \"" + parameter.getName() + "\", using String");
parameterSchema = new StringSchema().description("//TODO automatically added by openapi-generator due to missing type definition.");
}
ModelUtils.syncValidationProperties(parameterSchema, codegenParameter);
if (Boolean.TRUE.equals(parameterSchema.getNullable())) { // use nullable defined in the spec
codegenParameter.isNullable = true;
@ -6029,9 +5921,10 @@ public class DefaultCodegen implements CodegenConfig {
if (StringUtils.isNotBlank(schema.get$ref())) {
name = ModelUtils.getSimpleRef(schema.get$ref());
}
Schema validationSchema = unaliasSchema(schema, importMapping);
schema = ModelUtils.getReferencedSchema(this.openAPI, schema);
ModelUtils.syncValidationProperties(schema, codegenParameter);
ModelUtils.syncValidationProperties(validationSchema, codegenParameter);
if (ModelUtils.isMapSchema(schema)) {
// Schema with additionalproperties: true (including composed schemas with additionalproperties: true)
@ -6159,12 +6052,6 @@ public class DefaultCodegen implements CodegenConfig {
codegenParameter.dataType = codegenProperty.dataType;
codegenParameter.description = codegenProperty.description;
codegenParameter.paramName = toParamName(codegenParameter.baseName);
codegenParameter.minimum = codegenProperty.minimum;
codegenParameter.maximum = codegenProperty.maximum;
codegenParameter.exclusiveMinimum = codegenProperty.exclusiveMinimum;
codegenParameter.exclusiveMaximum = codegenProperty.exclusiveMaximum;
codegenParameter.minLength = codegenProperty.minLength;
codegenParameter.maxLength = codegenProperty.maxLength;
codegenParameter.pattern = codegenProperty.pattern;
codegenParameter.isNullable = codegenProperty.isNullable;

View File

@ -94,4 +94,8 @@ public interface IJsonSchemaValidationProperties {
boolean getIsNull();
void setIsNull(boolean isNull);
boolean getHasValidation();
void setHasValidation(boolean hasValidation);
}

View File

@ -1489,31 +1489,90 @@ public class ModelUtils {
public static void syncValidationProperties(Schema schema, IJsonSchemaValidationProperties target) {
if (schema != null && target != null) {
target.setPattern(schema.getPattern());
BigDecimal minimum = schema.getMinimum();
BigDecimal maximum = schema.getMaximum();
Boolean exclusiveMinimum = schema.getExclusiveMinimum();
Boolean exclusiveMaximum = schema.getExclusiveMaximum();
Integer minLength = schema.getMinLength();
Integer maxLength = schema.getMaxLength();
if (isNullType(schema) || schema.get$ref() != null || isBooleanSchema(schema)) {
return;
}
boolean isAnyType = (schema.getClass().equals(Schema.class) && schema.get$ref() == null && schema.getType() == null &&
(schema.getProperties() == null || schema.getProperties().isEmpty()) &&
schema.getAdditionalProperties() == null && schema.getNot() == null &&
schema.getEnum() == null);
if (isAnyType) {
return;
}
Integer minItems = schema.getMinItems();
Integer maxItems = schema.getMaxItems();
Boolean uniqueItems = schema.getUniqueItems();
Integer minProperties = schema.getMinProperties();
Integer maxProperties = schema.getMaxProperties();
Integer minLength = schema.getMinLength();
Integer maxLength = schema.getMaxLength();
String pattern = schema.getPattern();
BigDecimal multipleOf = schema.getMultipleOf();
BigDecimal minimum = schema.getMinimum();
BigDecimal maximum = schema.getMaximum();
Boolean exclusiveMinimum = schema.getExclusiveMinimum();
Boolean exclusiveMaximum = schema.getExclusiveMaximum();
if (minimum != null) target.setMinimum(String.valueOf(minimum));
if (maximum != null) target.setMaximum(String.valueOf(maximum));
if (exclusiveMinimum != null) target.setExclusiveMinimum(exclusiveMinimum);
if (exclusiveMaximum != null) target.setExclusiveMaximum(exclusiveMaximum);
if (minLength != null) target.setMinLength(minLength);
if (maxLength != null) target.setMaxLength(maxLength);
if (isArraySchema(schema)) {
setArrayValidations(minItems, maxItems, uniqueItems, target);
} else if (isMapSchema(schema) || isObjectSchema(schema)) {
setObjectValidations(minProperties, maxProperties, target);
} else if (isStringSchema(schema)) {
setStringValidations(minLength, maxLength, pattern, target);
if (isDecimalSchema(schema)) {
setNumericValidations(schema, multipleOf, minimum, maximum, exclusiveMinimum, exclusiveMaximum, target);
}
} else if (isNumberSchema(schema) || isIntegerSchema(schema)) {
setNumericValidations(schema, multipleOf, minimum, maximum, exclusiveMinimum, exclusiveMaximum, target);
} else if (isComposedSchema(schema)) {
// this could be composed out of anything so set all validations here
setArrayValidations(minItems, maxItems, uniqueItems, target);
setObjectValidations(minProperties, maxProperties, target);
setStringValidations(minLength, maxLength, pattern, target);
setNumericValidations(schema, multipleOf, minimum, maximum, exclusiveMinimum, exclusiveMaximum, target);
}
if (maxItems != null || minItems != null || minProperties != null || maxProperties != null || minLength != null || maxLength != null || multipleOf != null || pattern != null || minimum != null || maximum != null || exclusiveMinimum != null || exclusiveMaximum != null || uniqueItems != null) {
target.setHasValidation(true);
}
}
}
private static void setArrayValidations(Integer minItems, Integer maxItems, Boolean uniqueItems, IJsonSchemaValidationProperties target) {
if (minItems != null) target.setMinItems(minItems);
if (maxItems != null) target.setMaxItems(maxItems);
if (uniqueItems != null) target.setUniqueItems(uniqueItems);
}
private static void setObjectValidations(Integer minProperties, Integer maxProperties, IJsonSchemaValidationProperties target) {
if (minProperties != null) target.setMinProperties(minProperties);
if (maxProperties != null) target.setMaxProperties(maxProperties);
}
private static void setStringValidations(Integer minLength, Integer maxLength, String pattern, IJsonSchemaValidationProperties target) {
if (minLength != null) target.setMinLength(minLength);
if (maxLength != null) target.setMaxLength(maxLength);
if (pattern != null) target.setPattern(pattern);
}
private static void setNumericValidations(Schema schema, BigDecimal multipleOf, BigDecimal minimum, BigDecimal maximum, Boolean exclusiveMinimum, Boolean exclusiveMaximum, IJsonSchemaValidationProperties target) {
if (multipleOf != null) target.setMultipleOf(multipleOf);
if (minimum != null) {
if (isIntegerSchema(schema)) {
target.setMinimum(String.valueOf(minimum.longValue()));
} else {
target.setMinimum(String.valueOf(minimum));
}
if (exclusiveMinimum != null) target.setExclusiveMinimum(exclusiveMinimum);
}
if (maximum != null) {
if (isIntegerSchema(schema)) {
target.setMaximum(String.valueOf(maximum.longValue()));
} else {
target.setMaximum(String.valueOf(maximum));
}
if (exclusiveMaximum != null) target.setExclusiveMaximum(exclusiveMaximum);
}
}
private static ObjectMapper getRightMapper(String data) {

View File

@ -2651,6 +2651,227 @@ public class DefaultCodegenTest {
assertEquals(co.responses.get(0).isNull, true);
}
@Test
public void testModelGetHasValidation() {
final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/issue_7651.yaml");
final DefaultCodegen codegen = new DefaultCodegen();
codegen.setOpenAPI(openAPI);
Schema sc;
CodegenModel cm;
List<String> modelNames = Arrays.asList(
"ArrayWithMaxItems",
"ArrayWithMinItems",
"ArrayWithUniqueItems",
"ObjectWithMinProperties",
"ObjectWithMaxProperties",
"StringWithMinLength",
"DateWithMinLength",
"DateTimeWithMinLength",
"ByteWithMinLength",
"BinaryWithMinLength",
"StringWithMaxLength",
"DateWithMaxLength",
"DateTimeWithMaxLength",
"ByteWithMaxLength",
"BinaryWithMaxLength",
"IntegerWithMultipleOf",
"Integer32WithMultipleOf",
"Integer64WithMultipleOf",
"NumberWithMultipleOf",
"NumberFloatWithMultipleOf",
"NumberDoubleWithMultipleOf",
"StringWithPattern",
"DateWithPattern",
"DateTimeWithPattern",
"ByteWithPattern",
"BinaryWithPattern",
"IntegerWithMinimum",
"Integer32WithMinimum",
"Integer64WithMinimum",
"NumberWithMinimum",
"NumberFloatWithMinimum",
"NumberDoubleWithMinimum",
"IntegerWithMaximum",
"Integer32WithMaximum",
"Integer64WithMaximum",
"NumberWithMaximum",
"NumberFloatWithMaximum",
"NumberDoubleWithMaximum",
"IntegerWithExclusiveMaximum",
"Integer32WithExclusiveMaximum",
"Integer64WithExclusiveMaximum",
"NumberWithExclusiveMaximum",
"NumberFloatWithExclusiveMaximum",
"NumberDoubleWithExclusiveMaximum",
"IntegerWithExclusiveMinimum",
"Integer32WithExclusiveMinimum",
"Integer64WithExclusiveMinimum",
"NumberWithExclusiveMinimum",
"NumberFloatWithExclusiveMinimum",
"NumberDoubleWithExclusiveMinimum"
);
for (String modelName : modelNames) {
sc = openAPI.getComponents().getSchemas().get(modelName);
cm = codegen.fromModel(modelName, sc);
assertEquals(cm.getHasValidation(), true);
}
}
@Test
public void testPropertyGetHasValidation() {
final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/issue_7651.yaml");
final DefaultCodegen codegen = new DefaultCodegen();
codegen.setOpenAPI(openAPI);
String modelName = "ObjectWithPropertiesThatHaveValidations";
Schema sc = openAPI.getComponents().getSchemas().get(modelName);;
CodegenModel cm = codegen.fromModel(modelName, sc);
List<CodegenProperty> props = cm.getVars();
assertEquals(props.size(), 50);
for (CodegenProperty prop : props) {
assertEquals(prop.getHasValidation(), true);
}
}
@Test
public void testQueryParametersGetHasValidation() {
final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/issue_7651.yaml");
final DefaultCodegen codegen = new DefaultCodegen();
codegen.setOpenAPI(openAPI);
String path = "/queryParametersWithValidation";
Operation operation = openAPI.getPaths().get(path).getPost();
CodegenOperation co = codegen.fromOperation(path, "POST", operation, null);
List<CodegenParameter> params = co.queryParams;
assertEquals(params.size(), 50);
for (CodegenParameter param : params) {
assertEquals(param.getHasValidation(), true);
}
}
@Test
public void testHeaderParametersGetHasValidation() {
final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/issue_7651.yaml");
final DefaultCodegen codegen = new DefaultCodegen();
codegen.setOpenAPI(openAPI);
String path = "/headerParametersWithValidation";
Operation operation = openAPI.getPaths().get(path).getPost();
CodegenOperation co = codegen.fromOperation(path, "POST", operation, null);
List<CodegenParameter> params = co.headerParams;
assertEquals(params.size(), 50);
for (CodegenParameter param : params) {
assertEquals(param.getHasValidation(), true);
}
}
@Test
public void testCookieParametersGetHasValidation() {
final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/issue_7651.yaml");
final DefaultCodegen codegen = new DefaultCodegen();
codegen.setOpenAPI(openAPI);
String path = "/cookieParametersWithValidation";
Operation operation = openAPI.getPaths().get(path).getPost();
CodegenOperation co = codegen.fromOperation(path, "POST", operation, null);
List<CodegenParameter> params = co.cookieParams;
assertEquals(params.size(), 50);
for (CodegenParameter param : params) {
assertEquals(param.getHasValidation(), true);
}
}
@Test
public void testPathParametersGetHasValidation() {
final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/issue_7651.yaml");
final DefaultCodegen codegen = new DefaultCodegen();
codegen.setOpenAPI(openAPI);
String path = "/pathParametersWithValidation";
Operation operation = openAPI.getPaths().get(path).getPost();
CodegenOperation co = codegen.fromOperation(path, "POST", operation, null);
List<CodegenParameter> params = co.pathParams;
assertEquals(params.size(), 50);
for (CodegenParameter param : params) {
assertEquals(param.getHasValidation(), true);
}
}
@Test
public void testBodyAndResponseGetHasValidation() {
final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/issue_7651.yaml");
final DefaultCodegen codegen = new DefaultCodegen();
codegen.setOpenAPI(openAPI);
List<String> modelNames = Arrays.asList(
"ArrayWithMaxItems",
"ArrayWithMinItems",
"ArrayWithUniqueItems",
"ObjectWithMinProperties",
"ObjectWithMaxProperties",
"StringWithMinLength",
"DateWithMinLength",
"DateTimeWithMinLength",
"ByteWithMinLength",
"BinaryWithMinLength",
"StringWithMaxLength",
"DateWithMaxLength",
"DateTimeWithMaxLength",
"ByteWithMaxLength",
"BinaryWithMaxLength",
"StringWithPattern",
"DateWithPattern",
"DateTimeWithPattern",
"ByteWithPattern",
"BinaryWithPattern",
"IntegerWithMultipleOf",
"Integer32WithMultipleOf",
"Integer64WithMultipleOf",
"NumberWithMultipleOf",
"NumberFloatWithMultipleOf",
"NumberDoubleWithMultipleOf",
"IntegerWithMinimum",
"Integer32WithMinimum",
"Integer64WithMinimum",
"NumberWithMinimum",
"NumberFloatWithMinimum",
"NumberDoubleWithMinimum",
"IntegerWithMaximum",
"Integer32WithMaximum",
"Integer64WithMaximum",
"NumberWithMaximum",
"NumberFloatWithMaximum",
"NumberDoubleWithMaximum",
"IntegerWithExclusiveMaximum",
"Integer32WithExclusiveMaximum",
"Integer64WithExclusiveMaximum",
"NumberWithExclusiveMaximum",
"NumberFloatWithExclusiveMaximum",
"NumberDoubleWithExclusiveMaximum",
"IntegerWithExclusiveMinimum",
"Integer32WithExclusiveMinimum",
"Integer64WithExclusiveMinimum",
"NumberWithExclusiveMinimum",
"NumberFloatWithExclusiveMinimum",
"NumberDoubleWithExclusiveMinimum"
);
String path;
Operation operation;
CodegenOperation co;
for (String modelName : modelNames) {
path = "/"+modelName;
operation = openAPI.getPaths().get(path).getPost();
co = codegen.fromOperation(path, "POST", operation, null);
assertEquals(co.bodyParam.getHasValidation(), true);
assertEquals(co.responses.get(0).getHasValidation(), true);
}
}
@Test
public void testVarsAndRequiredVarsPresent() {
final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/issue_7613.yaml");