forked from loafle/openapi-generator-original
Adds vars and requiredVars to Schema classes (#7893)
* Adds vars to all schema classes, adds partial test * Adds tests and addVarsRequiredVarsAdditionaProps * Adds CodegenProperty test * Adds requiredVars * Adds vars and requiredVars cloning for CodegenProperty
This commit is contained in:
@@ -634,10 +634,12 @@ public class CodegenModel implements IJsonSchemaValidationProperties {
|
||||
this.readWriteVars = readWriteVars;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<CodegenProperty> getRequiredVars() {
|
||||
return requiredVars;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setRequiredVars(List<CodegenProperty> requiredVars) {
|
||||
this.requiredVars = requiredVars;
|
||||
}
|
||||
@@ -658,10 +660,12 @@ public class CodegenModel implements IJsonSchemaValidationProperties {
|
||||
this.unescapedDescription = unescapedDescription;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<CodegenProperty> getVars() {
|
||||
return vars;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setVars(List<CodegenProperty> vars) {
|
||||
this.vars = vars;
|
||||
}
|
||||
|
||||
@@ -43,6 +43,8 @@ public class CodegenParameter implements IJsonSchemaValidationProperties {
|
||||
public Map<String, Object> allowableValues;
|
||||
public CodegenProperty items;
|
||||
public CodegenProperty additionalProperties;
|
||||
public List<CodegenProperty> vars = new ArrayList<CodegenProperty>(); // all properties (without parent's properties)
|
||||
public List<CodegenProperty> requiredVars = new ArrayList<CodegenProperty>();
|
||||
public CodegenProperty mostInnerItems;
|
||||
public Map<String, Object> vendorExtensions = new HashMap<String, Object>();
|
||||
public boolean hasValidation;
|
||||
@@ -157,6 +159,12 @@ public class CodegenParameter implements IJsonSchemaValidationProperties {
|
||||
if (this.items != null) {
|
||||
output.items = this.items;
|
||||
}
|
||||
if (this.vars != null) {
|
||||
output.vars = this.vars;
|
||||
}
|
||||
if (this.requiredVars != null) {
|
||||
output.requiredVars = this.requiredVars;
|
||||
}
|
||||
if (this.mostInnerItems != null) {
|
||||
output.mostInnerItems = this.mostInnerItems;
|
||||
}
|
||||
@@ -194,7 +202,7 @@ public class CodegenParameter implements IJsonSchemaValidationProperties {
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(isFormParam, isQueryParam, isPathParam, isHeaderParam, isCookieParam, isBodyParam, hasMore, isContainer, secondaryParam, isCollectionFormatMulti, isPrimitiveType, isModel, isExplode, baseName, paramName, dataType, datatypeWithEnum, dataFormat, collectionFormat, description, unescapedDescription, baseType, defaultValue, enumName, style, 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, vendorExtensions, hasValidation, getMaxProperties(), getMinProperties(), isNullable, required, getMaximum(), getExclusiveMaximum(), getMinimum(), getExclusiveMinimum(), getMaxLength(), getMinLength(), getPattern(), getMaxItems(), getMinItems(), getUniqueItems(), contentType, multipleOf);
|
||||
return Objects.hash(isFormParam, isQueryParam, isPathParam, isHeaderParam, isCookieParam, isBodyParam, hasMore, isContainer, secondaryParam, isCollectionFormatMulti, isPrimitiveType, isModel, isExplode, baseName, paramName, dataType, datatypeWithEnum, dataFormat, collectionFormat, description, unescapedDescription, baseType, defaultValue, enumName, style, 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);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -261,6 +269,8 @@ public class CodegenParameter implements IJsonSchemaValidationProperties {
|
||||
Objects.equals(allowableValues, that.allowableValues) &&
|
||||
Objects.equals(items, that.items) &&
|
||||
Objects.equals(additionalProperties, that.additionalProperties) &&
|
||||
Objects.equals(vars, that.vars) &&
|
||||
Objects.equals(requiredVars, that.requiredVars) &&
|
||||
Objects.equals(mostInnerItems, that.mostInnerItems) &&
|
||||
Objects.equals(vendorExtensions, that.vendorExtensions) &&
|
||||
Objects.equals(getMaxProperties(), that.getMaxProperties()) &&
|
||||
@@ -333,6 +343,8 @@ public class CodegenParameter implements IJsonSchemaValidationProperties {
|
||||
sb.append(", items=").append(items);
|
||||
sb.append(", mostInnerItems=").append(mostInnerItems);
|
||||
sb.append(", additionalProperties=").append(additionalProperties);
|
||||
sb.append(", vars=").append(vars);
|
||||
sb.append(", requiredVars=").append(requiredVars);
|
||||
sb.append(", vendorExtensions=").append(vendorExtensions);
|
||||
sb.append(", hasValidation=").append(hasValidation);
|
||||
sb.append(", maxProperties=").append(maxProperties);
|
||||
@@ -542,5 +554,25 @@ public class CodegenParameter implements IJsonSchemaValidationProperties {
|
||||
public void setAdditionalProperties(CodegenProperty additionalProperties) {
|
||||
this.additionalProperties = additionalProperties;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<CodegenProperty> getVars() {
|
||||
return vars;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setVars(List<CodegenProperty> vars) {
|
||||
this.vars = vars;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<CodegenProperty> getRequiredVars() {
|
||||
return requiredVars;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setRequiredVars(List<CodegenProperty> requiredVars) {
|
||||
this.requiredVars = requiredVars;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -161,6 +161,8 @@ public class CodegenProperty implements Cloneable, IJsonSchemaValidationProperti
|
||||
// the undeclared properties.
|
||||
public CodegenProperty items;
|
||||
public CodegenProperty additionalProperties;
|
||||
public List<CodegenProperty> vars = new ArrayList<CodegenProperty>(); // all properties (without parent's properties)
|
||||
public List<CodegenProperty> requiredVars = new ArrayList<>();
|
||||
public CodegenProperty mostInnerItems;
|
||||
public Map<String, Object> vendorExtensions = new HashMap<String, Object>();
|
||||
public boolean hasValidation; // true if pattern, maximum, etc are set (only used in the mustache template)
|
||||
@@ -607,6 +609,12 @@ public class CodegenProperty implements Cloneable, IJsonSchemaValidationProperti
|
||||
if (this.additionalProperties != null) {
|
||||
cp.additionalProperties = this.additionalProperties;
|
||||
}
|
||||
if (this.vars != null) {
|
||||
cp.vars = this.vars;
|
||||
}
|
||||
if (this.requiredVars != null) {
|
||||
cp.requiredVars = this.requiredVars;
|
||||
}
|
||||
if (this.mostInnerItems != null) {
|
||||
cp.mostInnerItems = this.mostInnerItems;
|
||||
}
|
||||
@@ -658,6 +666,26 @@ public class CodegenProperty implements Cloneable, IJsonSchemaValidationProperti
|
||||
this.multipleOf = multipleOf;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<CodegenProperty> getVars() {
|
||||
return vars;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setVars(List<CodegenProperty> vars) {
|
||||
this.vars = vars;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<CodegenProperty> getRequiredVars() {
|
||||
return requiredVars;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setRequiredVars(List<CodegenProperty> requiredVars) {
|
||||
this.requiredVars = requiredVars;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
final StringBuilder sb = new StringBuilder("CodegenProperty{");
|
||||
@@ -727,6 +755,8 @@ public class CodegenProperty implements Cloneable, IJsonSchemaValidationProperti
|
||||
sb.append(", allowableValues=").append(allowableValues);
|
||||
sb.append(", items=").append(items);
|
||||
sb.append(", additionalProperties=").append(additionalProperties);
|
||||
sb.append(", vars=").append(vars);
|
||||
sb.append(", requiredVars=").append(requiredVars);
|
||||
sb.append(", mostInnerItems=").append(mostInnerItems);
|
||||
sb.append(", vendorExtensions=").append(vendorExtensions);
|
||||
sb.append(", hasValidation=").append(hasValidation);
|
||||
@@ -825,6 +855,8 @@ public class CodegenProperty implements Cloneable, IJsonSchemaValidationProperti
|
||||
Objects.equals(allowableValues, that.allowableValues) &&
|
||||
Objects.equals(items, that.items) &&
|
||||
Objects.equals(additionalProperties, that.additionalProperties) &&
|
||||
Objects.equals(vars, that.vars) &&
|
||||
Objects.equals(requiredVars, that.requiredVars) &&
|
||||
Objects.equals(mostInnerItems, that.mostInnerItems) &&
|
||||
Objects.equals(vendorExtensions, that.vendorExtensions) &&
|
||||
Objects.equals(discriminatorValue, that.discriminatorValue) &&
|
||||
@@ -852,7 +884,7 @@ public class CodegenProperty implements Cloneable, IJsonSchemaValidationProperti
|
||||
isBoolean, isDate, isDateTime, isUuid, isUri, isEmail, isFreeFormObject,
|
||||
isArray, isMap, isEnum, isReadOnly, isWriteOnly, isNullable,
|
||||
isSelfReference, isCircularReference, isDiscriminator, _enum, allowableValues,
|
||||
items, mostInnerItems, additionalProperties,
|
||||
items, mostInnerItems, additionalProperties, vars, requiredVars,
|
||||
vendorExtensions, hasValidation, isInherited, discriminatorValue, nameInCamelCase,
|
||||
nameInSnakeCase, enumName, maxItems, minItems, isXmlAttribute, xmlPrefix, xmlName,
|
||||
xmlNamespace, isXmlWrapped);
|
||||
|
||||
@@ -76,6 +76,8 @@ public class CodegenResponse implements IJsonSchemaValidationProperties {
|
||||
public Number multipleOf;
|
||||
public CodegenProperty items;
|
||||
public CodegenProperty additionalProperties;
|
||||
public List<CodegenProperty> vars = new ArrayList<CodegenProperty>(); // all properties (without parent's properties)
|
||||
public List<CodegenProperty> requiredVars = new ArrayList<CodegenProperty>();
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
@@ -83,6 +85,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,
|
||||
getMaxProperties(), getMinProperties(), uniqueItems, getMaxItems(), getMinItems(), getMaxLength(),
|
||||
getMinLength(), exclusiveMinimum, exclusiveMaximum, getMinimum(), getMaximum(), getPattern());
|
||||
}
|
||||
@@ -120,6 +123,8 @@ public class CodegenResponse implements IJsonSchemaValidationProperties {
|
||||
isFile == that.isFile &&
|
||||
items == that.items &&
|
||||
additionalProperties == that.additionalProperties &&
|
||||
Objects.equals(vars, that.vars) &&
|
||||
Objects.equals(requiredVars, that.requiredVars) &&
|
||||
Objects.equals(headers, that.headers) &&
|
||||
Objects.equals(code, that.code) &&
|
||||
Objects.equals(message, that.message) &&
|
||||
@@ -334,6 +339,26 @@ public class CodegenResponse implements IJsonSchemaValidationProperties {
|
||||
this.additionalProperties = additionalProperties;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<CodegenProperty> getVars() {
|
||||
return vars;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setVars(List<CodegenProperty> vars) {
|
||||
this.vars = vars;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<CodegenProperty> getRequiredVars() {
|
||||
return requiredVars;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setRequiredVars(List<CodegenProperty> requiredVars) {
|
||||
this.requiredVars = requiredVars;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
final StringBuilder sb = new StringBuilder("CodegenResponse{");
|
||||
@@ -388,6 +413,8 @@ public class CodegenResponse implements IJsonSchemaValidationProperties {
|
||||
sb.append(", multipleOf='").append(multipleOf).append('\'');
|
||||
sb.append(", items='").append(items).append('\'');
|
||||
sb.append(", additionalProperties='").append(additionalProperties).append('\'');
|
||||
sb.append(", vars='").append(vars).append('\'');
|
||||
sb.append(", requiredVars='").append(requiredVars).append('\'');
|
||||
sb.append('}');
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
@@ -3374,24 +3374,7 @@ public class DefaultCodegen implements CodegenConfig {
|
||||
property.isModel = (ModelUtils.isComposedSchema(refOrCurrent) || ModelUtils.isObjectSchema(refOrCurrent)) && ModelUtils.isModel(refOrCurrent);
|
||||
}
|
||||
|
||||
// process 'additionalProperties'
|
||||
if ("object".equals(p.getType())) {
|
||||
if (p.getAdditionalProperties() == null) {
|
||||
if (!disallowAdditionalPropertiesIfNotPresent) {
|
||||
CodegenProperty cp = fromProperty("", new Schema());
|
||||
property.setAdditionalProperties(cp);
|
||||
}
|
||||
} else if (p.getAdditionalProperties() instanceof Boolean) {
|
||||
if (Boolean.TRUE.equals(p.getAdditionalProperties())) {
|
||||
CodegenProperty cp = fromProperty("", new Schema());
|
||||
property.setAdditionalProperties(cp);
|
||||
}
|
||||
} else {
|
||||
CodegenProperty cp = fromProperty("", (Schema) p.getAdditionalProperties());
|
||||
property.setAdditionalProperties(cp);
|
||||
}
|
||||
}
|
||||
|
||||
addVarsRequiredVarsAdditionaProps(p, property);
|
||||
LOGGER.debug("debugging from property return: " + property);
|
||||
schemaCodegenPropertyCache.put(ns, property);
|
||||
return property;
|
||||
@@ -4120,23 +4103,7 @@ public class DefaultCodegen implements CodegenConfig {
|
||||
|
||||
r.primitiveType = (r.baseType == null || languageSpecificPrimitives().contains(r.baseType));
|
||||
|
||||
// process 'additionalProperties'
|
||||
if ("object".equals(responseSchema.getType())) {
|
||||
if (responseSchema.getAdditionalProperties() == null) {
|
||||
if (!disallowAdditionalPropertiesIfNotPresent) {
|
||||
CodegenProperty addPropProp = fromProperty("", new Schema());
|
||||
r.setAdditionalProperties(addPropProp);
|
||||
}
|
||||
} else if (responseSchema.getAdditionalProperties() instanceof Boolean) {
|
||||
if (Boolean.TRUE.equals(responseSchema.getAdditionalProperties())) {
|
||||
CodegenProperty addPropProp = fromProperty("", new Schema());
|
||||
r.setAdditionalProperties(addPropProp);
|
||||
}
|
||||
} else {
|
||||
CodegenProperty addPropProp = fromProperty("", (Schema) responseSchema.getAdditionalProperties());
|
||||
r.setAdditionalProperties(addPropProp);
|
||||
}
|
||||
}
|
||||
addVarsRequiredVarsAdditionaProps(responseSchema, r);
|
||||
}
|
||||
|
||||
if (r.baseType == null) {
|
||||
@@ -4425,24 +4392,7 @@ public class DefaultCodegen implements CodegenConfig {
|
||||
codegenParameter.pattern != null || codegenParameter.multipleOf != null) {
|
||||
codegenParameter.hasValidation = true;
|
||||
}
|
||||
|
||||
// process 'additionalProperties'
|
||||
if ("object".equals(parameterSchema.getType())) {
|
||||
if (parameterSchema.getAdditionalProperties() == null) {
|
||||
if (!disallowAdditionalPropertiesIfNotPresent) {
|
||||
CodegenProperty cp = fromProperty("", new Schema());
|
||||
codegenParameter.setAdditionalProperties(cp);
|
||||
}
|
||||
} else if (parameterSchema.getAdditionalProperties() instanceof Boolean) {
|
||||
if (Boolean.TRUE.equals(parameterSchema.getAdditionalProperties())) {
|
||||
CodegenProperty cp = fromProperty("", new Schema());
|
||||
codegenParameter.setAdditionalProperties(cp);
|
||||
}
|
||||
} else {
|
||||
CodegenProperty cp = fromProperty("", (Schema) parameterSchema.getAdditionalProperties());
|
||||
codegenParameter.setAdditionalProperties(cp);
|
||||
}
|
||||
}
|
||||
addVarsRequiredVarsAdditionaProps(parameterSchema, codegenParameter);
|
||||
|
||||
} else {
|
||||
LOGGER.error("ERROR! Not handling " + parameter + " as Body Parameter at the moment");
|
||||
@@ -4912,12 +4862,19 @@ public class DefaultCodegen implements CodegenConfig {
|
||||
/**
|
||||
* Add variables (properties) to codegen model (list of properties, various flags, etc)
|
||||
*
|
||||
* @param m Codegen model
|
||||
* @param m Must be an instance of IJsonSchemaValidationProperties, may be model or property...
|
||||
* @param vars list of codegen properties (e.g. vars, allVars) to be updated with the new properties
|
||||
* @param properties a map of properties (schema)
|
||||
* @param mandatory a set of required properties' name
|
||||
*/
|
||||
private void addVars(CodegenModel m, List<CodegenProperty> vars, Map<String, Schema> properties, Set<String> mandatory) {
|
||||
private void addVars(IJsonSchemaValidationProperties m, List<CodegenProperty> vars, Map<String, Schema> properties, Set<String> mandatory) {
|
||||
if (properties == null) {
|
||||
return;
|
||||
}
|
||||
CodegenModel cm = null;
|
||||
if (m instanceof CodegenModel) {
|
||||
cm = (CodegenModel) m;
|
||||
}
|
||||
for (Map.Entry<String, Schema> entry : properties.entrySet()) {
|
||||
|
||||
final String key = entry.getKey();
|
||||
@@ -4927,48 +4884,52 @@ public class DefaultCodegen implements CodegenConfig {
|
||||
} else {
|
||||
final CodegenProperty cp = fromProperty(key, prop);
|
||||
cp.required = mandatory.contains(key);
|
||||
m.hasRequired = m.hasRequired || cp.required;
|
||||
m.hasOptional = m.hasOptional || !cp.required;
|
||||
vars.add(cp);
|
||||
if (cm == null) {
|
||||
continue;
|
||||
}
|
||||
cm.hasRequired = cm.hasRequired || cp.required;
|
||||
cm.hasOptional = cm.hasOptional || !cp.required;
|
||||
if (cp.isEnum) {
|
||||
// FIXME: if supporting inheritance, when called a second time for allProperties it is possible for
|
||||
// m.hasEnums to be set incorrectly if allProperties has enumerations but properties does not.
|
||||
m.hasEnums = true;
|
||||
cm.hasEnums = true;
|
||||
}
|
||||
|
||||
// set model's hasOnlyReadOnly to false if the property is read-only
|
||||
if (!Boolean.TRUE.equals(cp.isReadOnly)) {
|
||||
m.hasOnlyReadOnly = false;
|
||||
cm.hasOnlyReadOnly = false;
|
||||
}
|
||||
|
||||
// TODO revise the logic to include map
|
||||
if (cp.isContainer) {
|
||||
addImport(m, typeMapping.get("array"));
|
||||
addImport(cm, typeMapping.get("array"));
|
||||
}
|
||||
|
||||
addImport(m, cp.baseType);
|
||||
addImport(cm, cp.baseType);
|
||||
CodegenProperty innerCp = cp;
|
||||
while (innerCp != null) {
|
||||
addImport(m, innerCp.complexType);
|
||||
addImport(cm, innerCp.complexType);
|
||||
innerCp = innerCp.items;
|
||||
}
|
||||
vars.add(cp);
|
||||
|
||||
// if required, add to the list "requiredVars"
|
||||
if (Boolean.TRUE.equals(cp.required)) {
|
||||
m.requiredVars.add(cp);
|
||||
cm.requiredVars.add(cp);
|
||||
} else { // else add to the list "optionalVars" for optional property
|
||||
m.optionalVars.add(cp);
|
||||
cm.optionalVars.add(cp);
|
||||
}
|
||||
|
||||
// if readonly, add to readOnlyVars (list of properties)
|
||||
if (Boolean.TRUE.equals(cp.isReadOnly)) {
|
||||
m.readOnlyVars.add(cp);
|
||||
cm.readOnlyVars.add(cp);
|
||||
} else { // else add to readWriteVars (list of properties)
|
||||
// duplicated properties will be removed by removeAllDuplicatedProperty later
|
||||
m.readWriteVars.add(cp);
|
||||
cm.readWriteVars.add(cp);
|
||||
}
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -6242,6 +6203,7 @@ public class DefaultCodegen implements CodegenConfig {
|
||||
setParameterNullable(codegenParameter, codegenProperty);
|
||||
}
|
||||
|
||||
addVarsRequiredVarsAdditionaProps(schema, codegenParameter);
|
||||
addJsonSchemaForBodyRequestInCaseItsNotPresent(codegenParameter, body);
|
||||
|
||||
// set the parameter's example value
|
||||
@@ -6251,6 +6213,39 @@ public class DefaultCodegen implements CodegenConfig {
|
||||
return codegenParameter;
|
||||
}
|
||||
|
||||
private void addVarsRequiredVarsAdditionaProps(Schema schema, IJsonSchemaValidationProperties property){
|
||||
if (!"object".equals(schema.getType())) {
|
||||
return;
|
||||
}
|
||||
if (schema instanceof ObjectSchema) {
|
||||
ObjectSchema objSchema = (ObjectSchema) schema;
|
||||
HashSet<String> requiredVars = new HashSet<>();
|
||||
if (objSchema.getRequired() != null) {
|
||||
requiredVars.addAll(objSchema.getRequired());
|
||||
}
|
||||
addVars(property, property.getVars(), objSchema.getProperties(), requiredVars);
|
||||
List<CodegenProperty> requireCpVars = property.getVars()
|
||||
.stream()
|
||||
.filter(p -> Boolean.TRUE.equals(p.required)).collect(Collectors.toList());
|
||||
property.setRequiredVars(requireCpVars);
|
||||
}
|
||||
if (schema.getAdditionalProperties() == null) {
|
||||
if (!disallowAdditionalPropertiesIfNotPresent) {
|
||||
CodegenProperty cp = fromProperty("", new Schema());
|
||||
property.setAdditionalProperties(cp);
|
||||
}
|
||||
} else if (schema.getAdditionalProperties() instanceof Boolean) {
|
||||
if (Boolean.TRUE.equals(schema.getAdditionalProperties())) {
|
||||
CodegenProperty cp = fromProperty("", new Schema());
|
||||
property.setAdditionalProperties(cp);
|
||||
}
|
||||
} else {
|
||||
CodegenProperty cp = fromProperty("", (Schema) schema.getAdditionalProperties());
|
||||
property.setAdditionalProperties(cp);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
private void addJsonSchemaForBodyRequestInCaseItsNotPresent(CodegenParameter codegenParameter, RequestBody body) {
|
||||
if (codegenParameter.jsonSchema == null)
|
||||
codegenParameter.jsonSchema = Json.pretty(body);
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package org.openapitools.codegen;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface IJsonSchemaValidationProperties {
|
||||
String getPattern();
|
||||
|
||||
@@ -80,4 +82,12 @@ public interface IJsonSchemaValidationProperties {
|
||||
CodegenProperty getAdditionalProperties();
|
||||
|
||||
void setAdditionalProperties(CodegenProperty additionalProperties);
|
||||
|
||||
List<CodegenProperty> getVars();
|
||||
|
||||
void setVars(List<CodegenProperty> vars);
|
||||
|
||||
List<CodegenProperty> getRequiredVars();
|
||||
|
||||
void setRequiredVars(List<CodegenProperty> requiredVars);
|
||||
}
|
||||
|
||||
@@ -2581,4 +2581,66 @@ public class DefaultCodegenTest {
|
||||
assertEquals(co.responses.get(0).isString, false);
|
||||
assertEquals(co.responses.get(0).isDateTime, true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testVarsAndRequiredVarsPresent() {
|
||||
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 modelName;
|
||||
Schema sc;
|
||||
CodegenModel cm;
|
||||
CodegenProperty propA = codegen.fromProperty("a", new Schema().type("string").minLength(1));
|
||||
propA.hasMore = true;
|
||||
propA.setRequired(true);
|
||||
CodegenProperty propB = codegen.fromProperty("b", new Schema().type("string").minLength(1));
|
||||
propB.hasMore = true;
|
||||
propB.setRequired(true);
|
||||
CodegenProperty propC = codegen.fromProperty("c", new Schema().type("string").minLength(1));
|
||||
propC.hasMore = false;
|
||||
propC.setRequired(false);
|
||||
CodegenProperty propBRequired = propB.clone();
|
||||
propBRequired.hasMore = false;
|
||||
|
||||
List<CodegenProperty> vars = new ArrayList<>(Arrays.asList(propA, propB, propC));
|
||||
List<CodegenProperty> requiredVars = new ArrayList<>(Arrays.asList(propA, propBRequired));
|
||||
|
||||
modelName = "ObjectWithOptionalAndRequiredProps";
|
||||
sc = openAPI.getComponents().getSchemas().get(modelName);
|
||||
cm = codegen.fromModel(modelName, sc);
|
||||
assertEquals(cm.vars, vars);
|
||||
assertEquals(cm.requiredVars, requiredVars);
|
||||
|
||||
String path;
|
||||
Operation operation;
|
||||
CodegenOperation co;
|
||||
|
||||
path = "/object_with_optional_and_required_props/{objectData}";
|
||||
operation = openAPI.getPaths().get(path).getPost();
|
||||
co = codegen.fromOperation(path, "POST", operation, null);
|
||||
// keep size() checks until https://github.com/OpenAPITools/openapi-generator/pull/7882 lands
|
||||
assertEquals(co.pathParams.get(0).vars.size(), vars.size());
|
||||
assertEquals(co.pathParams.get(0).requiredVars.size(), requiredVars.size());
|
||||
assertEquals(co.bodyParams.get(0).vars.size(), vars.size());
|
||||
assertEquals(co.bodyParams.get(0).requiredVars.size(), requiredVars.size());
|
||||
|
||||
// CodegenOperation puts the inline schema into schemas and refs it
|
||||
assertEquals(co.responses.get(0).isModel, true);
|
||||
assertEquals(co.responses.get(0).baseType, "objectData");
|
||||
modelName = "objectData";
|
||||
sc = openAPI.getComponents().getSchemas().get(modelName);
|
||||
cm = codegen.fromModel(modelName, sc);
|
||||
assertEquals(cm.vars, vars);
|
||||
assertEquals(cm.requiredVars, requiredVars);
|
||||
|
||||
// CodegenProperty puts the inline schema into schemas and refs it
|
||||
modelName = "ObjectPropContainsProps";
|
||||
sc = openAPI.getComponents().getSchemas().get(modelName);
|
||||
cm = codegen.fromModel(modelName, sc);
|
||||
CodegenProperty cp = cm.getVars().get(0);
|
||||
assertEquals(cp.isModel, true);
|
||||
assertEquals(cp.complexType, "objectData");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -182,6 +182,69 @@ paths:
|
||||
type: object
|
||||
additionalProperties:
|
||||
type: string
|
||||
/object_with_optional_and_required_props/{objectData}:
|
||||
post:
|
||||
tags:
|
||||
- vars
|
||||
operationId: objectWithOptionalAndRequiredProps
|
||||
parameters:
|
||||
- name: objectData
|
||||
in: path
|
||||
required: true
|
||||
schema:
|
||||
type: object
|
||||
properties:
|
||||
a:
|
||||
type: string
|
||||
minLength: 1
|
||||
b:
|
||||
type: string
|
||||
minLength: 1
|
||||
c:
|
||||
type: string
|
||||
minLength: 1
|
||||
required:
|
||||
- a
|
||||
- b
|
||||
requestBody:
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
type: object
|
||||
properties:
|
||||
a:
|
||||
type: string
|
||||
minLength: 1
|
||||
b:
|
||||
type: string
|
||||
minLength: 1
|
||||
c:
|
||||
type: string
|
||||
minLength: 1
|
||||
required:
|
||||
- a
|
||||
- b
|
||||
required: true
|
||||
responses:
|
||||
200:
|
||||
description: success
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
type: object
|
||||
properties:
|
||||
a:
|
||||
type: string
|
||||
minLength: 1
|
||||
b:
|
||||
type: string
|
||||
minLength: 1
|
||||
c:
|
||||
type: string
|
||||
minLength: 1
|
||||
required:
|
||||
- a
|
||||
- b
|
||||
components:
|
||||
schemas:
|
||||
ArrayWithValidationsInItems:
|
||||
@@ -258,4 +321,37 @@ components:
|
||||
type: object
|
||||
additionalProperties:
|
||||
type: string
|
||||
ObjectWithOptionalAndRequiredProps:
|
||||
type: object
|
||||
properties:
|
||||
a:
|
||||
type: string
|
||||
minLength: 1
|
||||
b:
|
||||
type: string
|
||||
minLength: 1
|
||||
c:
|
||||
type: string
|
||||
minLength: 1
|
||||
required:
|
||||
- a
|
||||
- b
|
||||
ObjectPropContainsProps:
|
||||
type: object
|
||||
properties:
|
||||
a:
|
||||
type: object
|
||||
properties:
|
||||
a:
|
||||
type: string
|
||||
minLength: 1
|
||||
b:
|
||||
type: string
|
||||
minLength: 1
|
||||
c:
|
||||
type: string
|
||||
minLength: 1
|
||||
required:
|
||||
- a
|
||||
- b
|
||||
securitySchemes: {}
|
||||
Reference in New Issue
Block a user