Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6286,7 +6286,7 @@ public CodegenParameter fromFormProperty(String name, Schema propertySchema, Set

codegenParameter.baseType = codegenProperty.baseType;
codegenParameter.dataType = codegenProperty.dataType;
codegenParameter.defaultValue = codegenProperty.getDefaultValue();
codegenParameter.defaultValue = toDefaultParameterValue(propertySchema);
codegenParameter.baseName = codegenProperty.baseName;
codegenParameter.paramName = toParamName(codegenParameter.baseName);
codegenParameter.dataFormat = codegenProperty.dataFormat;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -946,11 +946,21 @@ public String toDefaultValue(Schema schema) {
if (schema.getDefault() != null) {
String _default;
if (schema.getDefault() instanceof Date) {
Date date = (Date) schema.getDefault();
LocalDate localDate = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
return String.format(Locale.ROOT, localDate.toString(), "");
if ("java8".equals(getDateLibrary())) {
Date date = (Date) schema.getDefault();
LocalDate localDate = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
return String.format(Locale.ROOT, "LocalDate.parse(\"%s\")", localDate.toString());
} else {
return null;
}
} else if (schema.getDefault() instanceof java.time.OffsetDateTime) {
return "OffsetDateTime.parse(\"" + String.format(Locale.ROOT, ((java.time.OffsetDateTime) schema.getDefault()).atZoneSameInstant(ZoneId.systemDefault()).toString(), "") + "\", java.time.format.DateTimeFormatter.ISO_ZONED_DATE_TIME.withZone(java.time.ZoneId.systemDefault()))";
if ("java8".equals(getDateLibrary())) {
return String.format(Locale.ROOT, "OffsetDateTime.parse(\"%s\", %s)",
((java.time.OffsetDateTime) schema.getDefault()).atZoneSameInstant(ZoneId.systemDefault()),
"java.time.format.DateTimeFormatter.ISO_ZONED_DATE_TIME.withZone(java.time.ZoneId.systemDefault())");
} else {
return null;
}
} else {
_default = (String) schema.getDefault();
}
Expand Down Expand Up @@ -984,6 +994,11 @@ public String toDefaultParameterValue(final Schema<?> schema) {
if (defaultValue == null) {
return null;
}
if (defaultValue instanceof Date) {
Date date = (Date) schema.getDefault();
LocalDate localDate = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
return localDate.toString();
}
// escape quotes
return defaultValue.toString().replace("\"", "\\\"");
}
Expand Down Expand Up @@ -1504,27 +1519,6 @@ public CodegenOperation fromOperation(String path, String httpMethod, Operation
return op;
}

@Override
public void postProcessParameter(CodegenParameter p) {
// we use a custom version of this function to remove the l, d, and f suffixes from Long/Double/Float
// defaultValues
// remove the l because our users will use Long.parseLong(String defaultValue)
// remove the d because our users will use Double.parseDouble(String defaultValue)
// remove the f because our users will use Float.parseFloat(String defaultValue)
// NOTE: for CodegenParameters we DO need these suffixes because those defaultValues are used as java value
// literals assigned to Long/Double/Float
if (p.defaultValue == null) {
return;
}

Boolean fixLong = (p.isLong && "l".equals(p.defaultValue.substring(p.defaultValue.length()-1)));
Boolean fixDouble = (p.isDouble && "d".equals(p.defaultValue.substring(p.defaultValue.length()-1)));
Boolean fixFloat = (p.isFloat && "f".equals(p.defaultValue.substring(p.defaultValue.length()-1)));
if (fixLong || fixDouble || fixFloat) {
p.defaultValue = p.defaultValue.substring(0, p.defaultValue.length()-1);
}
}

private static CodegenModel reconcileInlineEnums(CodegenModel codegenModel, CodegenModel parentCodegenModel) {
// This generator uses inline classes to define enums, which breaks when
// dealing with models that have subTypes. To clean this up, we will analyze
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{{#isCookieParam}}{{#useBeanValidation}}{{>beanValidationQueryParams}}{{/useBeanValidation}}{{>paramDoc}} @CookieValue("{{baseName}}"){{>dateTimeParam}} {{>optionalDataType}} {{paramName}}{{/isCookieParam}}
{{#isCookieParam}}{{#useBeanValidation}}{{>beanValidationQueryParams}}{{/useBeanValidation}}{{>paramDoc}} @CookieValue(name="{{baseName}}"{{^isContainer}}{{#defaultValue}}, defaultValue = "{{{.}}}"{{/defaultValue}}{{/isContainer}}){{>dateTimeParam}} {{>optionalDataType}} {{paramName}}{{/isCookieParam}}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{{#isHeaderParam}}{{>paramDoc}} @RequestHeader(value = "{{baseName}}", required = {{#required}}true{{/required}}{{^required}}false{{/required}}){{>dateTimeParam}} {{>optionalDataType}} {{paramName}}{{/isHeaderParam}}
{{#isHeaderParam}}{{>paramDoc}} @RequestHeader(value = "{{baseName}}", required = {{#required}}true{{/required}}{{^required}}false{{/required}}{{^isContainer}}{{#defaultValue}}, defaultValue = "{{{.}}}"{{/defaultValue}}{{/isContainer}}){{>dateTimeParam}} {{>optionalDataType}} {{paramName}}{{/isHeaderParam}}
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,20 @@ public void testFormParameterHasDefaultValue() {
Assert.assertEquals(codegenParameter.getSchema(), null);
}

@Test
public void testDateTimeFormParameterHasDefaultValue() {
final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/spring/date-time-parameter-types-for-testing.yml");
final DefaultCodegen codegen = new DefaultCodegen();
codegen.setOpenAPI(openAPI);

Schema requestBodySchema = ModelUtils.getSchemaFromRequestBody(openAPI.getPaths().get("/thingy/{date}").getPost().getRequestBody());
CodegenParameter codegenParameter = codegen.fromFormProperty("visitDate", (Schema) requestBodySchema.getProperties().get("visitDate"),
new HashSet<>());

Assert.assertEquals(codegenParameter.defaultValue, "1971-12-19T03:39:57-08:00");
Assert.assertEquals(codegenParameter.getSchema(), null);
}

@Test
public void testOriginalOpenApiDocumentVersion() {
// Test with OAS 2.0 document.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,13 @@
import io.swagger.v3.oas.models.Components;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.media.*;
import java.time.OffsetDateTime;
import java.time.ZonedDateTime;
import java.util.HashSet;
import java.util.Set;
import org.openapitools.codegen.CodegenConstants;
import org.openapitools.codegen.CodegenModel;
import org.openapitools.codegen.CodegenParameter;
import org.openapitools.codegen.CodegenType;
import org.openapitools.codegen.TestUtils;
import org.openapitools.codegen.languages.AbstractJavaCodegen;
Expand Down Expand Up @@ -538,10 +543,37 @@ public void snapshotVersionAlreadySnapshotTest() {

Assert.assertEquals(codegen.getArtifactVersion(), "4.1.2-SNAPSHOT");
}
@Test
public void toDefaultValueDateTimeLegacyTest() {
final P_AbstractJavaCodegen codegen = new P_AbstractJavaCodegen();
codegen.setDateLibrary("legacy");
String defaultValue;

// Test default value for date format
DateSchema dateSchema = new DateSchema();
LocalDate defaultLocalDate = LocalDate.of(2019,2,15);
Date date = Date.from(defaultLocalDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
dateSchema.setDefault(date);
defaultValue = codegen.toDefaultValue(dateSchema);

// dateLibrary <> java8
Assert.assertNull(defaultValue);

DateTimeSchema dateTimeSchema = new DateTimeSchema();
OffsetDateTime defaultDateTime = OffsetDateTime.parse("1984-12-19T03:39:57-08:00");
ZonedDateTime expectedDateTime = defaultDateTime.atZoneSameInstant(ZoneId.systemDefault());
dateTimeSchema.setDefault(defaultDateTime);
defaultValue = codegen.toDefaultValue(dateTimeSchema);

// dateLibrary <> java8
Assert.assertNull(defaultValue);
}

@Test
public void toDefaultValueTest() {
final P_AbstractJavaCodegen codegen = new P_AbstractJavaCodegen();
codegen.setDateLibrary("java8");


Schema<?> schema = createObjectSchemaWithMinItems();
String defaultValue = codegen.toDefaultValue(schema);
Expand Down Expand Up @@ -579,7 +611,14 @@ public void toDefaultValueTest() {
Date date = Date.from(defaultLocalDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
dateSchema.setDefault(date);
defaultValue = codegen.toDefaultValue(dateSchema);
Assert.assertEquals(defaultLocalDate, LocalDate.parse(defaultValue));
Assert.assertEquals(defaultValue, "LocalDate.parse(\"" + defaultLocalDate.toString() + "\")");

DateTimeSchema dateTimeSchema = new DateTimeSchema();
OffsetDateTime defaultDateTime = OffsetDateTime.parse("1984-12-19T03:39:57-08:00");
ZonedDateTime expectedDateTime = defaultDateTime.atZoneSameInstant(ZoneId.systemDefault());
dateTimeSchema.setDefault(defaultDateTime);
defaultValue = codegen.toDefaultValue(dateTimeSchema);
Assert.assertTrue(defaultValue.startsWith("OffsetDateTime.parse(\"" + expectedDateTime.toString()));

// Test default value for number without format
NumberSchema numberSchema = new NumberSchema();
Expand All @@ -594,6 +633,44 @@ public void toDefaultValueTest() {
Assert.assertEquals(defaultValue, doubleValue + "d");
}

@Test
public void dateDefaultValueIsIsoDate() {
final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/spring/date-time-parameter-types-for-testing.yml");
final P_AbstractJavaCodegen codegen = new P_AbstractJavaCodegen();
codegen.setOpenAPI(openAPI);

Set<String> imports = new HashSet<>();
CodegenParameter parameter = codegen.fromParameter(openAPI.getPaths().get("/thingy/{date}").getGet().getParameters().get(2), imports);

Assert.assertEquals(parameter.dataType, "Date");
Assert.assertEquals(parameter.isDate, true);
Assert.assertEquals(parameter.defaultValue, "1974-01-01");
Assert.assertEquals(imports.size(), 1);
Assert.assertEquals(imports.iterator().next(), "Date");

Assert.assertNotNull(parameter.getSchema());
Assert.assertEquals(parameter.getSchema().baseType, "Date");
}

@Test
public void dateDefaultValueIsIsoDateTime() {
final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/spring/date-time-parameter-types-for-testing.yml");
final P_AbstractJavaCodegen codegen = new P_AbstractJavaCodegen();
codegen.setOpenAPI(openAPI);

Set<String> imports = new HashSet<>();
CodegenParameter parameter = codegen.fromParameter(openAPI.getPaths().get("/thingy/{date}").getGet().getParameters().get(1), imports);

Assert.assertEquals(parameter.dataType, "Date");
Assert.assertEquals(parameter.isDateTime, true);
Assert.assertEquals(parameter.defaultValue, "1973-12-19T03:39:57-08:00");
Assert.assertEquals(imports.size(), 1);
Assert.assertEquals(imports.iterator().next(), "Date");

Assert.assertNotNull(parameter.getSchema());
Assert.assertEquals(parameter.getSchema().baseType, "Date");
}

@Test
public void getTypeDeclarationGivenImportMappingTest() {
final P_AbstractJavaCodegen codegen = new P_AbstractJavaCodegen();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ paths:
schema:
type: string
format: date
default: '1970-01-01'
example: '2021-01-01'
responses:
'405':
Expand All @@ -29,6 +30,7 @@ paths:
visitDate:
description: Updated last vist timestamp
type: string
default: '1971-12-19T03:39:57-08:00'
format: date-time
get:
operationId: get
Expand All @@ -40,6 +42,7 @@ paths:
schema:
type: string
format: date
default: '1972-01-01'
example: '2021-01-01'
- name: dateTime
description: 'A date-time query parameter'
Expand All @@ -48,6 +51,7 @@ paths:
schema:
type: string
format: date-time
default: '1973-12-19T03:39:57-08:00'
example: '1996-12-19T16:39:57-08:00'
- name: X-Order-Date
in: header
Expand All @@ -56,6 +60,7 @@ paths:
schema:
type: string
format: date
default: '1974-01-01'
example: '2021-01-01'
- name: loginDate
in: cookie
Expand All @@ -64,7 +69,36 @@ paths:
schema:
type: string
format: date
default: '1975-01-01'
example: '2021-01-01'
responses:
'200':
description: OK
description: OK

components:
schemas:
'Pet':
type: object
required:
- '@type'
properties:
'@type':
type: string
default: 'Pet'
age:
type: integer
default: 4
happy:
type: boolean
default: true
price:
type: number
default: 32000000000
lastFeed:
type: string
format: date-time
default: '1973-12-19T03:39:57-08:00'
dateOfBirth:
type: string
format: date
default: '2021-01-01'
4 changes: 2 additions & 2 deletions samples/client/petstore/java/okhttp-gson/docs/FakeApi.md
Original file line number Diff line number Diff line change
Expand Up @@ -605,7 +605,7 @@ public class Example {
String string = "string_example"; // String | None
File binary = new File("/path/to/file"); // File | None
LocalDate date = LocalDate.now(); // LocalDate | None
OffsetDateTime dateTime = OffsetDateTime.parse("OffsetDateTime.parse("2010-02-01T09:20:10.111110Z[UTC]", java.time.format.DateTimeFormatter.ISO_ZONED_DATE_TIME.withZone(java.time.ZoneId.systemDefault()))"); // OffsetDateTime | None
OffsetDateTime dateTime = OffsetDateTime.parse("2010-02-01T10:20:10.111110+01:00"); // OffsetDateTime | None
String password = "password_example"; // String | None
String paramCallback = "paramCallback_example"; // String | None
try {
Expand Down Expand Up @@ -636,7 +636,7 @@ Name | Type | Description | Notes
**string** | **String**| None | [optional]
**binary** | **File**| None | [optional]
**date** | **LocalDate**| None | [optional]
**dateTime** | **OffsetDateTime**| None | [optional] [default to OffsetDateTime.parse(&quot;2010-02-01T09:20:10.111110Z[UTC]&quot;, java.time.format.DateTimeFormatter.ISO_ZONED_DATE_TIME.withZone(java.time.ZoneId.systemDefault()))]
**dateTime** | **OffsetDateTime**| None | [optional] [default to 2010-02-01T10:20:10.111110+01:00]
**password** | **String**| None | [optional]
**paramCallback** | **String**| None | [optional]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1238,7 +1238,7 @@ public okhttp3.Call testClientModelAsync(Client client, final ApiCallback<Client
* @param string None (optional)
* @param binary None (optional)
* @param date None (optional)
* @param dateTime None (optional, default to OffsetDateTime.parse(&quot;2010-02-01T09:20:10.111110Z[UTC]&quot;, java.time.format.DateTimeFormatter.ISO_ZONED_DATE_TIME.withZone(java.time.ZoneId.systemDefault())))
* @param dateTime None (optional, default to 2010-02-01T10:20:10.111110+01:00)
* @param password None (optional)
* @param paramCallback None (optional)
* @param _callback Callback for upload/download progress
Expand Down Expand Up @@ -1395,7 +1395,7 @@ private okhttp3.Call testEndpointParametersValidateBeforeCall(BigDecimal number,
* @param string None (optional)
* @param binary None (optional)
* @param date None (optional)
* @param dateTime None (optional, default to OffsetDateTime.parse(&quot;2010-02-01T09:20:10.111110Z[UTC]&quot;, java.time.format.DateTimeFormatter.ISO_ZONED_DATE_TIME.withZone(java.time.ZoneId.systemDefault())))
* @param dateTime None (optional, default to 2010-02-01T10:20:10.111110+01:00)
* @param password None (optional)
* @param paramCallback None (optional)
* @throws ApiException If fail to call the API, e.g. server error or cannot deserialize the response body
Expand Down Expand Up @@ -1424,7 +1424,7 @@ public void testEndpointParameters(BigDecimal number, Double _double, String pat
* @param string None (optional)
* @param binary None (optional)
* @param date None (optional)
* @param dateTime None (optional, default to OffsetDateTime.parse(&quot;2010-02-01T09:20:10.111110Z[UTC]&quot;, java.time.format.DateTimeFormatter.ISO_ZONED_DATE_TIME.withZone(java.time.ZoneId.systemDefault())))
* @param dateTime None (optional, default to 2010-02-01T10:20:10.111110+01:00)
* @param password None (optional)
* @param paramCallback None (optional)
* @return ApiResponse&lt;Void&gt;
Expand Down Expand Up @@ -1455,7 +1455,7 @@ public ApiResponse<Void> testEndpointParametersWithHttpInfo(BigDecimal number, D
* @param string None (optional)
* @param binary None (optional)
* @param date None (optional)
* @param dateTime None (optional, default to OffsetDateTime.parse(&quot;2010-02-01T09:20:10.111110Z[UTC]&quot;, java.time.format.DateTimeFormatter.ISO_ZONED_DATE_TIME.withZone(java.time.ZoneId.systemDefault())))
* @param dateTime None (optional, default to 2010-02-01T10:20:10.111110+01:00)
* @param password None (optional)
* @param paramCallback None (optional)
* @param _callback The callback to be executed when the API call finishes
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
README.md
pom.xml
src/main/java/org/openapitools/api/DefaultApi.java
src/main/java/org/openapitools/model/Pet.java
Loading