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 @@ -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;
Expand Down Expand Up @@ -437,6 +439,8 @@ public int hashCode()
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());
Expand Down Expand Up @@ -587,6 +591,12 @@ public boolean equals(Object obj) {
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;
}
Expand Down Expand Up @@ -769,6 +779,8 @@ public java.lang.String toString() {
", isMapContainer=" + isMapContainer +
", isEnum=" + isEnum +
", isReadOnly=" + isReadOnly +
", isWriteOnly=" + isWriteOnly+
", isNullable=" + isNullable +
", _enum=" + _enum +
", allowableValues=" + allowableValues +
", items=" + items +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ public void processOpts() {
.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()));
}
Expand Down Expand Up @@ -771,7 +771,7 @@ public String toOperationId(String operationId) {
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;
Expand All @@ -789,7 +789,7 @@ public String toParamName(String name) {
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;
Expand Down Expand Up @@ -832,24 +832,24 @@ public String escapeReservedWord(String name) {
/**
* 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>
* throws Runtime exception as word is not escaped properly.
*/
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.");
Expand Down Expand Up @@ -1683,13 +1683,13 @@ public CodegenModel fromModel(String name, Schema schema, Map<String, Schema> al
}

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));
Expand All @@ -1698,9 +1698,9 @@ private CodegenDiscriminator createDiscriminator(String schemaName, Schema schem
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));
}
Expand Down Expand Up @@ -1786,6 +1786,13 @@ public CodegenProperty fromProperty(String name, Schema p) {
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();
Expand Down Expand Up @@ -2506,7 +2513,7 @@ public boolean isParameterNameUnique(CodegenParameter p, List<CodegenParameter>
/**
* 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
Expand Down Expand Up @@ -3495,7 +3502,7 @@ public String apiFilename(String templateName, String tag) {
*/
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;
}

Expand Down Expand Up @@ -4430,7 +4437,7 @@ public CodegenParameter fromRequestBody(RequestBody body, Map<String, Schema> sc
}

if (StringUtils.isEmpty(bodyParameterName)) {
if(StringUtils.isEmpty(mostInnerItem.complexType)) {
if (StringUtils.isEmpty(mostInnerItem.complexType)) {
codegenParameter.baseName = "request_body";
} else {
codegenParameter.baseName = mostInnerItem.complexType;
Expand Down Expand Up @@ -4602,7 +4609,7 @@ public List<CodegenServer> fromServers(List<Server> servers) {
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();
Expand All @@ -4618,7 +4625,7 @@ public List<CodegenServerVariable> fromServerVariables(Map<String, ServerVariabl
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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import io.swagger.parser.OpenAPIParser;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.media.Schema;
import io.swagger.v3.parser.core.models.ParseOptions;

import org.apache.commons.io.FileUtils;
Expand Down Expand Up @@ -169,4 +170,61 @@ public void bodyParameterTest() {
Assert.assertEquals(bp.example, "OnlinePetstore::Pet.new");
}


@Test(description = "test nullable for properties")
public void nullableTest() {
final OpenAPI openAPI = new OpenAPIParser().readLocation("src/test/resources/3_0/petstore_oas3_test.yaml", null, new ParseOptions()).getOpenAPI();
final RubyClientCodegen codegen = new RubyClientCodegen();
codegen.setModuleName("OnlinePetstore");
final String path = "/pet";

final Schema schema = openAPI.getComponents().getSchemas().get("NullablePet");
CodegenModel nullablePet = codegen.fromModel("NullablePet", schema, openAPI.getComponents().getSchemas());
CodegenProperty cp0 = nullablePet.getVars().get(0);
Assert.assertTrue(cp0.isNullable);

CodegenProperty cp1 = nullablePet.getVars().get(1);
Assert.assertFalse(cp1.isNullable);

CodegenProperty cp2 = nullablePet.getVars().get(2);
Assert.assertTrue(cp2.isNullable);

CodegenProperty cp3 = nullablePet.getVars().get(3);
Assert.assertTrue(cp3.isNullable);

CodegenProperty cp4 = nullablePet.getVars().get(4);
Assert.assertFalse(cp4.isNullable);

CodegenProperty cp5 = nullablePet.getVars().get(5);
Assert.assertTrue(cp5.isNullable);
}

@Test(description = "test properties without nullable")
public void propertiesWithoutNullableTest() {
final OpenAPI openAPI = new OpenAPIParser().readLocation("src/test/resources/3_0/petstore_oas3_test.yaml", null, new ParseOptions()).getOpenAPI();
final RubyClientCodegen codegen = new RubyClientCodegen();
codegen.setModuleName("OnlinePetstore");
final String path = "/pet";

final Schema schema = openAPI.getComponents().getSchemas().get("Pet");
CodegenModel nullablePet = codegen.fromModel("Pet", schema, openAPI.getComponents().getSchemas());
CodegenProperty cp0 = nullablePet.getVars().get(0);
Assert.assertFalse(cp0.isNullable);

CodegenProperty cp1 = nullablePet.getVars().get(1);
Assert.assertFalse(cp1.isNullable);

CodegenProperty cp2 = nullablePet.getVars().get(2);
Assert.assertFalse(cp2.isNullable);

CodegenProperty cp3 = nullablePet.getVars().get(3);
Assert.assertFalse(cp3.isNullable);

CodegenProperty cp4 = nullablePet.getVars().get(4);
Assert.assertFalse(cp4.isNullable);

CodegenProperty cp5 = nullablePet.getVars().get(5);
Assert.assertFalse(cp5.isNullable);
}

}
Loading