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 @@ -41,6 +41,7 @@ public class CodegenParameter {
public CodegenProperty mostInnerItems;
public Map<String, Object> vendorExtensions = new HashMap<String, Object>();
public boolean hasValidation;
public boolean isNullable;

/**
* Determines whether this parameter is mandatory. If the parameter is in "path",
Expand Down Expand Up @@ -150,6 +151,7 @@ public CodegenParameter copy() {
output.vendorExtensions = new HashMap<String, Object>(this.vendorExtensions);
}
output.hasValidation = this.hasValidation;
output.isNullable = this.isNullable;
output.isBinary = this.isBinary;
output.isByteArray = this.isByteArray;
output.isString = this.isString;
Expand Down Expand Up @@ -269,6 +271,8 @@ public boolean equals(Object o) {
return false;
if (hasValidation != that.hasValidation)
return false;
if (isNullable != that.isNullable)
return false;
if (required != that.required)
return false;
if (maximum != null ? !maximum.equals(that.maximum) : that.maximum != null)
Expand Down Expand Up @@ -344,6 +348,7 @@ public int hashCode() {
result = 31 * result + (mostInnerItems != null ? mostInnerItems.hashCode() : 0);
result = 31 * result + (vendorExtensions != null ? vendorExtensions.hashCode() : 0);
result = 31 * result + (hasValidation ? 13:31);
result = 31 * result + (isNullable ? 13:31);
result = 31 * result + (required ? 13:31);
result = 31 * result + (maximum != null ? maximum.hashCode() : 0);
result = 31 * result + (exclusiveMaximum ? 13:31);
Expand Down Expand Up @@ -409,6 +414,7 @@ public java.lang.String toString() {
", mostInnerItems=" + mostInnerItems +
", vendorExtensions=" + vendorExtensions +
", hasValidation=" + hasValidation +
", isNullable=" + isNullable +
", required=" + required +
", maximum='" + maximum + '\'' +
", exclusiveMaximum=" + exclusiveMaximum +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1789,7 +1789,11 @@ public CodegenProperty fromProperty(String name, Schema p) {
if (p.getWriteOnly() != null) {
property.isWriteOnly = p.getWriteOnly();
}
if (p.getNullable() != null) {

// use x-nullable
if (p.getExtensions() != null && p.getExtensions().get("x-nullable") != null) {
property.isNullable = Boolean.valueOf(p.getExtensions().get("x-nullable").toString());
} else if (p.getNullable() != null) { // use nullable defined in OAS3
property.isNullable = p.getNullable();
}

Expand Down Expand Up @@ -2649,6 +2653,14 @@ public CodegenParameter fromParameter(Parameter parameter, Set<String> imports)
LOGGER.warn("warning! Schema not found for parameter \"" + parameter.getName() + "\", using String");
parameterSchema = new StringSchema().description("//TODO automatically added by openapi-generator due to missing type definition.");
}

// x-nullable extension in OAS2
if (parameter.getExtensions() != null && parameter.getExtensions().get("x-nullable") != null) {
codegenParameter.isNullable = Boolean.valueOf(parameter.getExtensions().get("x-nullable").toString());
} else if (Boolean.TRUE.equals(parameterSchema.getNullable())) { // use nullable defined in the spec
codegenParameter.isNullable = true;
}

// set default value
if (parameterSchema.getDefault() != null) {
codegenParameter.defaultValue = toDefaultValue(parameterSchema);
Expand Down Expand Up @@ -4265,6 +4277,9 @@ public List<CodegenParameter> fromRequestBodyToFormParameters(RequestBody body,
// default to csv:
codegenParameter.collectionFormat = StringUtils.isEmpty(collectionFormat) ? "csv" : collectionFormat;

// set nullable
setParameterNullable(codegenParameter, codegenProperty);

// recursively add import
while (codegenProperty != null) {
imports.add(codegenProperty.baseType);
Expand Down Expand Up @@ -4293,7 +4308,7 @@ public List<CodegenParameter> fromRequestBodyToFormParameters(RequestBody body,
public CodegenParameter fromFormProperty(String name, Schema propertySchema, Set<String> imports) {
CodegenParameter codegenParameter = CodegenModelFactory.newInstance(CodegenModelType.PARAMETER);

LOGGER.debug("Debugging fromFormProperty: " + name);
LOGGER.debug("Debugging fromFormProperty {}: {}", name, propertySchema);
CodegenProperty codegenProperty = fromProperty(name, propertySchema);

codegenParameter.isFormParam = Boolean.TRUE;
Expand All @@ -4307,7 +4322,6 @@ public CodegenParameter fromFormProperty(String name, Schema propertySchema, Set
codegenParameter.jsonSchema = Json.pretty(propertySchema);
codegenParameter.defaultValue = codegenProperty.getDefaultValue();


if (codegenProperty.getVendorExtensions() != null && !codegenProperty.getVendorExtensions().isEmpty()) {
codegenParameter.vendorExtensions = codegenProperty.getVendorExtensions();
}
Expand Down Expand Up @@ -4366,6 +4380,8 @@ public CodegenParameter fromFormProperty(String name, Schema propertySchema, Set

setParameterBooleanFlagWithCodegenProperty(codegenParameter, codegenProperty);
setParameterExampleValue(codegenParameter);
// set nullable
setParameterNullable(codegenParameter, codegenProperty);

//TODO collectionFormat for form parameter not yet supported
//codegenParameter.collectionFormat = getCollectionFormat(propertySchema);
Expand Down Expand Up @@ -4415,6 +4431,9 @@ public CodegenParameter fromRequestBody(RequestBody body, Map<String, Schema> sc
codegenParameter.isMapContainer = Boolean.TRUE;

setParameterBooleanFlagWithCodegenProperty(codegenParameter, codegenProperty);

// set nullable
setParameterNullable(codegenParameter, codegenProperty);
} else if (ModelUtils.isArraySchema(schema)) {
final ArraySchema arraySchema = (ArraySchema) schema;
Schema inner = arraySchema.getItems();
Expand Down Expand Up @@ -4454,6 +4473,8 @@ public CodegenParameter fromRequestBody(RequestBody body, Map<String, Schema> sc
codegenParameter.isListContainer = Boolean.TRUE;

setParameterBooleanFlagWithCodegenProperty(codegenParameter, codegenProperty);
// set nullable
setParameterNullable(codegenParameter, codegenProperty);

while (codegenProperty != null) {
imports.add(codegenProperty.baseType);
Expand Down Expand Up @@ -4516,6 +4537,8 @@ public CodegenParameter fromRequestBody(RequestBody body, Map<String, Schema> sc
}
}
setParameterBooleanFlagWithCodegenProperty(codegenParameter, codegenProperty);
// set nullable
setParameterNullable(codegenParameter, codegenProperty);
}

} else {
Expand All @@ -4539,6 +4562,8 @@ public CodegenParameter fromRequestBody(RequestBody body, Map<String, Schema> sc

}
setParameterBooleanFlagWithCodegenProperty(codegenParameter, codegenProperty);
// set nullable
setParameterNullable(codegenParameter, codegenProperty);
}

// set the parameter's example value
Expand Down Expand Up @@ -4636,4 +4661,12 @@ public List<CodegenServerVariable> fromServerVariables(Map<String, ServerVariabl
}
return codegenServerVariables;
}

private void setParameterNullable(CodegenParameter parameter, CodegenProperty property) {
if (property.getVendorExtensions() != null && property.getVendorExtensions().get("x-nullable") != null) {
parameter.isNullable = Boolean.valueOf(property.getVendorExtensions().get("x-nullable").toString());
} else {
parameter.isNullable = property.isNullable;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ public void bodyParameterTest() {


@Test(description = "test nullable for properties")
public void nullableTest() {
public void nullablePropertyTest() {
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");
Expand Down Expand Up @@ -227,4 +227,49 @@ public void propertiesWithoutNullableTest() {
Assert.assertFalse(cp5.isNullable);
}

@Test(description = "test nullable for parameters (OAS3)")
public void nullableParameterOAS3Test() {
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/{petId}";

final Operation p = openAPI.getPaths().get(path).getPost();
final CodegenOperation op = codegen.fromOperation(path, "post", p, openAPI.getComponents().getSchemas());

Assert.assertEquals(op.pathParams.size(), 1);
CodegenParameter pp = op.pathParams.get(0);
Assert.assertTrue(pp.isNullable);

Assert.assertEquals(op.formParams.size(), 2);
CodegenParameter name = op.formParams.get(0);
Assert.assertFalse(name.isNullable);
CodegenParameter status = op.formParams.get(1);
Assert.assertTrue(status.isNullable);
}

@Test(description = "test nullable for parameters (OAS2)")
public void nullableParameterOAS2Test() {
final OpenAPI openAPI = new OpenAPIParser().readLocation("src/test/resources/2_0/petstore-nullable.yaml", null, new ParseOptions()).getOpenAPI();
final RubyClientCodegen codegen = new RubyClientCodegen();
codegen.setModuleName("OnlinePetstore");
final String path = "/pet/{petId}";

final Operation p = openAPI.getPaths().get(path).getPost();
final CodegenOperation op = codegen.fromOperation(path, "post", p, openAPI.getComponents().getSchemas());

// path parameter x-nullable test
Assert.assertEquals(op.pathParams.size(), 1);
CodegenParameter pp = op.pathParams.get(0);
Assert.assertTrue(pp.isNullable);

// form parameter x-nullable test
Assert.assertEquals(op.formParams.size(), 2);
CodegenParameter name = op.formParams.get(0);
Assert.assertFalse(name.isNullable);
CodegenParameter status = op.formParams.get(1);
// TODO comment out the following as there seems to be an issue with swagger parser not brining over the
// vendor extensions of the form parameter when creating the schema
//Assert.assertTrue(status.isNullable);
}
}
Loading