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 03c285890254..612e7dc8f86d 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 @@ -711,7 +711,7 @@ public String toExampleValue(Schema schema) { private String toExampleValueRecursive(Schema schema, List included_schemas, int indentation) { String indentation_string = ""; - for (int i=0 ; i< indentation ; i++) indentation_string += " "; + for (int i = 0; i < indentation; i++) indentation_string += " "; String example = super.toExampleValue(schema); if (ModelUtils.isNullType(schema) && null != example) { @@ -720,9 +720,11 @@ private String toExampleValueRecursive(Schema schema, List included_sche return "None"; } // correct "true"s into "True"s, since super.toExampleValue uses "toString()" on Java booleans - if (ModelUtils.isBooleanSchema(schema) && null!=example) { - if ("false".equalsIgnoreCase(example)) example = "False"; - else example = "True"; + if (ModelUtils.isBooleanSchema(schema) && null != example) { + if ("false".equalsIgnoreCase(example)) + example = "False"; + else + example = "True"; } // correct "'"s into "'"s after toString() @@ -739,7 +741,7 @@ private String toExampleValueRecursive(Schema schema, List included_sche } if (schema.getEnum() != null && !schema.getEnum().isEmpty()) { - // Enum case: + // Enum case: example = schema.getEnum().get(0).toString(); if (ModelUtils.isStringSchema(schema)) { example = "'" + escapeText(example) + "'"; @@ -749,7 +751,7 @@ private String toExampleValueRecursive(Schema schema, List included_sche return example; } else if (null != schema.get$ref()) { - // $ref case: + // $ref case: Map allDefinitions = ModelUtils.getSchemas(this.openAPI); String ref = ModelUtils.getSimpleRef(schema.get$ref()); if (allDefinitions != null) { @@ -783,13 +785,22 @@ private String toExampleValueRecursive(Schema schema, List included_sche example = "YQ=="; } else if (ModelUtils.isStringSchema(schema)) { // a BigDecimal: - if ("Number".equalsIgnoreCase(schema.getFormat())) {return "1";} - if (StringUtils.isNotBlank(schema.getPattern())) return "'a'"; // I cheat here, since it would be too complicated to generate a string from a regexp + if ("Number".equalsIgnoreCase(schema.getFormat())) { + return "1"; + } + if (StringUtils.isNotBlank(schema.getPattern())) + return "'a'"; // I cheat here, since it would be too complicated to generate a string from a regexp + int len = 0; - if (null != schema.getMinLength()) len = schema.getMinLength().intValue(); - if (len < 1) len = 1; + + if (null != schema.getMinLength()) + len = schema.getMinLength().intValue(); + + if (len < 1) + len = 1; + example = ""; - for (int i=0;i included_sche included_schemas.add(schema.getTitle()); } ArraySchema arrayschema = (ArraySchema) schema; - example = "[\n" + indentation_string + toExampleValueRecursive(arrayschema.getItems(), included_schemas, indentation+1) + "\n" + indentation_string + "]"; + example = "[\n" + indentation_string + toExampleValueRecursive(arrayschema.getItems(), included_schemas, indentation + 1) + "\n" + indentation_string + "]"; } else if (ModelUtils.isMapSchema(schema)) { if (StringUtils.isNotBlank(schema.getTitle()) && !"null".equals(schema.getTitle())) { included_schemas.add(schema.getTitle()); @@ -822,7 +833,7 @@ private String toExampleValueRecursive(Schema schema, List included_sche the_key = "'" + escapeText(the_key) + "'"; } } - example = "{\n" + indentation_string + the_key + " : " + toExampleValueRecursive(additional, included_schemas, indentation+1) + "\n" + indentation_string + "}"; + example = "{\n" + indentation_string + the_key + " : " + toExampleValueRecursive(additional, included_schemas, indentation + 1) + "\n" + indentation_string + "}"; } else { example = "{ }"; } @@ -834,11 +845,11 @@ private String toExampleValueRecursive(Schema schema, List included_sche // I remove any property that is a discriminator, since it is not well supported by the python generator String toExclude = null; - if (schema.getDiscriminator()!=null) { + if (schema.getDiscriminator() != null) { toExclude = schema.getDiscriminator().getPropertyName(); } - example = packageName + ".models." + underscore(schema.getTitle())+"."+schema.getTitle()+"("; + example = packageName + ".models." + underscore(schema.getTitle()) + "." + schema.getTitle() + "("; // if required only: // List reqs = schema.getRequired(); @@ -852,7 +863,8 @@ private String toExampleValueRecursive(Schema schema, List included_sche Map properties = schema.getProperties(); Set propkeys = null; - if (properties != null) propkeys = properties.keySet(); + if (properties != null) + propkeys = properties.keySet(); if (toExclude != null && reqs.contains(toExclude)) { reqs.remove(toExclude); } @@ -879,7 +891,7 @@ private String toExampleValueRecursive(Schema schema, List included_sche } } } - example +=")"; + example += ")"; } else { LOGGER.warn("Type " + schema.getType() + " not handled properly in toExampleValue"); } @@ -908,44 +920,44 @@ public void setParameterExampleValue(CodegenParameter p) { } if (type != null) { - if ("String".equalsIgnoreCase(type) || "str".equalsIgnoreCase(type)) { - if (example == null) { - example = p.paramName + "_example"; - } - example = "'" + escapeText(example) + "'"; - } else if ("Integer".equals(type) || "int".equals(type)) { - if (example == null) { - example = "56"; - } - } else if ("Float".equalsIgnoreCase(type) || "Double".equalsIgnoreCase(type)) { - if (example == null) { - example = "3.4"; - } - } else if ("BOOLEAN".equalsIgnoreCase(type) || "bool".equalsIgnoreCase(type)) { - if (example == null) { - example = "True"; - } - } else if ("file".equalsIgnoreCase(type)) { - if (example == null) { - example = "/path/to/file"; - } - example = "'" + escapeText(example) + "'"; - } else if ("Date".equalsIgnoreCase(type)) { - if (example == null) { - example = "2013-10-20"; - } - example = "'" + escapeText(example) + "'"; - } else if ("DateTime".equalsIgnoreCase(type)) { - if (example == null) { - example = "2013-10-20T19:20:30+01:00"; + if ("String".equalsIgnoreCase(type) || "str".equalsIgnoreCase(type)) { + if (example == null) { + example = p.paramName + "_example"; + } + example = "'" + escapeText(example) + "'"; + } else if ("Integer".equals(type) || "int".equals(type)) { + if (example == null) { + example = "56"; + } + } else if ("Float".equalsIgnoreCase(type) || "Double".equalsIgnoreCase(type)) { + if (example == null) { + example = "3.4"; + } + } else if ("BOOLEAN".equalsIgnoreCase(type) || "bool".equalsIgnoreCase(type)) { + if (example == null) { + example = "True"; + } + } else if ("file".equalsIgnoreCase(type)) { + if (example == null) { + example = "/path/to/file"; + } + example = "'" + escapeText(example) + "'"; + } else if ("Date".equalsIgnoreCase(type)) { + if (example == null) { + example = "2013-10-20"; + } + example = "'" + escapeText(example) + "'"; + } else if ("DateTime".equalsIgnoreCase(type)) { + if (example == null) { + example = "2013-10-20T19:20:30+01:00"; + } + example = "'" + escapeText(example) + "'"; + } else if (!languageSpecificPrimitives.contains(type)) { + // type is a model class, e.g. User + example = this.packageName + "." + type + "()"; + } else { + LOGGER.warn("Type " + type + " not handled properly in setParameterExampleValue"); } - example = "'" + escapeText(example) + "'"; - } else if (!languageSpecificPrimitives.contains(type)) { - // type is a model class, e.g. User - example = this.packageName + "." + type + "()"; - } else { - LOGGER.warn("Type " + type + " not handled properly in setParameterExampleValue"); - } } if (example == null) { diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PythonClientExperimentalCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PythonClientExperimentalCodegen.java index 384960012385..8fff23a7af31 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PythonClientExperimentalCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PythonClientExperimentalCodegen.java @@ -17,30 +17,24 @@ package org.openapitools.codegen.languages; import io.swagger.v3.oas.models.Operation; -import io.swagger.v3.oas.models.examples.Example; import io.swagger.v3.oas.models.media.*; -import io.swagger.v3.oas.models.media.ArraySchema; -import io.swagger.v3.oas.models.media.MediaType; -import io.swagger.v3.oas.models.media.Schema; -import io.swagger.v3.oas.models.parameters.Parameter; import io.swagger.v3.oas.models.parameters.RequestBody; import io.swagger.v3.oas.models.responses.ApiResponse; import io.swagger.v3.oas.models.security.SecurityScheme; -import org.apache.commons.io.FilenameUtils; import org.apache.commons.lang3.StringUtils; import org.openapitools.codegen.*; import org.openapitools.codegen.examples.ExampleGenerator; +import org.openapitools.codegen.meta.GeneratorMetadata; +import org.openapitools.codegen.meta.Stability; import org.openapitools.codegen.meta.features.*; import org.openapitools.codegen.utils.ModelUtils; import org.openapitools.codegen.utils.ProcessUtils; -import org.openapitools.codegen.meta.GeneratorMetadata; -import org.openapitools.codegen.meta.Stability; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.io.File; import java.text.DateFormat; import java.text.SimpleDateFormat; -import java.io.File; import java.util.*; import java.util.regex.Pattern; @@ -83,7 +77,7 @@ public PythonClientExperimentalCodegen() { .excludeParameterFeatures( ParameterFeature.Cookie ) - ); + ); // this may set datatype right for additional properties instantiationTypes.put("map", "dict"); @@ -145,7 +139,7 @@ public void processOpts() { // Generate the 'signing.py' module, but only if the 'HTTP signature' security scheme is specified in the OAS. Map securitySchemeMap = openAPI != null ? - (openAPI.getComponents() != null ? openAPI.getComponents().getSecuritySchemes() : null) : null; + (openAPI.getComponents() != null ? openAPI.getComponents().getSecuritySchemes() : null) : null; List authMethods = fromSecurity(securitySchemeMap); if (ProcessUtils.hasHttpSignatureMethods(authMethods)) { supportingFiles.add(new SupportingFile("python-experimental/signing.mustache", packagePath(), "signing.py")); @@ -172,12 +166,12 @@ public void processOpts() { supportingFiles.add(new SupportingFile(readmeTemplate, "", readmePath)); if (!generateSourceCodeOnly) { - supportingFiles.remove(new SupportingFile("setup.mustache", "", "setup.py")); - supportingFiles.add(new SupportingFile("python-experimental/setup.mustache", "", "setup.py")); - supportingFiles.remove(new SupportingFile("requirements.mustache", "", "requirements.txt")); - supportingFiles.add(new SupportingFile("python-experimental/requirements.mustache", "", "requirements.txt")); - supportingFiles.remove(new SupportingFile("test-requirements.mustache", "", "test-requirements.txt")); - supportingFiles.add(new SupportingFile("python-experimental/test-requirements.mustache", "", "test-requirements.txt")); + supportingFiles.remove(new SupportingFile("setup.mustache", "", "setup.py")); + supportingFiles.add(new SupportingFile("python-experimental/setup.mustache", "", "setup.py")); + supportingFiles.remove(new SupportingFile("requirements.mustache", "", "requirements.txt")); + supportingFiles.add(new SupportingFile("python-experimental/requirements.mustache", "", "requirements.txt")); + supportingFiles.remove(new SupportingFile("test-requirements.mustache", "", "test-requirements.txt")); + supportingFiles.add(new SupportingFile("python-experimental/test-requirements.mustache", "", "test-requirements.txt")); } // default this to true so the python ModelSimple models will be generated @@ -305,7 +299,7 @@ public String toModelImport(String name) { // name looks like cat.Cat String moduleName = name.split("\\.")[0]; // https://exceptionshub.com/circular-or-cyclic-imports-in-python.html - return "from " + modelPackage() + " import "+ moduleName; + return "from " + modelPackage() + " import " + moduleName; } private String robustImport(String name) { @@ -313,8 +307,8 @@ private String robustImport(String name) { String moduleName = name.split("\\.")[0]; // https://exceptionshub.com/circular-or-cyclic-imports-in-python.html String modelImport = "try:\n from " + modelPackage() + - " import " + moduleName+ "\nexcept ImportError:\n " + - moduleName + " = sys.modules[\n '" + modelPackage() + "." + moduleName + "']"; + " import " + moduleName + "\nexcept ImportError:\n " + + moduleName + " = sys.modules[\n '" + modelPackage() + "." + moduleName + "']"; return modelImport; } @@ -346,9 +340,9 @@ private void fixOperationImports(Set imports) { @Override @SuppressWarnings("static-method") public Map postProcessOperationsWithModels(Map objs, List allModels) { - HashMap val = (HashMap)objs.get("operations"); + HashMap val = (HashMap) objs.get("operations"); ArrayList operations = (ArrayList) val.get("operation"); - ArrayList> imports = (ArrayList>)objs.get("imports"); + ArrayList> imports = (ArrayList>) objs.get("imports"); imports.clear(); for (CodegenOperation operation : operations) { fixOperationImports(operation.imports); @@ -377,7 +371,7 @@ private void fixModelImports(Set imports) { /** * Override with special post-processing for all models. - */ + */ @SuppressWarnings({"static-method", "unchecked"}) public Map postProcessAllModels(Map objs) { // loop through all models and delete ones where type!=object and the model has no validations and enums @@ -720,7 +714,7 @@ public void postProcessModelProperty(CodegenModel model, CodegenProperty p) { public void postProcessParameter(CodegenParameter p) { postProcessPattern(p.pattern, p.vendorExtensions); // set baseType to null so the api docs will not point to a model for languageSpecificPrimitives - if (p.baseType != null && languageSpecificPrimitives.contains(p.baseType)){ + if (p.baseType != null && languageSpecificPrimitives.contains(p.baseType)) { p.baseType = null; } else if (p.isListContainer && p.mostInnerItems.complexType != null && !languageSpecificPrimitives.contains(p.mostInnerItems.complexType)) { // fix ListContainers @@ -728,7 +722,7 @@ public void postProcessParameter(CodegenParameter p) { } } - private void addNullDefaultToOneOfAnyOfReqProps(Schema schema, CodegenModel result){ + private void addNullDefaultToOneOfAnyOfReqProps(Schema schema, CodegenModel result) { // for composed schema models, if the required properties are only from oneOf or anyOf models // give them a nulltype.Null so the user can omit including them in python ComposedSchema cs = (ComposedSchema) schema; @@ -750,7 +744,7 @@ private void addNullDefaultToOneOfAnyOfReqProps(Schema schema, CodegenModel resu if (anyOf != null) { oneOfanyOfSchemas.addAll(anyOf); } - for (Schema sc: oneOfanyOfSchemas) { + for (Schema sc : oneOfanyOfSchemas) { Schema refSchema = ModelUtils.getReferencedSchema(this.openAPI, sc); addProperties(otherProperties, otherRequired, refSchema); } @@ -768,7 +762,7 @@ private void addNullDefaultToOneOfAnyOfReqProps(Schema schema, CodegenModel resu List reqVars = result.getRequiredVars(); if (reqVars != null) { - for (CodegenProperty cp: reqVars) { + for (CodegenProperty cp : reqVars) { String propName = cp.baseName; if (otherRequiredSet.contains(propName) && !selfRequiredSet.contains(propName)) { // if var is in otherRequiredSet and is not in selfRequiredSet and is in result.requiredVars @@ -905,20 +899,20 @@ public String getSimpleTypeDeclaration(Schema schema) { * Primitive types in the OAS specification are implemented in Python using the corresponding * Python primitive types. * Composed types (e.g. allAll, oneOf, anyOf) are represented in Python using list of types. - * + *

* The caller should set the prefix and suffix arguments to empty string, except when * getTypeString invokes itself recursively. A non-empty prefix/suffix may be specified * to wrap the return value in a python dict, list or tuple. - * + *

* Examples: * - "bool, date, float" The data must be a bool, date or float. * - "[bool, date]" The data must be an array, and the array items must be a bool or date. - * - * @param p The OAS schema. - * @param prefix prepended to the returned value. - * @param suffix appended to the returned value. + * + * @param p The OAS schema. + * @param prefix prepended to the returned value. + * @param suffix appended to the returned value. * @param referencedModelNames a list of models that are being referenced while generating the types, - * may be used to generate imports. + * may be used to generate imports. * @return a comma-separated string representation of the Python types */ private String getTypeString(Schema p, String prefix, String suffix, List referencedModelNames) { @@ -968,7 +962,7 @@ private String getTypeString(Schema p, String prefix, String suffix, List