Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds hasVars #8928

Merged
merged 11 commits into from
Mar 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -723,6 +723,16 @@ public void setAdditionalPropertiesIsAnyType(boolean additionalPropertiesIsAnyTy
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -151,6 +152,7 @@ public CodegenParameter copy() {
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);
Expand Down Expand Up @@ -205,7 +207,7 @@ 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, 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
Expand Down Expand Up @@ -251,6 +253,7 @@ public boolean equals(Object o) {
required == that.required &&
isNull == that.isNull &&
getAdditionalPropertiesIsAnyType() == that.getAdditionalPropertiesIsAnyType() &&
getHasVars() == that.getHasVars() &&
getExclusiveMaximum() == that.getExclusiveMaximum() &&
getExclusiveMinimum() == that.getExclusiveMinimum() &&
getUniqueItems() == that.getUniqueItems() &&
Expand Down Expand Up @@ -367,7 +370,8 @@ public String toString() {
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();
}
Expand Down Expand Up @@ -605,5 +609,15 @@ public boolean getAdditionalPropertiesIsAnyType() {
public void setAdditionalPropertiesIsAnyType(boolean additionalPropertiesIsAnyType) {
this.additionalPropertiesIsAnyType = additionalPropertiesIsAnyType;
}

@Override
public boolean getHasVars() {
return this.hasVars;
}

@Override
public void setHasVars(boolean hasVars) {
this.hasVars = hasVars;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -704,6 +705,16 @@ public void setAdditionalPropertiesIsAnyType(boolean additionalPropertiesIsAnyTy
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{");
Expand Down Expand Up @@ -794,6 +805,7 @@ public String toString() {
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();
}
Expand Down Expand Up @@ -844,6 +856,7 @@ public boolean equals(Object o) {
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) &&
Expand Down Expand Up @@ -905,6 +918,6 @@ public int hashCode() {
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -90,7 +91,7 @@ public int hashCode() {
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
Expand Down Expand Up @@ -133,6 +134,7 @@ public boolean equals(Object o) {
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) &&
Expand Down Expand Up @@ -431,7 +433,8 @@ public String toString() {
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();
}
Expand Down Expand Up @@ -478,4 +481,14 @@ public boolean getAdditionalPropertiesIsAnyType() {
public void setAdditionalPropertiesIsAnyType(boolean additionalPropertiesIsAnyType) {
this.additionalPropertiesIsAnyType = additionalPropertiesIsAnyType;
}

@Override
public boolean getHasVars() {
return this.hasVars;
}

@Override
public void setHasVars(boolean hasVars) {
this.hasVars = hasVars;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6141,6 +6141,9 @@ private void addVarsRequiredVarsAdditionalProps(Schema schema, IJsonSchemaValida
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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,8 @@ public interface IJsonSchemaValidationProperties {
boolean getAdditionalPropertiesIsAnyType();

void setAdditionalPropertiesIsAnyType(boolean additionalPropertiesIsAnyType);

boolean getHasVars();

void setHasVars(boolean hasVars);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2955,4 +2955,124 @@ public void testVarsAndRequiredVarsPresent() {
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);
}
}
22 changes: 22 additions & 0 deletions modules/openapi-generator/src/test/resources/3_0/issue_7613.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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: {}
Binary file not shown.
Loading