Skip to content
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 @@ -164,6 +164,7 @@ public class CodegenModel implements IJsonSchemaValidationProperties {
private boolean hasDiscriminatorWithNonEmptyMapping;
private boolean isAnyType;
private boolean isUuid;
private Map<String, CodegenProperty> requiredVarsMap;

public String getAdditionalPropertiesType() {
return additionalPropertiesType;
Expand Down Expand Up @@ -928,6 +929,7 @@ public boolean equals(Object o) {
getUniqueItems() == that.getUniqueItems() &&
getExclusiveMinimum() == that.getExclusiveMinimum() &&
getExclusiveMaximum() == that.getExclusiveMaximum() &&
Objects.equals(requiredVarsMap, that.getRequiredVarsMap()) &&
Objects.equals(composedSchemas, that.composedSchemas) &&
Objects.equals(parent, that.parent) &&
Objects.equals(parentSchema, that.parentSchema) &&
Expand Down Expand Up @@ -1000,7 +1002,7 @@ hasChildren, isMap, isDeprecated, hasOnlyReadOnly, getExternalDocumentation(), g
getMinItems(), getMaxLength(), getMinLength(), getExclusiveMinimum(), getExclusiveMaximum(), getMinimum(),
getMaximum(), getPattern(), getMultipleOf(), getItems(), getAdditionalProperties(), getIsModel(),
getAdditionalPropertiesIsAnyType(), hasDiscriminatorWithNonEmptyMapping,
isAnyType, getComposedSchemas(), hasMultipleTypes, isDecimal, isUuid);
isAnyType, getComposedSchemas(), hasMultipleTypes, isDecimal, isUuid, requiredVarsMap);
}

@Override
Expand Down Expand Up @@ -1097,6 +1099,7 @@ public String toString() {
sb.append(", hasMultipleTypes=").append(hasMultipleTypes);
sb.append(", isDecimal=").append(isDecimal);
sb.append(", isUUID=").append(isUuid);
sb.append(", requiredVarsMap=").append(requiredVarsMap);
sb.append('}');
return sb.toString();
}
Expand Down Expand Up @@ -1124,6 +1127,12 @@ public boolean getHasItems() {
return this.items != null;
}

@Override
public Map<String, CodegenProperty> getRequiredVarsMap() { return requiredVarsMap; }

@Override
public void setRequiredVarsMap(Map<String, CodegenProperty> requiredVarsMap) { this.requiredVarsMap=requiredVarsMap; }

