From bcff006dc8d021a4fa0e8cf48b71649c87a9cb5b Mon Sep 17 00:00:00 2001 From: Jim Schubert Date: Wed, 22 Jan 2020 19:22:18 -0500 Subject: [PATCH] [python] Fix NPE in example generation (#5082) --- .../languages/PythonClientCodegen.java | 59 ++++++++++--------- 1 file changed, 31 insertions(+), 28 deletions(-) diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PythonClientCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PythonClientCodegen.java index f605ea8dbb..c621ebef02 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PythonClientCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PythonClientCodegen.java @@ -829,36 +829,39 @@ public class PythonClientCodegen extends DefaultCodegen implements CodegenConfig // List reqs = schema.getRequired(); // if required and optionals - List reqs = new ArrayList(); - for (Object toAdd : schema.getProperties().keySet()) - reqs.add((String)toAdd); - - Map properties = schema.getProperties(); - Set propkeys = null; - if (properties != null) propkeys = properties.keySet(); - if (toExclude != null && reqs.contains(toExclude)) { - reqs.remove(toExclude); - } - for (String toRemove : included_schemas) { - if (reqs.contains(toRemove)) { - reqs.remove(toRemove); + List reqs = new ArrayList<>(); + if (schema.getProperties() != null && !schema.getProperties().isEmpty()) { + for (Object toAdd : schema.getProperties().keySet()) { + reqs.add((String) toAdd); } - } - if (StringUtils.isNotBlank(schema.getTitle()) && !"null".equals(schema.getTitle())) { - included_schemas.add(schema.getTitle()); - } - if (null != schema.getRequired()) for (Object toAdd : schema.getRequired()) { - reqs.add((String)toAdd); - } - if (null!=propkeys) for (String propname : propkeys) { - Schema schema2 = properties.get(propname); - if (reqs.contains(propname)) { - String refTitle = schema2.getTitle(); - if (StringUtils.isBlank(refTitle) || "null".equals(refTitle)) { - schema2.setTitle(propname); + + Map properties = schema.getProperties(); + Set propkeys = null; + if (properties != null) propkeys = properties.keySet(); + if (toExclude != null && reqs.contains(toExclude)) { + reqs.remove(toExclude); + } + for (String toRemove : included_schemas) { + if (reqs.contains(toRemove)) { + reqs.remove(toRemove); + } + } + if (StringUtils.isNotBlank(schema.getTitle()) && !"null".equals(schema.getTitle())) { + included_schemas.add(schema.getTitle()); + } + if (null != schema.getRequired()) for (Object toAdd : schema.getRequired()) { + reqs.add((String) toAdd); + } + if (null != propkeys) for (String propname : propkeys) { + Schema schema2 = properties.get(propname); + if (reqs.contains(propname)) { + String refTitle = schema2.getTitle(); + if (StringUtils.isBlank(refTitle) || "null".equals(refTitle)) { + schema2.setTitle(propname); + } + example += "\n" + indentation_string + underscore(propname) + " = " + + toExampleValueRecursive(schema2, included_schemas, indentation + 1) + ", "; } - example += "\n" + indentation_string + underscore(propname) + " = "+ - toExampleValueRecursive(schema2, included_schemas, indentation+1)+", "; } } example +=")";