Skip to content

Commit

Permalink
Add nullable property support
Browse files Browse the repository at this point in the history
Requires: #8809

When I wrote this, I missed the changes on CodegenParameter and I had
some unnecessary changes on CodegenModel. I realized my mistake when I
went to add a similar change to openapi-generator and saw it was already
there, and they knew more about what to do than I did.

So, this is based in part on work by wing328 and jmini
- OpenAPITools/openapi-generator#873
- OpenAPITools/openapi-generator#889
- OpenAPITools/openapi-generator#930
  • Loading branch information
cognifloyd committed Dec 19, 2018
1 parent 6425283 commit 61275df
Showing 1 changed file with 16 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1455,6 +1455,9 @@ public CodegenProperty fromProperty(String name, Schema propertySchema) {
codegenProperty.defaultValue = toDefaultValue(propertySchema);
codegenProperty.defaultValueWithParam = toDefaultValueWithParam(name, propertySchema);
codegenProperty.jsonSchema = Json.pretty(propertySchema);
if (propertySchema.getNullable() != null) {
codegenProperty.nullable = propertySchema.getNullable();
}
if (propertySchema.getReadOnly() != null) {
codegenProperty.getVendorExtensions().put(CodegenConstants.IS_READ_ONLY_EXT_NAME, propertySchema.getReadOnly());
}
Expand Down Expand Up @@ -2270,6 +2273,9 @@ public CodegenParameter fromParameter(Parameter parameter, Set<String> imports)
if (parameter.getRequired() != null) {
codegenParameter.required = parameter.getRequired();
}
if (parameter.getRequired() != null) {
codegenParameter.required = parameter.getRequired();
}
codegenParameter.jsonSchema = Json.pretty(parameter);

if (System.getProperty("debugParser") != null) {
Expand Down Expand Up @@ -2326,10 +2332,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 swagger-codegen.");
}
if (Boolean.TRUE.equals(parameterSchema.getNullable())) {
codegenParameter.nullable = true;
}
CodegenProperty codegenProperty = fromProperty(parameter.getName(), parameterSchema);

// set boolean flag (e.g. isString)
setParameterBooleanFlagWithCodegenProperty(codegenParameter, codegenProperty);
setParameterNullable(codegenParameter, codegenProperty);

codegenParameter.dataType = codegenProperty.datatype;
codegenParameter.dataFormat = codegenProperty.dataFormat;
Expand Down Expand Up @@ -2501,6 +2511,7 @@ public CodegenParameter fromRequestBody(RequestBody body, String name, Schema sc
}
}
setParameterBooleanFlagWithCodegenProperty(codegenParameter, codegenProperty);
setParameterNullable(codegenParameter, codegenProperty);
}
}
else if (schema instanceof ArraySchema) {
Expand Down Expand Up @@ -2534,6 +2545,7 @@ else if (schema instanceof ArraySchema) {
codegenParameter.getVendorExtensions().put(CodegenConstants.IS_LIST_CONTAINER_EXT_NAME, Boolean.TRUE);

setParameterBooleanFlagWithCodegenProperty(codegenParameter, codegenProperty);
setParameterNullable(codegenParameter, codegenProperty);

while (codegenProperty != null) {
imports.add(codegenProperty.baseType);
Expand Down Expand Up @@ -3994,4 +4006,8 @@ private void addParemeters(CodegenContent codegenContent, List<CodegenParameter>
codegenContent.getParameters().add(codegenParameter.copy());
}
}

protected void setParameterNullable(CodegenParameter parameter, CodegenProperty property) {
parameter.nullable = property.nullable;
}
}

0 comments on commit 61275df

Please sign in to comment.