/**
* Remove duplicated properties in all variable list
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ public class CodegenParameter implements IJsonSchemaValidationProperties {
private CodegenComposedSchemas composedSchemas;
private boolean hasMultipleTypes = false;
private LinkedHashMap<String, CodegenMediaType> content;
private Map<String, CodegenProperty> requiredVarsMap;

public CodegenParameter copy() {
CodegenParameter output = new CodegenParameter();
Expand Down Expand Up @@ -194,6 +195,9 @@ public CodegenParameter copy() {
if (this.vendorExtensions != null) {
output.vendorExtensions = new HashMap<String, Object>(this.vendorExtensions);
}
if (this.requiredVarsMap != null) {
output.setRequiredVarsMap(this.requiredVarsMap);
}
output.hasValidation = this.hasValidation;
output.isNullable = this.isNullable;
output.isDeprecated = this.isDeprecated;
Expand Down Expand Up @@ -230,7 +234,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, isAllowEmptyValue, example, jsonSchema, isString, isNumeric, isInteger, isLong, isNumber, isFloat, isDouble, isDecimal, isByteArray, isBinary, isBoolean, isDate, isDateTime, isUuid, isUri, isEmail, isFreeFormObject, isAnyType, isArray, isMap, isFile, isEnum, _enum, allowableValues, items, mostInnerItems, additionalProperties, vars, requiredVars, vendorExtensions, hasValidation, getMaxProperties(), getMinProperties(), isNullable, isDeprecated, required, getMaximum(), getExclusiveMaximum(), getMinimum(), getExclusiveMinimum(), getMaxLength(), getMinLength(), getPattern(), getMaxItems(), getMinItems(), getUniqueItems(), contentType, multipleOf, isNull, additionalPropertiesIsAnyType, hasVars, hasRequired, isShort, isUnboundedInteger, hasDiscriminatorWithNonEmptyMapping, composedSchemas, hasMultipleTypes, schema, content);
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, isAllowEmptyValue, example, jsonSchema, isString, isNumeric, isInteger, isLong, isNumber, isFloat, isDouble, isDecimal, isByteArray, isBinary, isBoolean, isDate, isDateTime, isUuid, isUri, isEmail, isFreeFormObject, isAnyType, isArray, isMap, isFile, isEnum, _enum, allowableValues, items, mostInnerItems, additionalProperties, vars, requiredVars, vendorExtensions, hasValidation, getMaxProperties(), getMinProperties(), isNullable, isDeprecated, required, getMaximum(), getExclusiveMaximum(), getMinimum(), getExclusiveMinimum(), getMaxLength(), getMinLength(), getPattern(), getMaxItems(), getMinItems(), getUniqueItems(), contentType, multipleOf, isNull, additionalPropertiesIsAnyType, hasVars, hasRequired, isShort, isUnboundedInteger, hasDiscriminatorWithNonEmptyMapping, composedSchemas, hasMultipleTypes, schema, content, requiredVarsMap);
}

@Override
Expand Down Expand Up @@ -286,6 +290,7 @@ public boolean equals(Object o) {
getExclusiveMaximum() == that.getExclusiveMaximum() &&
getExclusiveMinimum() == that.getExclusiveMinimum() &&
getUniqueItems() == that.getUniqueItems() &&
Objects.equals(requiredVarsMap, that.getRequiredVarsMap()) &&
Objects.equals(content, that.getContent()) &&
Objects.equals(schema, that.getSchema()) &&
Objects.equals(composedSchemas, that.getComposedSchemas()) &&
Expand Down Expand Up @@ -415,6 +420,7 @@ public String toString() {
sb.append(", hasMultipleTypes=").append(hasMultipleTypes);
sb.append(", schema=").append(schema);
sb.append(", content=").append(content);
sb.append(", requiredVarsMap=").append(requiredVarsMap);
sb.append('}');
return sb.toString();
}
Expand Down Expand Up @@ -815,5 +821,11 @@ public void setContent(LinkedHashMap<String, CodegenMediaType> content) {
public String getBaseType() {
return baseType;
}

@Override
public Map<String, CodegenProperty> getRequiredVarsMap() { return requiredVarsMap; }

@Override
public void setRequiredVarsMap(Map<String, CodegenProperty> requiredVarsMap) { this.requiredVarsMap=requiredVarsMap; }
}

Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ public class CodegenProperty implements Cloneable, IJsonSchemaValidationProperti
private boolean hasDiscriminatorWithNonEmptyMapping;
private CodegenComposedSchemas composedSchemas = null;
private boolean hasMultipleTypes = false;
private Map<String, CodegenProperty> requiredVarsMap;

public String getBaseName() {
return baseName;
Expand Down Expand Up @@ -690,6 +691,9 @@ public CodegenProperty clone() {
if (this.composedSchemas != null) {
cp.composedSchemas = this.composedSchemas;
}
if (this.requiredVarsMap != null) {
cp.setRequiredVarsMap(this.requiredVarsMap);
}

return cp;
} catch (CloneNotSupportedException e) {
Expand Down Expand Up @@ -867,6 +871,12 @@ public void setHasMultipleTypes(boolean hasMultipleTypes) {

public void setIsUuid(boolean isUuid) { this.isUuid = isUuid; }

@Override
public Map<String, CodegenProperty> getRequiredVarsMap() { return requiredVarsMap; }

@Override
public void setRequiredVarsMap(Map<String, CodegenProperty> requiredVarsMap) { this.requiredVarsMap=requiredVarsMap; }

@Override
public String toString() {
final StringBuilder sb = new StringBuilder("CodegenProperty{");
Expand Down Expand Up @@ -966,6 +976,7 @@ public String toString() {
sb.append(", getHasDiscriminatorWithNonEmptyMapping=").append(hasDiscriminatorWithNonEmptyMapping);
sb.append(", composedSchemas=").append(composedSchemas);
sb.append(", hasMultipleTypes=").append(hasMultipleTypes);
sb.append(", requiredVarsMap=").append(requiredVarsMap);
sb.append('}');
return sb.toString();
}
Expand Down Expand Up @@ -1024,6 +1035,7 @@ public boolean equals(Object o) {
getAdditionalPropertiesIsAnyType() == that.getAdditionalPropertiesIsAnyType() &&
getHasVars() == that.getHasVars() &&
getHasRequired() == that.getHasRequired() &&
Objects.equals(requiredVarsMap, that.getRequiredVarsMap()) &&
Objects.equals(composedSchemas, that.composedSchemas) &&
Objects.equals(openApiType, that.openApiType) &&
Objects.equals(baseName, that.baseName) &&
Expand Down Expand Up @@ -1087,6 +1099,6 @@ public int hashCode() {
vendorExtensions, hasValidation, isInherited, discriminatorValue, nameInCamelCase,
nameInSnakeCase, enumName, maxItems, minItems, isXmlAttribute, xmlPrefix, xmlName,
xmlNamespace, isXmlWrapped, isNull, additionalPropertiesIsAnyType, hasVars, hasRequired,
hasDiscriminatorWithNonEmptyMapping, composedSchemas, hasMultipleTypes);
hasDiscriminatorWithNonEmptyMapping, composedSchemas, hasMultipleTypes, requiredVarsMap);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ public class CodegenResponse implements IJsonSchemaValidationProperties {
private CodegenComposedSchemas composedSchemas;
private boolean hasMultipleTypes = false;
private LinkedHashMap<String, CodegenMediaType> content;
private Map<String, CodegenProperty> requiredVarsMap;

@Override
public int hashCode() {
Expand All @@ -100,7 +101,8 @@ public int hashCode() {
getMaxProperties(), getMinProperties(), uniqueItems, getMaxItems(), getMinItems(), getMaxLength(),
getMinLength(), exclusiveMinimum, exclusiveMaximum, getMinimum(), getMaximum(), getPattern(),
is1xx, is2xx, is3xx, is4xx, is5xx, additionalPropertiesIsAnyType, hasVars, hasRequired,
hasDiscriminatorWithNonEmptyMapping, composedSchemas, hasMultipleTypes, responseHeaders, content);
hasDiscriminatorWithNonEmptyMapping, composedSchemas, hasMultipleTypes, responseHeaders, content,
requiredVarsMap);
}

@Override
Expand Down Expand Up @@ -149,6 +151,7 @@ public boolean equals(Object o) {
getAdditionalPropertiesIsAnyType() == that.getAdditionalPropertiesIsAnyType() &&
getHasVars() == that.getHasVars() &&
getHasRequired() == that.getHasRequired() &&
Objects.equals(requiredVarsMap, that.getRequiredVarsMap()) &&
Objects.equals(content, that.getContent()) &&
Objects.equals(responseHeaders, that.getResponseHeaders()) &&
Objects.equals(composedSchemas, that.getComposedSchemas()) &&
Expand Down Expand Up @@ -538,6 +541,7 @@ public String toString() {
sb.append(", hasMultipleTypes=").append(hasMultipleTypes);
sb.append(", responseHeaders=").append(responseHeaders);
sb.append(", content=").append(content);
sb.append(", requiredVarsMap=").append(requiredVarsMap);
sb.append('}');
return sb.toString();
}
Expand Down Expand Up @@ -665,4 +669,10 @@ public void setHasMultipleTypes(boolean hasMultipleTypes) {
public String getBaseType() {
return baseType;
}

@Override
public Map<String, CodegenProperty> getRequiredVarsMap() { return requiredVarsMap; }

@Override
public void setRequiredVarsMap(Map<String, CodegenProperty> requiredVarsMap) { this.requiredVarsMap=requiredVarsMap; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -2692,6 +2692,7 @@ protected void updateModelForObject(CodegenModel m, Schema schema) {
}
// process 'additionalProperties'
setAddProps(schema, m);
addRequiredVarsMap(schema, m);
}

protected void updateModelForAnyType(CodegenModel m, Schema schema) {
Expand All @@ -2712,6 +2713,7 @@ protected void updateModelForAnyType(CodegenModel m, Schema schema) {
}
// process 'additionalProperties'
setAddProps(schema, m);
addRequiredVarsMap(schema, m);
}

protected String toTestCaseName(String specTestCaseName) {
Expand Down Expand Up @@ -7100,11 +7102,70 @@ public CodegenParameter fromRequestBody(RequestBody body, Set<String> imports, S
return codegenParameter;
}

protected void addRequiredVarsMap(Schema schema, IJsonSchemaValidationProperties property) {
/*
this should be called after vars and additionalProperties are set
Features added by storing codegenProperty values:
- complexType stores reference to additionalProperties definition
- baseName stores original name (can be invalid in a programming language)
- nameInSnakeCase can store valid name for a programming language
*/
Map<String, Schema> properties = schema.getProperties();
Map<String, CodegenProperty> requiredVarsMap = new HashMap<>();
List<String> requiredPropertyNames = schema.getRequired();
if (requiredPropertyNames == null) {
return;
}
for (String requiredPropertyName: requiredPropertyNames) {
// required property is defined in properties, value is that CodegenProperty
String usedRequiredPropertyName = handleSpecialCharacters(requiredPropertyName);
if (properties != null && properties.containsKey(requiredPropertyName)) {
// get cp from property
boolean found = false;
for (CodegenProperty cp: property.getVars()) {
if (cp.baseName.equals(requiredPropertyName)) {
found = true;
requiredVarsMap.put(requiredPropertyName, cp);
}
}
if (found == false) {
throw new RuntimeException("Property " + requiredPropertyName + " is missing from getVars");
}
} else if (schema.getAdditionalProperties() instanceof Boolean && Boolean.FALSE.equals(schema.getAdditionalProperties())) {
// TODO add processing for requiredPropertyName
// required property is not defined in properties, and additionalProperties is false, value is null
requiredVarsMap.put(usedRequiredPropertyName, null);
} else {
// required property is not defined in properties, and additionalProperties is true or unset value is CodegenProperty made from empty schema
// required property is not defined in properties, and additionalProperties is schema, value is CodegenProperty made from schema
if (supportsAdditionalPropertiesWithComposedSchema && !disallowAdditionalPropertiesIfNotPresent) {
if (property.getAdditionalProperties() == null) {
throw new RuntimeException("additionalProperties is unset and should be set in" + schema.toString());
}
CodegenProperty cp;
if (schema.getAdditionalProperties() == null) {
cp = fromProperty(usedRequiredPropertyName, new Schema(), true);
} else if (schema.getAdditionalProperties() instanceof Boolean && Boolean.TRUE.equals(schema.getAdditionalProperties())) {
cp = fromProperty(requiredPropertyName, new Schema(), true);
} else {
CodegenProperty addPropsProp = property.getAdditionalProperties();
cp = addPropsProp;
}
requiredVarsMap.put(usedRequiredPropertyName, cp);
}
}
}
if (!requiredVarsMap.isEmpty()) {
property.setRequiredVarsMap(requiredVarsMap);
}
}

