Adds hasVars (#8928)

* Adds getHasVars and setHasVars to IJsonSchemaValidationProperties

* Updates CodegenModel

* Updates CodegenProperty

* Updates codegenparameter

* Updates codegenresponse

* Adds testHasVarsInModel

* Adds testHasVarsInProperty

* Updates testHasVarsInProperty

* Adds testHasVarsInParameter

* Adds testHasVarsInResponse

* Samples update
This commit is contained in:
Justin Black
2021-03-09 12:14:02 -08:00
committed by GitHub
parent 86e418d57b
commit 786c71f339
10 changed files with 206 additions and 7 deletions

View File

@@ -723,6 +723,16 @@ public class CodegenModel implements IJsonSchemaValidationProperties {
this.additionalPropertiesIsAnyType = additionalPropertiesIsAnyType;
}
@Override
public boolean getHasVars() {
return this.hasVars;
}
@Override
public void setHasVars(boolean hasVars) {
this.hasVars = hasVars;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;

View File

@@ -40,6 +40,7 @@ public class CodegenParameter implements IJsonSchemaValidationProperties {
public boolean isFile;
public boolean isEnum;
private boolean additionalPropertiesIsAnyType;
private boolean hasVars;
public List<String> _enum;
public Map<String, Object> allowableValues;
public CodegenProperty items;
@@ -151,6 +152,7 @@ public class CodegenParameter implements IJsonSchemaValidationProperties {
output.additionalProperties = this.additionalProperties;
output.isNull = this.isNull;
output.setAdditionalPropertiesIsAnyType(this.getAdditionalPropertiesIsAnyType());
output.setHasVars(this.hasVars);
if (this._enum != null) {
output._enum = new ArrayList<String>(this._enum);
@@ -205,7 +207,7 @@ public class CodegenParameter implements IJsonSchemaValidationProperties {
@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, defaultValue, enumName, style, isDeepObject, 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, required, getMaximum(), getExclusiveMaximum(), getMinimum(), getExclusiveMinimum(), getMaxLength(), getMinLength(), getPattern(), getMaxItems(), getMinItems(), getUniqueItems(), contentType, multipleOf, isNull, getAdditionalPropertiesIsAnyType());
return Objects.hash(isFormParam, isQueryParam, isPathParam, isHeaderParam, isCookieParam, isBodyParam, isContainer, isCollectionFormatMulti, isPrimitiveType, isModel, isExplode, baseName, paramName, dataType, datatypeWithEnum, dataFormat, collectionFormat, description, unescapedDescription, baseType, defaultValue, enumName, style, isDeepObject, 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, required, getMaximum(), getExclusiveMaximum(), getMinimum(), getExclusiveMinimum(), getMaxLength(), getMinLength(), getPattern(), getMaxItems(), getMinItems(), getUniqueItems(), contentType, multipleOf, isNull, additionalPropertiesIsAnyType, hasVars);
}
@Override
@@ -251,6 +253,7 @@ public class CodegenParameter implements IJsonSchemaValidationProperties {
required == that.required &&
isNull == that.isNull &&
getAdditionalPropertiesIsAnyType() == that.getAdditionalPropertiesIsAnyType() &&
getHasVars() == that.getHasVars() &&
getExclusiveMaximum() == that.getExclusiveMaximum() &&
getExclusiveMinimum() == that.getExclusiveMinimum() &&
getUniqueItems() == that.getUniqueItems() &&
@@ -367,7 +370,8 @@ public class CodegenParameter implements IJsonSchemaValidationProperties {
sb.append(", contentType=").append(contentType);
sb.append(", multipleOf=").append(multipleOf);
sb.append(", isNull=").append(isNull);
sb.append(", getAdditionalPropertiesIsAnyType=").append(getAdditionalPropertiesIsAnyType());
sb.append(", getAdditionalPropertiesIsAnyType=").append(additionalPropertiesIsAnyType);
sb.append(", getHasVars=").append(hasVars);
sb.append('}');
return sb.toString();
}
@@ -605,5 +609,15 @@ public class CodegenParameter implements IJsonSchemaValidationProperties {
public void setAdditionalPropertiesIsAnyType(boolean additionalPropertiesIsAnyType) {
this.additionalPropertiesIsAnyType = additionalPropertiesIsAnyType;
}
@Override
public boolean getHasVars() {
return this.hasVars;
}
@Override
public void setHasVars(boolean hasVars) {
this.hasVars = hasVars;
}
}

View File

@@ -186,6 +186,7 @@ public class CodegenProperty implements Cloneable, IJsonSchemaValidationProperti
public String xmlNamespace;
public boolean isXmlWrapped = false;
private boolean additionalPropertiesIsAnyType;
private boolean hasVars;
public String getBaseName() {
return baseName;
@@ -704,6 +705,16 @@ public class CodegenProperty implements Cloneable, IJsonSchemaValidationProperti
this.additionalPropertiesIsAnyType = additionalPropertiesIsAnyType;
}
@Override
public boolean getHasVars() {
return this.hasVars;
}
@Override
public void setHasVars(boolean hasVars) {
this.hasVars = hasVars;
}
@Override
public String toString() {
final StringBuilder sb = new StringBuilder("CodegenProperty{");
@@ -794,6 +805,7 @@ public class CodegenProperty implements Cloneable, IJsonSchemaValidationProperti
sb.append(", isXmlWrapped=").append(isXmlWrapped);
sb.append(", isNull=").append(isNull);
sb.append(", getAdditionalPropertiesIsAnyType=").append(getAdditionalPropertiesIsAnyType());
sb.append(", getHasVars=").append(getHasVars());
sb.append('}');
return sb.toString();
}
@@ -844,6 +856,7 @@ public class CodegenProperty implements Cloneable, IJsonSchemaValidationProperti
isXmlWrapped == that.isXmlWrapped &&
isNull == that.isNull &&
getAdditionalPropertiesIsAnyType() == that.getAdditionalPropertiesIsAnyType() &&
getHasVars() == that.getHasVars() &&
Objects.equals(openApiType, that.openApiType) &&
Objects.equals(baseName, that.baseName) &&
Objects.equals(complexType, that.complexType) &&
@@ -905,6 +918,6 @@ public class CodegenProperty implements Cloneable, IJsonSchemaValidationProperti
items, mostInnerItems, additionalProperties, vars, requiredVars,
vendorExtensions, hasValidation, isInherited, discriminatorValue, nameInCamelCase,
nameInSnakeCase, enumName, maxItems, minItems, isXmlAttribute, xmlPrefix, xmlName,
xmlNamespace, isXmlWrapped, isNull, getAdditionalPropertiesIsAnyType());
xmlNamespace, isXmlWrapped, isNull, additionalPropertiesIsAnyType, hasVars);
}
}

View File

@@ -80,6 +80,7 @@ public class CodegenResponse implements IJsonSchemaValidationProperties {
public List<CodegenProperty> requiredVars = new ArrayList<CodegenProperty>();
private boolean hasValidation;
private boolean additionalPropertiesIsAnyType;
private boolean hasVars;
@Override
public int hashCode() {
@@ -90,7 +91,7 @@ public class CodegenResponse implements IJsonSchemaValidationProperties {
vars, requiredVars, isNull, hasValidation,
getMaxProperties(), getMinProperties(), uniqueItems, getMaxItems(), getMinItems(), getMaxLength(),
getMinLength(), exclusiveMinimum, exclusiveMaximum, getMinimum(), getMaximum(), getPattern(),
is1xx, is2xx, is3xx, is4xx, is5xx, getAdditionalPropertiesIsAnyType());
is1xx, is2xx, is3xx, is4xx, is5xx, additionalPropertiesIsAnyType, hasVars);
}
@Override
@@ -133,6 +134,7 @@ public class CodegenResponse implements IJsonSchemaValidationProperties {
is4xx == that.is4xx &&
is5xx == that.is5xx &&
getAdditionalPropertiesIsAnyType() == that.getAdditionalPropertiesIsAnyType() &&
getHasVars() == that.getHasVars() &&
Objects.equals(vars, that.vars) &&
Objects.equals(requiredVars, that.requiredVars) &&
Objects.equals(headers, that.headers) &&
@@ -431,7 +433,8 @@ public class CodegenResponse implements IJsonSchemaValidationProperties {
sb.append(", requiredVars='").append(requiredVars).append('\'');
sb.append(", isNull='").append(isNull);
sb.append(", hasValidation='").append(hasValidation);
sb.append(", getAdditionalPropertiesIsAnyType=").append(getAdditionalPropertiesIsAnyType());
sb.append(", getAdditionalPropertiesIsAnyType=").append(additionalPropertiesIsAnyType);
sb.append(", getHasVars=").append(hasVars);
sb.append('}');
return sb.toString();
}
@@ -478,4 +481,14 @@ public class CodegenResponse implements IJsonSchemaValidationProperties {
public void setAdditionalPropertiesIsAnyType(boolean additionalPropertiesIsAnyType) {
this.additionalPropertiesIsAnyType = additionalPropertiesIsAnyType;
}
@Override
public boolean getHasVars() {
return this.hasVars;
}
@Override
public void setHasVars(boolean hasVars) {
this.hasVars = hasVars;
}
}

View File

@@ -6141,6 +6141,9 @@ public class DefaultCodegen implements CodegenConfig {
if (objSchema.getRequired() != null) {
requiredVars.addAll(objSchema.getRequired());
}
if (objSchema.getProperties() != null && objSchema.getProperties().size() > 0) {
property.setHasVars(true);
}
addVars(property, property.getVars(), objSchema.getProperties(), requiredVars);
List<CodegenProperty> requireCpVars = property.getVars()
.stream()

View File

@@ -102,4 +102,8 @@ public interface IJsonSchemaValidationProperties {
boolean getAdditionalPropertiesIsAnyType();
void setAdditionalPropertiesIsAnyType(boolean additionalPropertiesIsAnyType);
boolean getHasVars();
void setHasVars(boolean hasVars);
}

View File

@@ -2955,4 +2955,124 @@ public class DefaultCodegenTest {
assertEquals(cp.isModel, true);
assertEquals(cp.complexType, "objectData");
}
@Test
public void testHasVarsInModel() {
final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/issue_7613.yaml");
final DefaultCodegen codegen = new DefaultCodegen();
codegen.setOpenAPI(openAPI);
codegen.setDisallowAdditionalPropertiesIfNotPresent(false);
Schema sc;
CodegenModel cm;
List<String> modelNames;
modelNames = Arrays.asList(
"ArrayWithValidationsInItems",
"ObjectWithValidationsInAdditionalProperties",
"AdditionalPropertiesUnset",
"AdditionalPropertiesTrue",
"AdditionalPropertiesFalse",
"AdditionalPropertiesSchema"
);
for (String modelName : modelNames) {
sc = openAPI.getComponents().getSchemas().get(modelName);
cm = codegen.fromModel(modelName, sc);
assertEquals(cm.getHasVars(), false);
}
modelNames = Arrays.asList(
"ObjectModelWithRefAddPropsInProps",
"ObjectModelWithAddPropsInProps",
"ObjectWithOptionalAndRequiredProps",
"ObjectPropContainsProps"
);
for (String modelName : modelNames) {
sc = openAPI.getComponents().getSchemas().get(modelName);
cm = codegen.fromModel(modelName, sc);
assertEquals(cm.getHasVars(), true);
}
}
@Test
public void testHasVarsInProperty() {
final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/issue_7613.yaml");
final DefaultCodegen codegen = new DefaultCodegen();
codegen.setOpenAPI(openAPI);
codegen.setDisallowAdditionalPropertiesIfNotPresent(false);
Schema sc;
CodegenModel cm;
List<String> modelNames;
modelNames = Arrays.asList(
"ObjectWithValidationsInArrayPropItems",
"ObjectModelWithRefAddPropsInProps",
"ObjectModelWithAddPropsInProps",
"ObjectWithOptionalAndRequiredProps"
);
for (String modelName : modelNames) {
sc = openAPI.getComponents().getSchemas().get(modelName);
cm = codegen.fromModel(modelName, sc);
assertEquals(cm.vars.get(0).getHasVars(), false);
}
String modelName;
modelName = "ArrayWithObjectWithPropsInItems";
sc = openAPI.getComponents().getSchemas().get(modelName);
cm = codegen.fromModel(modelName, sc);
assertEquals(cm.getItems().getHasVars(), true);
modelName = "ObjectWithObjectWithPropsInAdditionalProperties";
sc = openAPI.getComponents().getSchemas().get(modelName);
cm = codegen.fromModel(modelName, sc);
assertEquals(cm.getAdditionalProperties().getHasVars(), true);
}
@Test
public void testHasVarsInParameter() {
final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/issue_7613.yaml");
final DefaultCodegen codegen = new DefaultCodegen();
codegen.setOpenAPI(openAPI);
codegen.setDisallowAdditionalPropertiesIfNotPresent(false);
String path;
Operation operation;
CodegenOperation co;
path = "/array_with_validations_in_items/{items}";
operation = openAPI.getPaths().get(path).getPost();
co = codegen.fromOperation(path, "POST", operation, null);
assertEquals(co.pathParams.get(0).getHasVars(), false);
assertEquals(co.bodyParam.getHasVars(), false);
path = "/object_with_optional_and_required_props/{objectData}";
operation = openAPI.getPaths().get(path).getPost();
co = codegen.fromOperation(path, "POST", operation, null);
assertEquals(co.pathParams.get(0).getHasVars(), true);
assertEquals(co.bodyParam.getHasVars(), true);
}
@Test
public void testHasVarsInResponse() {
final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/issue_7613.yaml");
final DefaultCodegen codegen = new DefaultCodegen();
codegen.setOpenAPI(openAPI);
codegen.setDisallowAdditionalPropertiesIfNotPresent(false);
String path;
Operation operation;
CodegenOperation co;
path = "/additional_properties/";
operation = openAPI.getPaths().get(path).getPost();
co = codegen.fromOperation(path, "POST", operation, null);
assertEquals(co.responses.get(0).getHasVars(), false);
path = "/object_with_optional_and_required_props/{objectData}";
operation = openAPI.getPaths().get(path).getPost();
co = codegen.fromOperation(path, "POST", operation, null);
// does not have vars because the inline schema was extracted into a component ref
assertEquals(co.responses.get(0).getHasVars(), false);
}
}

View File

@@ -354,4 +354,26 @@ components:
required:
- a
- b
ArrayWithObjectWithPropsInItems:
type: array
items:
type: object
properties:
a:
type: string
minLength: 1
b:
type: string
minLength: 1
ObjectWithObjectWithPropsInAdditionalProperties:
type: object
additionalProperties:
type: object
properties:
a:
type: string
minLength: 1
b:
type: string
minLength: 1
securitySchemes: {}

View File

@@ -541,10 +541,10 @@ public interface PathHandlerInterface {
* <p><b>Response headers</b>: [CodegenProperty{openApiType='integer', baseName='X-Rate-Limit', complexType='null', getter='getxRateLimit', setter='setxRateLimit', description='calls per hour allowed by the user', dataType='Integer', datatypeWithEnum='Integer', dataFormat='int32', name='xRateLimit', min='null', max='null', defaultValue='null', defaultValueWithParam=' = data.X-Rate-Limit;', baseType='Integer', containerType='null', title='null', unescapedDescription='calls per hour allowed by the user', maxLength=null, minLength=null, pattern='null', example='null', jsonSchema='{
"type" : "integer",
"format" : "int32"
}', minimum='null', maximum='null', exclusiveMinimum=false, exclusiveMaximum=false, required=false, deprecated=false, hasMoreNonReadOnly=false, isPrimitiveType=true, isModel=false, isContainer=false, isString=false, isNumeric=true, isInteger=true, isLong=false, isNumber=false, isFloat=false, isDouble=false, isDecimal=false, isByteArray=false, isBinary=false, isFile=false, isBoolean=false, isDate=false, isDateTime=false, isUuid=false, isUri=false, isEmail=false, isFreeFormObject=false, isArray=false, isMap=false, isEnum=false, isReadOnly=false, isWriteOnly=false, isNullable=false, isSelfReference=false, isCircularReference=false, isDiscriminator=false, _enum=null, allowableValues=null, items=null, additionalProperties=null, vars=[], requiredVars=[], mostInnerItems=null, vendorExtensions={}, hasValidation=false, isInherited=false, discriminatorValue='null', nameInCamelCase='XRateLimit', nameInSnakeCase='X_RATE_LIMIT', enumName='null', maxItems=null, minItems=null, maxProperties=null, minProperties=null, uniqueItems=false, multipleOf=null, isXmlAttribute=false, xmlPrefix='null', xmlName='null', xmlNamespace='null', isXmlWrapped=false, isNull=false, getAdditionalPropertiesIsAnyType=false}, CodegenProperty{openApiType='string', baseName='X-Expires-After', complexType='Date', getter='getxExpiresAfter', setter='setxExpiresAfter', description='date in UTC when toekn expires', dataType='Date', datatypeWithEnum='Date', dataFormat='date-time', name='xExpiresAfter', min='null', max='null', defaultValue='null', defaultValueWithParam=' = data.X-Expires-After;', baseType='Date', containerType='null', title='null', unescapedDescription='date in UTC when toekn expires', maxLength=null, minLength=null, pattern='null', example='null', jsonSchema='{
}', minimum='null', maximum='null', exclusiveMinimum=false, exclusiveMaximum=false, required=false, deprecated=false, hasMoreNonReadOnly=false, isPrimitiveType=true, isModel=false, isContainer=false, isString=false, isNumeric=true, isInteger=true, isLong=false, isNumber=false, isFloat=false, isDouble=false, isDecimal=false, isByteArray=false, isBinary=false, isFile=false, isBoolean=false, isDate=false, isDateTime=false, isUuid=false, isUri=false, isEmail=false, isFreeFormObject=false, isArray=false, isMap=false, isEnum=false, isReadOnly=false, isWriteOnly=false, isNullable=false, isSelfReference=false, isCircularReference=false, isDiscriminator=false, _enum=null, allowableValues=null, items=null, additionalProperties=null, vars=[], requiredVars=[], mostInnerItems=null, vendorExtensions={}, hasValidation=false, isInherited=false, discriminatorValue='null', nameInCamelCase='XRateLimit', nameInSnakeCase='X_RATE_LIMIT', enumName='null', maxItems=null, minItems=null, maxProperties=null, minProperties=null, uniqueItems=false, multipleOf=null, isXmlAttribute=false, xmlPrefix='null', xmlName='null', xmlNamespace='null', isXmlWrapped=false, isNull=false, getAdditionalPropertiesIsAnyType=false, getHasVars=false}, CodegenProperty{openApiType='string', baseName='X-Expires-After', complexType='Date', getter='getxExpiresAfter', setter='setxExpiresAfter', description='date in UTC when toekn expires', dataType='Date', datatypeWithEnum='Date', dataFormat='date-time', name='xExpiresAfter', min='null', max='null', defaultValue='null', defaultValueWithParam=' = data.X-Expires-After;', baseType='Date', containerType='null', title='null', unescapedDescription='date in UTC when toekn expires', maxLength=null, minLength=null, pattern='null', example='null', jsonSchema='{
"type" : "string",
"format" : "date-time"
}', minimum='null', maximum='null', exclusiveMinimum=false, exclusiveMaximum=false, required=false, deprecated=false, hasMoreNonReadOnly=false, isPrimitiveType=false, isModel=false, isContainer=false, isString=false, isNumeric=false, isInteger=false, isLong=false, isNumber=false, isFloat=false, isDouble=false, isDecimal=false, isByteArray=false, isBinary=false, isFile=false, isBoolean=false, isDate=false, isDateTime=true, isUuid=false, isUri=false, isEmail=false, isFreeFormObject=false, isArray=false, isMap=false, isEnum=false, isReadOnly=false, isWriteOnly=false, isNullable=false, isSelfReference=false, isCircularReference=false, isDiscriminator=false, _enum=null, allowableValues=null, items=null, additionalProperties=null, vars=[], requiredVars=[], mostInnerItems=null, vendorExtensions={}, hasValidation=false, isInherited=false, discriminatorValue='null', nameInCamelCase='XExpiresAfter', nameInSnakeCase='X_EXPIRES_AFTER', enumName='null', maxItems=null, minItems=null, maxProperties=null, minProperties=null, uniqueItems=false, multipleOf=null, isXmlAttribute=false, xmlPrefix='null', xmlName='null', xmlNamespace='null', isXmlWrapped=false, isNull=false, getAdditionalPropertiesIsAnyType=false}]</p>
}', minimum='null', maximum='null', exclusiveMinimum=false, exclusiveMaximum=false, required=false, deprecated=false, hasMoreNonReadOnly=false, isPrimitiveType=false, isModel=false, isContainer=false, isString=false, isNumeric=false, isInteger=false, isLong=false, isNumber=false, isFloat=false, isDouble=false, isDecimal=false, isByteArray=false, isBinary=false, isFile=false, isBoolean=false, isDate=false, isDateTime=true, isUuid=false, isUri=false, isEmail=false, isFreeFormObject=false, isArray=false, isMap=false, isEnum=false, isReadOnly=false, isWriteOnly=false, isNullable=false, isSelfReference=false, isCircularReference=false, isDiscriminator=false, _enum=null, allowableValues=null, items=null, additionalProperties=null, vars=[], requiredVars=[], mostInnerItems=null, vendorExtensions={}, hasValidation=false, isInherited=false, discriminatorValue='null', nameInCamelCase='XExpiresAfter', nameInSnakeCase='X_EXPIRES_AFTER', enumName='null', maxItems=null, minItems=null, maxProperties=null, minProperties=null, uniqueItems=false, multipleOf=null, isXmlAttribute=false, xmlPrefix='null', xmlName='null', xmlNamespace='null', isXmlWrapped=false, isNull=false, getAdditionalPropertiesIsAnyType=false, getHasVars=false}]</p>
*
* <p><b>Produces</b>: [{mediaType=application/xml}, {mediaType=application/json}]</p>
* <p><b>Returns</b>: {@link String}</p>