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 5c0df0ab8178..dffc0c62a192 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 @@ -722,7 +722,7 @@ private String toExampleValueRecursive(Schema schema, List included_sche } // correct "'"s into "'"s after toString() - if (ModelUtils.isStringSchema(schema) && schema.getDefault() != null) { + if (ModelUtils.isStringSchema(schema) && schema.getDefault() != null && !ModelUtils.isDateSchema(schema) && !ModelUtils.isDateTimeSchema(schema)) { example = (String) schema.getDefault(); } 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..6096b444ca32 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 @@ -38,8 +38,9 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.text.DateFormat; -import java.text.SimpleDateFormat; +import java.time.OffsetDateTime; +import java.time.ZoneOffset; +import java.time.format.DateTimeFormatter; import java.io.File; import java.util.*; import java.util.regex.Pattern; @@ -196,15 +197,15 @@ public String getName() { return "python-experimental"; } - public String dateToString(Schema p, Date date, DateFormat dateFormatter, DateFormat dateTimeFormatter) { + public String dateToString(Schema p, OffsetDateTime date, DateTimeFormatter dateFormatter, DateTimeFormatter dateTimeFormatter) { // converts a date into a date or date-time python string if (!(ModelUtils.isDateSchema(p) || ModelUtils.isDateTimeSchema(p))) { throw new RuntimeException("passed schema must be of type Date or DateTime"); } if (ModelUtils.isDateSchema(p)) { - return "dateutil_parser('" + dateFormatter.format(date) + "').date()"; + return "dateutil_parser('" + date.format(dateFormatter) + "').date()"; } - return "dateutil_parser('" + dateTimeFormatter.format(date) + "')"; + return "dateutil_parser('" + date.format(dateTimeFormatter) + "')"; } /** @@ -228,20 +229,17 @@ public String toDefaultValue(Schema p) { } // convert datetime and date enums if they exist - DateFormat iso8601Date = new SimpleDateFormat("yyyy-MM-dd", Locale.ROOT); - DateFormat iso8601DateTime = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX", Locale.ROOT); - TimeZone utc = TimeZone.getTimeZone("UTC"); - iso8601Date.setTimeZone(utc); - iso8601DateTime.setTimeZone(utc); + DateTimeFormatter iso8601Date = DateTimeFormatter.ISO_DATE; + DateTimeFormatter iso8601DateTime = DateTimeFormatter.ISO_DATE_TIME; if (ModelUtils.isDateSchema(p) || ModelUtils.isDateTimeSchema(p)) { List currentEnum = p.getEnum(); List fixedEnum = new ArrayList(); String fixedValue = null; - Date date = null; + OffsetDateTime date = null; if (currentEnum != null && !currentEnum.isEmpty()) { for (Object enumItem : currentEnum) { - date = (Date) enumItem; + date = (OffsetDateTime) enumItem; fixedValue = dateToString(p, date, iso8601Date, iso8601DateTime); fixedEnum.add(fixedValue); } @@ -251,15 +249,21 @@ public String toDefaultValue(Schema p) { // convert the example if it exists Object currentExample = p.getExample(); if (currentExample != null) { - date = (Date) currentExample; + try { + date = (OffsetDateTime) currentExample; + } catch (ClassCastException e) { + date = ((Date) currentExample).toInstant().atOffset(ZoneOffset.UTC); + LOGGER.warn("Invalid `date-time` format for value {}", currentExample); + } fixedValue = dateToString(p, date, iso8601Date, iso8601DateTime); fixedEnum.add(fixedValue); p.setExample(fixedValue); + LOGGER.warn(fixedValue); } // fix defaultObject if (defaultObject != null) { - date = (Date) defaultObject; + date = (OffsetDateTime) defaultObject; fixedValue = dateToString(p, date, iso8601Date, iso8601DateTime); p.setDefault(fixedValue); defaultObject = fixedValue; @@ -377,7 +381,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 @@ -905,7 +909,7 @@ 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. @@ -913,7 +917,7 @@ public String getSimpleTypeDeclaration(Schema schema) { * 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. @@ -922,7 +926,6 @@ public String getSimpleTypeDeclaration(Schema schema) { * @return a comma-separated string representation of the Python types */ private String getTypeString(Schema p, String prefix, String suffix, List referencedModelNames) { - // this is used to set dataType, which defines a python tuple of classes String fullSuffix = suffix; if (")".equals(suffix)) { fullSuffix = "," + suffix; @@ -968,7 +971,7 @@ private String getTypeString(Schema p, String prefix, String suffix, List