protected void addVarsRequiredVarsAdditionalProps(Schema schema, IJsonSchemaValidationProperties property) {
setAddProps(schema, property);
Set<String> mandatory = schema.getRequired() == null ? Collections.emptySet()
: new TreeSet<>(schema.getRequired());
addVars(property, property.getVars(), schema.getProperties(), mandatory);
addRequiredVarsMap(schema, property);
}

private void addJsonSchemaForBodyRequestInCaseItsNotPresent(CodegenParameter codegenParameter, RequestBody body) {
Expand Down Expand Up @@ -7666,4 +7727,11 @@ public List<VendorExtension> getSupportedVendorExtensions() {

@Override
public boolean getUseInlineModelResolver() { return true; }

/*
A function to convert yaml or json ingested strings like property names
And convert special characters like newline, tab, carriage return
Into strings that can be rendered in the language that the generator will output to
*/
protected String handleSpecialCharacters(String name) { return name; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Stream;

Expand Down Expand Up @@ -115,6 +116,17 @@ public interface IJsonSchemaValidationProperties {

void setRequiredVars(List<CodegenProperty> requiredVars);

Map<String, CodegenProperty> getRequiredVarsMap();

// goes from required propertyName to its CodegenProperty
// Use Cases:
// 1. required property is defined in properties, value is that CodegenProperty
// 2. required property is not defined in properties, and additionalProperties is true or unset value is CodegenProperty made from empty schema
// 3. required property is not defined in properties, and additionalProperties is schema, value is CodegenProperty made from schema
// 4. required property is not defined in properties, and additionalProperties is false, value is null
void setRequiredVarsMap(Map<String, CodegenProperty> requiredVarsMap);


boolean getIsNull();

void setIsNull(boolean isNull);
Expand Down
Loading