add nullable support (#873)

This commit is contained in:
William Cheng
2018-08-23 10:51:57 +08:00
committed by GitHub
parent 3d06900e68
commit 6a47d49ee8
4 changed files with 843 additions and 25 deletions

View File

@@ -59,7 +59,9 @@ public class CodegenProperty implements Cloneable {
public boolean isString, isNumeric, isInteger, isLong, isNumber, isFloat, isDouble, isByteArray, isBinary, isFile, isBoolean, isDate, isDateTime, isUuid;
public boolean isListContainer, isMapContainer;
public boolean isEnum;
public boolean isReadOnly = false;
public boolean isReadOnly;
public boolean isWriteOnly;
public boolean isNullable;
public List<String> _enum;
public Map<String, Object> allowableValues;
public CodegenProperty items;
@@ -437,6 +439,8 @@ public class CodegenProperty implements Cloneable {
result = prime * result + ((isNotContainer ? 13:31));
result = prime * result + ((isPrimitiveType ? 13:31));
result = prime * result + ((isReadOnly ? 13:31));
result = prime * result + ((isWriteOnly ? 13:31));
result = prime * result + ((isNullable ? 13:31));
result = prime * result + ((items == null) ? 0 : items.hashCode());
result = prime * result + ((mostInnerItems == null) ? 0 : mostInnerItems.hashCode());
result = prime * result + ((jsonSchema == null) ? 0 : jsonSchema.hashCode());
@@ -587,6 +591,12 @@ public class CodegenProperty implements Cloneable {
if (this.isReadOnly != other.isReadOnly) {
return false;
}
if (this.isWriteOnly != other.isWriteOnly) {
return false;
}
if (this.isNullable != other.isNullable) {
return false;
}
if (this._enum != other._enum && (this._enum == null || !this._enum.equals(other._enum))) {
return false;
}
@@ -769,6 +779,8 @@ public class CodegenProperty implements Cloneable {
", isMapContainer=" + isMapContainer +
", isEnum=" + isEnum +
", isReadOnly=" + isReadOnly +
", isWriteOnly=" + isWriteOnly+
", isNullable=" + isNullable +
", _enum=" + _enum +
", allowableValues=" + allowableValues +
", items=" + items +

View File

@@ -185,7 +185,7 @@ public class DefaultCodegen implements CodegenConfig {
.get(CodegenConstants.REMOVE_OPERATION_ID_PREFIX).toString()));
}
if (additionalProperties.containsKey(CodegenConstants.DOCEXTENSION)){
if (additionalProperties.containsKey(CodegenConstants.DOCEXTENSION)) {
this.setDocExtension(String.valueOf(additionalProperties
.get(CodegenConstants.DOCEXTENSION).toString()));
}
@@ -771,7 +771,7 @@ public class DefaultCodegen implements CodegenConfig {
public String toVarName(String name) {
if (reservedWords.contains(name)) {
return escapeReservedWord(name);
} else if (((CharSequence) name).chars().anyMatch(character -> specialCharReplacements.keySet().contains( "" + ((char) character)))) {
} else if (((CharSequence) name).chars().anyMatch(character -> specialCharReplacements.keySet().contains("" + ((char) character)))) {
return escapeSpecialCharacters(name, null, null);
} else {
return name;
@@ -789,7 +789,7 @@ public class DefaultCodegen implements CodegenConfig {
name = removeNonNameElementToCamelCase(name); // FIXME: a parameter should not be assigned. Also declare the methods parameters as 'final'.
if (reservedWords.contains(name)) {
return escapeReservedWord(name);
} else if (((CharSequence) name).chars().anyMatch(character -> specialCharReplacements.keySet().contains( "" + ((char) character)))) {
} else if (((CharSequence) name).chars().anyMatch(character -> specialCharReplacements.keySet().contains("" + ((char) character)))) {
return escapeSpecialCharacters(name, null, null);
}
return name;
@@ -832,8 +832,8 @@ public class DefaultCodegen implements CodegenConfig {
/**
* Return the name with escaped characters.
*
* @param name the name to be escaped
* @param charactersToAllow characters that are not escaped
* @param name the name to be escaped
* @param charactersToAllow characters that are not escaped
* @param appdendixToReplacement String to append to replaced characters.
* @return the escaped word
* <p>
@@ -841,15 +841,15 @@ public class DefaultCodegen implements CodegenConfig {
*/
public String escapeSpecialCharacters(String name, List<String> charactersToAllow, String appdendixToReplacement) {
String result = (String) ((CharSequence) name).chars().mapToObj(c -> {
String character = "" + (char) c;
if (charactersToAllow != null && charactersToAllow.contains(character)) {
return character;
} else if (specialCharReplacements.containsKey(character)) {
return specialCharReplacements.get(character) + (appdendixToReplacement != null ? appdendixToReplacement: "");
} else {
return character;
}
}).reduce( (c1, c2) -> "" + c1 + c2).orElse(null);
String character = "" + (char) c;
if (charactersToAllow != null && charactersToAllow.contains(character)) {
return character;
} else if (specialCharReplacements.containsKey(character)) {
return specialCharReplacements.get(character) + (appdendixToReplacement != null ? appdendixToReplacement : "");
} else {
return character;
}
}).reduce((c1, c2) -> "" + c1 + c2).orElse(null);
if (result != null) return result;
throw new RuntimeException("Word '" + name + "' could not be escaped.");
@@ -1683,13 +1683,13 @@ public class DefaultCodegen implements CodegenConfig {
}
private CodegenDiscriminator createDiscriminator(String schemaName, Schema schema, Map<String, Schema> allDefinitions) {
if(schema.getDiscriminator() == null) {
if (schema.getDiscriminator() == null) {
return null;
}
CodegenDiscriminator discriminator = new CodegenDiscriminator();
discriminator.setPropertyName(schema.getDiscriminator().getPropertyName());
discriminator.setMapping(schema.getDiscriminator().getMapping());
if(schema.getDiscriminator().getMapping() != null && !schema.getDiscriminator().getMapping().isEmpty()) {
if (schema.getDiscriminator().getMapping() != null && !schema.getDiscriminator().getMapping().isEmpty()) {
for (Entry<String, String> e : schema.getDiscriminator().getMapping().entrySet()) {
String name = ModelUtils.getSimpleRef(e.getValue());
discriminator.getMappedModels().add(new MappedModel(e.getKey(), name));
@@ -1698,9 +1698,9 @@ public class DefaultCodegen implements CodegenConfig {
allDefinitions.forEach((childName, child) -> {
if (child instanceof ComposedSchema && ((ComposedSchema) child).getAllOf() != null) {
Set<String> parentSchemas = ((ComposedSchema) child).getAllOf().stream()
.filter(s -> s.get$ref() != null)
.map(s -> ModelUtils.getSimpleRef(s.get$ref()))
.collect(Collectors.toSet());
.filter(s -> s.get$ref() != null)
.map(s -> ModelUtils.getSimpleRef(s.get$ref()))
.collect(Collectors.toSet());
if (parentSchemas.contains(schemaName)) {
discriminator.getMappedModels().add(new MappedModel(childName, childName));
}
@@ -1786,6 +1786,13 @@ public class DefaultCodegen implements CodegenConfig {
if (p.getReadOnly() != null) {
property.isReadOnly = p.getReadOnly();
}
if (p.getWriteOnly() != null) {
property.isWriteOnly = p.getWriteOnly();
}
if (p.getNullable() != null) {
property.isNullable = p.getNullable();
}
if (p.getXml() != null) {
if (p.getXml().getAttribute() != null) {
property.isXmlAttribute = p.getXml().getAttribute();
@@ -2506,7 +2513,7 @@ public class DefaultCodegen implements CodegenConfig {
/**
* Convert OAS Response object to Codegen Response object
*
* @param openAPI a OAS object representing the spec
* @param openAPI a OAS object representing the spec
* @param responseCode HTTP response code
* @param response OAS Response object
* @return Codegen Response object
@@ -3495,7 +3502,7 @@ public class DefaultCodegen implements CodegenConfig {
*/
public String apiDocFilename(String templateName, String tag) {
String docExtension = getDocExtension();
String suffix = docExtension != null ? docExtension: apiDocTemplateFiles().get(templateName);
String suffix = docExtension != null ? docExtension : apiDocTemplateFiles().get(templateName);
return apiDocFileFolder() + File.separator + toApiDocFilename(tag) + suffix;
}
@@ -4430,7 +4437,7 @@ public class DefaultCodegen implements CodegenConfig {
}
if (StringUtils.isEmpty(bodyParameterName)) {
if(StringUtils.isEmpty(mostInnerItem.complexType)) {
if (StringUtils.isEmpty(mostInnerItem.complexType)) {
codegenParameter.baseName = "request_body";
} else {
codegenParameter.baseName = mostInnerItem.complexType;
@@ -4602,7 +4609,7 @@ public class DefaultCodegen implements CodegenConfig {
return Collections.emptyList();
}
List<CodegenServer> codegenServers = new LinkedList<>();
for (Server server: servers) {
for (Server server : servers) {
CodegenServer cs = new CodegenServer();
cs.description = escapeText(server.getDescription());
cs.url = server.getUrl();
@@ -4618,7 +4625,7 @@ public class DefaultCodegen implements CodegenConfig {
return Collections.emptyList();
}
List<CodegenServerVariable> codegenServerVariables = new LinkedList<>();
for (Entry<String, ServerVariable> variableEntry: variables.entrySet()) {
for (Entry<String, ServerVariable> variableEntry : variables.entrySet()) {
CodegenServerVariable codegenServerVariable = new CodegenServerVariable();
ServerVariable variable = variableEntry.getValue();
codegenServerVariable.defaultValue = variable.getDefault();