From 61275dfe865964b8c182d7e3a2cd110fc07e6fbf Mon Sep 17 00:00:00 2001 From: Jacob Floyd Date: Fri, 12 Oct 2018 20:00:15 -0500 Subject: [PATCH] Add nullable property support Requires: https://github.com/swagger-api/swagger-codegen/pull/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 --- .../v3/generators/DefaultCodegenConfig.java | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/main/java/io/swagger/codegen/v3/generators/DefaultCodegenConfig.java b/src/main/java/io/swagger/codegen/v3/generators/DefaultCodegenConfig.java index ae9ab0d0f08..f7f14f545a8 100644 --- a/src/main/java/io/swagger/codegen/v3/generators/DefaultCodegenConfig.java +++ b/src/main/java/io/swagger/codegen/v3/generators/DefaultCodegenConfig.java @@ -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()); } @@ -2270,6 +2273,9 @@ public CodegenParameter fromParameter(Parameter parameter, Set 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) { @@ -2326,10 +2332,14 @@ public CodegenParameter fromParameter(Parameter parameter, Set 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; @@ -2501,6 +2511,7 @@ public CodegenParameter fromRequestBody(RequestBody body, String name, Schema sc } } setParameterBooleanFlagWithCodegenProperty(codegenParameter, codegenProperty); + setParameterNullable(codegenParameter, codegenProperty); } } else if (schema instanceof ArraySchema) { @@ -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); @@ -3994,4 +4006,8 @@ private void addParemeters(CodegenContent codegenContent, List codegenContent.getParameters().add(codegenParameter.copy()); } } + + protected void setParameterNullable(CodegenParameter parameter, CodegenProperty property) { + parameter.nullable = property.nullable; + } }