From a6ce167f34aa027139d170cfa68f1d0e5fd90265 Mon Sep 17 00:00:00 2001 From: Ivan Smolin Date: Fri, 4 Mar 2022 21:18:21 +0300 Subject: [PATCH] simplify & remove unused stuff --- .../touchin/codegen/TINetworkingCodegen.java | 99 +++---------------- ...java => TINetworkingCodegenConstants.java} | 2 +- .../handlebars/TINetworking/api.mustache | 2 +- 3 files changed, 16 insertions(+), 87 deletions(-) rename src/main/java/ru/touchin/codegen/{TINetowrkingCodegenConstants.java => TINetworkingCodegenConstants.java} (85%) diff --git a/src/main/java/ru/touchin/codegen/TINetworkingCodegen.java b/src/main/java/ru/touchin/codegen/TINetworkingCodegen.java index 6a0021d..0f2c4c1 100644 --- a/src/main/java/ru/touchin/codegen/TINetworkingCodegen.java +++ b/src/main/java/ru/touchin/codegen/TINetworkingCodegen.java @@ -19,16 +19,12 @@ import java.util.*; import java.util.regex.Matcher; import java.util.regex.Pattern; -import static io.swagger.codegen.v3.generators.handlebars.ExtensionHelper.getBooleanValue; - public class TINetworkingCodegen extends DefaultCodegenConfig { protected static final Logger LOGGER = LoggerFactory.getLogger(TINetworkingCodegen.class); public static final String PROJECT_NAME = "projectName"; public static final String RESPONSE_AS = "responseAs"; - public static final String UNWRAP_REQUIRED = "unwrapRequired"; protected String projectName = "SwaggerAPI"; - private boolean unwrapRequired; private String[] responseAs = new String[0]; protected String sourceFolder = "Classes" + File.separator + "Swaggers"; @@ -174,10 +170,6 @@ public class TINetworkingCodegen extends DefaultCodegenConfig { importMapping = new HashMap<>(); cliOptions.add(new CliOption(PROJECT_NAME, "Project name in Xcode")); - cliOptions.add(new CliOption(UNWRAP_REQUIRED, - "Treat 'required' properties in response as non-optional " - + "(which would crash the app if api returns null as opposed " - + "to required option specified in json schema")); cliOptions.add(new CliOption(CodegenConstants.HIDE_GENERATION_TIMESTAMP, CodegenConstants.HIDE_GENERATION_TIMESTAMP_DESC) .defaultValue(Boolean.TRUE.toString())); @@ -203,13 +195,6 @@ public class TINetworkingCodegen extends DefaultCodegenConfig { } sourceFolder = projectName + File.separator + sourceFolder; - // Setup unwrapRequired option, which makes all the - // properties with "required" non-optional - if (additionalProperties.containsKey(UNWRAP_REQUIRED)) { - setUnwrapRequired(convertPropertyToBooleanAndWriteBack(UNWRAP_REQUIRED)); - } - additionalProperties.put(UNWRAP_REQUIRED, unwrapRequired); - // Setup unwrapRequired option, which makes all the properties with "required" non-optional if (additionalProperties.containsKey(RESPONSE_AS)) { Object responseAsObject = additionalProperties.get(RESPONSE_AS); @@ -489,51 +474,9 @@ public class TINetworkingCodegen extends DefaultCodegenConfig { } } - for (CodegenProperty property : codegenModel.allVars) { - Map vendorExtensions = property.getVendorExtensions(); - - boolean containsCustomDateFormat = vendorExtensions.containsKey(TINetowrkingCodegenConstants.DATE_FORMAT); - - if (property.getIsDate() - || property.getIsDateTime() - && !containsCustomDateFormat) { - vendorExtensions.put(TINetowrkingCodegenConstants.IS_ISO8601_DATE, true); - } else if (containsCustomDateFormat) { - String customDateFormat = (String) vendorExtensions.get(TINetowrkingCodegenConstants.DATE_FORMAT); - String dateFormatName = customDateFormat.replace(".", "_") - .replaceAll("[^A-Za-z0-9_]", ""); - vendorExtensions.put(TINetowrkingCodegenConstants.DATE_FORMAT_NAME, dateFormatName); - } - } - return codegenModel; } - @Override - public Map postProcessAllModels(Map processedModels) { - for (Object processedModel : processedModels.values()) { - Map modelMap = (Map) processedModel; - List> modelsMap = (List>) modelMap.get("models"); - for (Map modelModelsMap : modelsMap) { - CodegenModel codegenModel = (CodegenModel) modelModelsMap.get("model"); - for (CodegenProperty modelProperty : codegenModel.getAllVars()) { - String customDateFormat = (String) modelProperty.getVendorExtensions() - .get(TINetowrkingCodegenConstants.DATE_FORMAT); - - if (customDateFormat != null) { - String dateFormatName = (String) modelProperty.getVendorExtensions() - .get(TINetowrkingCodegenConstants.DATE_FORMAT_NAME); - - allCustomDateFormats.put(dateFormatName, customDateFormat); - } - } - } - - } - - return super.postProcessAllModels(processedModels); - } - @Override public Map postProcessSupportingFileData(Map objs) { Map supportingFileData = super.postProcessSupportingFileData(objs); @@ -576,10 +519,6 @@ public class TINetworkingCodegen extends DefaultCodegenConfig { this.projectName = projectName; } - public void setUnwrapRequired(boolean unwrapRequired) { - this.unwrapRequired = unwrapRequired; - } - public void setResponseAs(String[] responseAs) { this.responseAs = responseAs; } @@ -710,31 +649,21 @@ public class TINetworkingCodegen extends DefaultCodegenConfig { public void postProcessModelProperty(CodegenModel model, CodegenProperty property) { super.postProcessModelProperty(model, property); - // The default template code has the following logic for - // assigning a type as Swift Optional: - // - // {{^unwrapRequired}}?{{/unwrapRequired}} - // {{#unwrapRequired}}{{^required}}?{{/required}}{{/unwrapRequired}} - // - // which means: - // - // boolean isSwiftOptional = !unwrapRequired || (unwrapRequired && !property.required); - // - // We can drop the check for unwrapRequired in (unwrapRequired && !property.required) - // due to short-circuit evaluation of the || operator. - boolean isSwiftOptional = !unwrapRequired || !property.required; - boolean isSwiftScalarType = getBooleanValue(property, CodegenConstants.IS_INTEGER_EXT_NAME) - || getBooleanValue(property, CodegenConstants.IS_LONG_EXT_NAME) - || getBooleanValue(property, CodegenConstants.IS_FLOAT_EXT_NAME) - || getBooleanValue(property, CodegenConstants.IS_DOUBLE_EXT_NAME) - || getBooleanValue(property, CodegenConstants.IS_BOOLEAN_EXT_NAME); + Map vendorExtensions = property.getVendorExtensions(); - if (isSwiftOptional && isSwiftScalarType) { - // Optional scalar types like Int?, Int64?, Float?, Double?, and Bool? - // do not translate to Objective-C. So we want to flag those - // properties in case we want to put special code in the templates - // which provide Objective-C compatibility. - property.vendorExtensions.put("x-swift-optional-scalar", true); + boolean containsCustomDateFormat = vendorExtensions.containsKey(TINetworkingCodegenConstants.DATE_FORMAT); + + if (property.getIsDate() + || property.getIsDateTime() + && !containsCustomDateFormat) { + vendorExtensions.put(TINetworkingCodegenConstants.IS_ISO8601_DATE, true); + } else if (containsCustomDateFormat) { + String customDateFormat = (String) vendorExtensions.get(TINetworkingCodegenConstants.DATE_FORMAT); + String dateFormatName = customDateFormat.replace(".", "_") + .replaceAll("[^A-Za-z0-9_]", ""); + vendorExtensions.put(TINetworkingCodegenConstants.DATE_FORMAT_NAME, dateFormatName); + + allCustomDateFormats.put(dateFormatName, customDateFormat); } } diff --git a/src/main/java/ru/touchin/codegen/TINetowrkingCodegenConstants.java b/src/main/java/ru/touchin/codegen/TINetworkingCodegenConstants.java similarity index 85% rename from src/main/java/ru/touchin/codegen/TINetowrkingCodegenConstants.java rename to src/main/java/ru/touchin/codegen/TINetworkingCodegenConstants.java index aa253a5..55b8c1e 100644 --- a/src/main/java/ru/touchin/codegen/TINetowrkingCodegenConstants.java +++ b/src/main/java/ru/touchin/codegen/TINetworkingCodegenConstants.java @@ -1,6 +1,6 @@ package ru.touchin.codegen; -public class TINetowrkingCodegenConstants { +public class TINetworkingCodegenConstants { public static final String DATE_FORMAT = "x-custom-date-format"; public static final String DATE_FORMAT_NAME = "x-codegen-date-format-name"; public static final String IS_ISO8601_DATE = "x-codegen-is-iso8601-date"; diff --git a/src/main/resources/handlebars/TINetworking/api.mustache b/src/main/resources/handlebars/TINetworking/api.mustache index 1797925..f91c3a5 100644 --- a/src/main/resources/handlebars/TINetworking/api.mustache +++ b/src/main/resources/handlebars/TINetworking/api.mustache @@ -36,7 +36,7 @@ public extension EndpointRequest { - parameter {{paramName}}: {{description}} {{^required}}(optional{{#defaultValue}}, default to {{{.}}}{{/defaultValue}}){{/required}} {{/parameters}} */ - static func {{operationId}}({{#parameters}}{{paramName}}: {{#isEnum}}{{#isContainer}}{{{dataType}}}{{/isContainer}}{{^isContainer}}{{{datatypeWithEnum}}}_{{operationId}}{{/isContainer}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{/isEnum}}{{^required}}? = nil{{/required}}{{#hasMore}}, {{/hasMore}}{{/parameters}}{{#hasParams}}, {{/hasParams}}server: Server = .default) -> EndpointRequest<{{#parameters}}{{#isBodyParam}}{{{dataType}}}{{/isBodyParam}}{{/parameters}}{{^hasBodyParam}}EmptyBody{{/hasBodyParam}}, {{{returnType}}}> { + static func {{operationId}}({{#parameters}}{{paramName}}: {{#isEnum}}{{#isContainer}}{{{dataType}}}{{/isContainer}}{{^isContainer}}{{{datatypeWithEnum}}}_{{operationId}}{{/isContainer}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{/isEnum}}{{^required}}? = nil{{/required}}{{#hasMore}}, {{/hasMore}}{{/parameters}}{{#hasParams}}, {{/hasParams}}server: Server? = nil) -> EndpointRequest<{{#parameters}}{{#isBodyParam}}{{{dataType}}}{{/isBodyParam}}{{/parameters}}{{^hasBodyParam}}EmptyBody{{/hasBodyParam}}, {{{returnType}}}> { .init(templatePath: "{{{path}}}", method: .init(rawValue: "{{httpMethod}}"), body: {{#hasBodyParam}}body{{/hasBodyParam}}{{^hasBodyParam}}EmptyBody(){{/hasBodyParam}},