From ef561edd1682f1906b82a32210a10b174806d5bb Mon Sep 17 00:00:00 2001 From: Jonas Reichert Date: Sun, 25 Sep 2022 20:01:41 +0200 Subject: [PATCH] implement flag and adjust tests --- .../languages/Swift5ClientCodegen.java | 21 +++++++++- .../options/Swift5OptionsProvider.java | 1 + .../swift5/Swift5ClientCodegenTest.java | 35 +++++++++++++++- .../codegen/swift5/Swift5ModelTest.java | 41 +++++++++++++++++++ 4 files changed, 95 insertions(+), 3 deletions(-) diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/Swift5ClientCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/Swift5ClientCodegen.java index 363a52556f5f..de4fe8899e9e 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/Swift5ClientCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/Swift5ClientCodegen.java @@ -72,6 +72,7 @@ public class Swift5ClientCodegen extends DefaultCodegen implements CodegenConfig public static final String HASHABLE_MODELS = "hashableModels"; public static final String USE_JSON_ENCODABLE = "useJsonEncodable"; public static final String MAP_FILE_BINARY_TO_DATA = "mapFileBinaryToData"; + public static final String USE_CUSTOM_DATE_WITHOUT_TIME = "useCustomDateWithoutTime"; protected static final String LIBRARY_ALAMOFIRE = "alamofire"; protected static final String LIBRARY_URLSESSION = "urlsession"; protected static final String LIBRARY_VAPOR = "vapor"; @@ -96,6 +97,7 @@ public class Swift5ClientCodegen extends DefaultCodegen implements CodegenConfig protected boolean hashableModels = true; protected boolean useJsonEncodable = true; protected boolean mapFileBinaryToData = false; + protected boolean useCustomDateWithoutTime = false; protected String[] responseAs = new String[0]; protected String sourceFolder = swiftPackagePath; protected HashSet objcReservedWords; @@ -222,7 +224,6 @@ public class Swift5ClientCodegen extends DefaultCodegen implements CodegenConfig typeMapping.put("array", "Array"); typeMapping.put("map", "Dictionary"); typeMapping.put("set", "Set"); - typeMapping.put("date", "DateWithoutTime"); typeMapping.put("Date", "Date"); typeMapping.put("DateTime", "Date"); typeMapping.put("boolean", "Bool"); @@ -310,6 +311,10 @@ public class Swift5ClientCodegen extends DefaultCodegen implements CodegenConfig "[WARNING] This option will be removed and enabled by default in the future once we've enhanced the code to work with `Data` in all the different situations. Map File and Binary to Data (default: false)") .defaultValue(Boolean.FALSE.toString())); + cliOptions.add(new CliOption(USE_CUSTOM_DATE_WITHOUT_TIME, + "[WARNING] This option will be removed and enabled by default in the future. Uses a custom type to de- and encode dates without time information (default: false)") + .defaultValue(Boolean.FALSE.toString())); + supportedLibraries.put(LIBRARY_URLSESSION, "[DEFAULT] HTTP client: URLSession"); supportedLibraries.put(LIBRARY_ALAMOFIRE, "HTTP client: Alamofire"); supportedLibraries.put(LIBRARY_VAPOR, "HTTP client: Vapor"); @@ -518,6 +523,16 @@ public class Swift5ClientCodegen extends DefaultCodegen implements CodegenConfig typeMapping.put("binary", "Data"); } + if (additionalProperties.containsKey(USE_CUSTOM_DATE_WITHOUT_TIME)) { + setUseCustomDateWithoutTime(convertPropertyToBooleanAndWriteBack(USE_CUSTOM_DATE_WITHOUT_TIME)); + } + additionalProperties.put(USE_CUSTOM_DATE_WITHOUT_TIME, useCustomDateWithoutTime); + if (useCustomDateWithoutTime) { + typeMapping.put("date", "DateWithoutTime"); + } else { + typeMapping.put("date", "Date"); + } + if (additionalProperties.containsKey(USE_CLASSES)) { setUseClasses(convertPropertyToBooleanAndWriteBack(USE_CLASSES)); } @@ -619,6 +634,10 @@ public class Swift5ClientCodegen extends DefaultCodegen implements CodegenConfig this.mapFileBinaryToData = mapFileBinaryToData; } + public void setUseCustomDateWithoutTime(boolean useCustomDateWithoutTime) { + this.useCustomDateWithoutTime = useCustomDateWithoutTime; + } + @Override protected boolean isReservedWord(String word) { return word != null && reservedWords.contains(word); //don't lowercase as super does diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/options/Swift5OptionsProvider.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/options/Swift5OptionsProvider.java index b41f76f9a993..79c19e189a25 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/options/Swift5OptionsProvider.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/options/Swift5OptionsProvider.java @@ -101,6 +101,7 @@ public class Swift5OptionsProvider implements OptionsProvider { .put(Swift5ClientCodegen.HASHABLE_MODELS, HASHABLE_MODELS_VALUE) .put(Swift5ClientCodegen.USE_JSON_ENCODABLE, USE_JSON_ENCODABLE_VALUE) .put(Swift5ClientCodegen.MAP_FILE_BINARY_TO_DATA, "false") + .put(Swift5ClientCodegen.USE_CUSTOM_DATE_WITHOUT_TIME, "false") .put(Swift5ClientCodegen.USE_CLASSES, "false") .put(CodegenConstants.ENUM_UNKNOWN_DEFAULT_CASE, ENUM_UNKNOWN_DEFAULT_CASE_VALUE) .build(); diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/swift5/Swift5ClientCodegenTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/swift5/Swift5ClientCodegenTest.java index 6a3ebf766a24..1b590c74fdc2 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/swift5/Swift5ClientCodegenTest.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/swift5/Swift5ClientCodegenTest.java @@ -122,8 +122,8 @@ public class Swift5ClientCodegenTest { Assert.assertTrue(op.responses.get(0).isBinary); } - @Test(description = "returns Date when response format is date", enabled = true) - public void dateTest() { + @Test(description = "returns Date when response format is date per default", enabled = true) + public void dateDefaultTest() { final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/2_0/datePropertyTest.json"); final DefaultCodegen codegen = new Swift5ClientCodegen(); codegen.setOpenAPI(openAPI); @@ -131,6 +131,37 @@ public class Swift5ClientCodegenTest { final Operation p = openAPI.getPaths().get(path).getPost(); final CodegenOperation op = codegen.fromOperation(path, "post", p, null); + Assert.assertEquals(op.returnType, "Date"); + Assert.assertEquals(op.bodyParam.dataType, "Date"); + } + + @Test(description = "returns Date when response format is date and cli option is disabled", enabled = true) + public void dateDisabledCLITest() { + final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/2_0/datePropertyTest.json"); + final DefaultCodegen codegen = new Swift5ClientCodegen(); + codegen.setOpenAPI(openAPI); + codegen.additionalProperties().put(Swift5ClientCodegen.USE_CUSTOM_DATE_WITHOUT_TIME, false); + codegen.processOpts(); + final String path = "/tests/dateResponse"; + final Operation p = openAPI.getPaths().get(path).getPost(); + final CodegenOperation op = codegen.fromOperation(path, "post", p, null); + + Assert.assertEquals(op.returnType, "Date"); + Assert.assertEquals(op.bodyParam.dataType, "Date"); + } + + @Test(description = "returns DateWithoutTime when response format is date and cli option is enabled", enabled = true) + public void dateWithoutTimeTest() { + final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/2_0/datePropertyTest.json"); + final DefaultCodegen codegen = new Swift5ClientCodegen(); + codegen.setOpenAPI(openAPI); + codegen.additionalProperties().put(Swift5ClientCodegen.USE_CUSTOM_DATE_WITHOUT_TIME, true); + codegen.processOpts(); + + final String path = "/tests/dateResponse"; + final Operation p = openAPI.getPaths().get(path).getPost(); + final CodegenOperation op = codegen.fromOperation(path, "post", p, null); + Assert.assertEquals(op.returnType, "DateWithoutTime"); Assert.assertEquals(op.bodyParam.dataType, "DateWithoutTime"); } diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/swift5/Swift5ModelTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/swift5/Swift5ModelTest.java index 1f1415d68e4e..4fa8016e4161 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/swift5/Swift5ModelTest.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/swift5/Swift5ModelTest.java @@ -113,6 +113,47 @@ public class Swift5ModelTest { Assert.assertFalse(property6.isContainer); final CodegenProperty property7 = cm.vars.get(6); + Assert.assertEquals(property7.baseName, "dateOfBirth"); + Assert.assertEquals(property7.dataType, "Date"); + Assert.assertEquals(property7.name, "dateOfBirth"); + Assert.assertNull(property7.defaultValue); + Assert.assertEquals(property7.baseType, "Date"); + Assert.assertFalse(property7.required); + Assert.assertFalse(property7.isContainer); + } + + @Test(description = "convert a simple java model", enabled = true) + public void useCustomDateTimeTest() { + final Schema schema = new Schema() + .description("a sample model") + .addProperties("id", new IntegerSchema().format(SchemaTypeUtil.INTEGER64_FORMAT)) + .addProperties("name", new StringSchema()) + .addProperties("createdAt", new DateTimeSchema()) + .addProperties("binary", new BinarySchema()) + .addProperties("byte", new ByteArraySchema()) + .addProperties("uuid", new UUIDSchema()) + .addProperties("dateOfBirth", new DateSchema()) + .addRequiredItem("id") + .addRequiredItem("name") + .discriminator(new Discriminator().propertyName("test")); + final DefaultCodegen codegen = new Swift5ClientCodegen(); + OpenAPI openAPI = TestUtils.createOpenAPIWithOneSchema("sample", schema); + codegen.setOpenAPI(openAPI); + codegen.additionalProperties().put(Swift5ClientCodegen.USE_CUSTOM_DATE_WITHOUT_TIME, true); + codegen.processOpts(); + + final CodegenModel cm = codegen.fromModel("sample", schema); + final CodegenProperty property7 = cm.vars.get(6); + + final CodegenProperty property3 = cm.vars.get(2); + Assert.assertEquals(property3.baseName, "createdAt"); + Assert.assertEquals(property3.dataType, "Date"); + Assert.assertEquals(property3.name, "createdAt"); + Assert.assertNull(property3.defaultValue); + Assert.assertEquals(property3.baseType, "Date"); + Assert.assertFalse(property3.required); + Assert.assertFalse(property3.isContainer); + Assert.assertEquals(property7.baseName, "dateOfBirth"); Assert.assertEquals(property7.dataType, "DateWithoutTime"); Assert.assertEquals(property7.name, "dateOfBirth");