[kotlin-server] cleanup KotlinServerCodegen configuration (#15653)

This commit is contained in:
Stephan Strate
2023-05-29 09:58:08 +02:00
committed by GitHub
parent 431cc2ec8e
commit 8e2fa826e5
4 changed files with 86 additions and 25 deletions

View File

@@ -22,6 +22,7 @@ import io.swagger.v3.parser.util.SchemaTypeUtil;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Objects;
public class CliOption {
private final String opt;
@@ -144,4 +145,22 @@ public class CliOption {
}
return sb.toString();
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
CliOption cliOption = (CliOption) o;
return Objects.equals(opt, cliOption.opt) &&
Objects.equals(description, cliOption.description) &&
Objects.equals(type, cliOption.type) &&
Objects.equals(defaultValue, cliOption.defaultValue) &&
Objects.equals(optValue, cliOption.optValue) &&
Objects.equals(enumValues, cliOption.enumValues);
}
@Override
public int hashCode() {
return Objects.hash(opt, description, type, defaultValue, optValue, enumValues);
}
}

View File

@@ -7535,9 +7535,15 @@ public class DefaultCodegen implements CodegenConfig {
}
protected void addOption(String key, String description, String defaultValue) {
addOption(key, description, defaultValue, null);
}
protected void addOption(String key, String description, String defaultValue, Map<String, String> enumValues) {
CliOption option = new CliOption(key, description);
if (defaultValue != null)
option.defaultValue(defaultValue);
if (enumValues != null)
option.setEnum(enumValues);
cliOptions.add(option);
}

View File

@@ -19,7 +19,6 @@ package org.openapitools.codegen.languages;
import com.google.common.collect.ImmutableMap;
import org.apache.commons.lang3.StringUtils;
import org.openapitools.codegen.CliOption;
import org.openapitools.codegen.CodegenConstants;
import org.openapitools.codegen.CodegenType;
import org.openapitools.codegen.SupportingFile;
@@ -36,9 +35,6 @@ import java.util.Map;
public class KotlinServerCodegen extends AbstractKotlinCodegen implements BeanValidationFeatures {
public static final String INTERFACE_ONLY = "interfaceOnly";
public static final String USE_COROUTINES = "useCoroutines";
public static final String RETURN_RESPONSE = "returnResponse";
public static final String DEFAULT_LIBRARY = Constants.KTOR;
private final Logger LOGGER = LoggerFactory.getLogger(KotlinServerCodegen.class);
@@ -65,6 +61,12 @@ public class KotlinServerCodegen extends AbstractKotlinCodegen implements BeanVa
Constants.RESOURCES,
Constants.METRICS
))
.put(Constants.JAXRS_SPEC, Arrays.asList(
USE_BEANVALIDATION,
Constants.USE_COROUTINES,
Constants.RETURN_RESPONSE,
Constants.INTERFACE_ONLY
))
.build();
/**
@@ -115,12 +117,7 @@ public class KotlinServerCodegen extends AbstractKotlinCodegen implements BeanVa
supportedLibraries.put(Constants.JAXRS_SPEC, "JAX-RS spec only");
// TODO: Configurable server engine. Defaults to netty in build.gradle.
CliOption library = new CliOption(CodegenConstants.LIBRARY, CodegenConstants.LIBRARY_DESC);
library.setDefault(DEFAULT_LIBRARY);
library.setEnum(supportedLibraries);
cliOptions.add(library);
addOption(CodegenConstants.LIBRARY, CodegenConstants.LIBRARY_DESC, DEFAULT_LIBRARY, supportedLibraries);
addSwitch(Constants.AUTOMATIC_HEAD_REQUESTS, Constants.AUTOMATIC_HEAD_REQUESTS_DESC, getAutoHeadFeatureEnabled());
addSwitch(Constants.CONDITIONAL_HEADERS, Constants.CONDITIONAL_HEADERS_DESC, getConditionalHeadersFeatureEnabled());
addSwitch(Constants.HSTS, Constants.HSTS_DESC, getHstsFeatureEnabled());
@@ -128,12 +125,11 @@ public class KotlinServerCodegen extends AbstractKotlinCodegen implements BeanVa
addSwitch(Constants.COMPRESSION, Constants.COMPRESSION_DESC, getCompressionFeatureEnabled());
addSwitch(Constants.RESOURCES, Constants.RESOURCES_DESC, getResourcesFeatureEnabled());
addSwitch(Constants.METRICS, Constants.METRICS_DESC, getMetricsFeatureEnabled());
cliOptions.add(CliOption.newBoolean(INTERFACE_ONLY, "Whether to generate only API interface stubs without the server files. This option is currently supported only when using jaxrs-spec library.").defaultValue(String.valueOf(interfaceOnly)));
cliOptions.add(CliOption.newBoolean(USE_BEANVALIDATION, "Use BeanValidation API annotations. This option is currently supported only when using jaxrs-spec library.", useBeanValidation));
cliOptions.add(CliOption.newBoolean(USE_COROUTINES, "Whether to use the Coroutines. This option is currently supported only when using jaxrs-spec library.", useCoroutines));
cliOptions.add(CliOption.newBoolean(RETURN_RESPONSE, "Whether generate API interface should return javax.ws.rs.core.Response instead of a deserialized entity. Only useful if interfaceOnly is true. This option is currently supported only when using jaxrs-spec library.").defaultValue(String.valueOf(returnResponse)));
cliOptions.add(CliOption.newBoolean(USE_JAKARTA_EE, "whether to use Jakarta EE namespace instead of javax", useJakartaEe));
addSwitch(Constants.INTERFACE_ONLY, Constants.INTERFACE_ONLY_DESC, interfaceOnly);
addSwitch(USE_BEANVALIDATION, Constants.USE_BEANVALIDATION_DESC, useBeanValidation);
addSwitch(Constants.USE_COROUTINES, Constants.USE_COROUTINES_DESC, useCoroutines);
addSwitch(Constants.RETURN_RESPONSE, Constants.RETURN_RESPONSE_DESC, returnResponse);
addSwitch(USE_JAKARTA_EE, Constants.USE_JAKARTA_EE_DESC, useJakartaEe);
}
public Boolean getAutoHeadFeatureEnabled() {
@@ -216,24 +212,24 @@ public class KotlinServerCodegen extends AbstractKotlinCodegen implements BeanVa
this.setLibrary((String) additionalProperties.get(CodegenConstants.LIBRARY));
}
if (additionalProperties.containsKey(INTERFACE_ONLY)) {
interfaceOnly = Boolean.parseBoolean(additionalProperties.get(INTERFACE_ONLY).toString());
if (additionalProperties.containsKey(Constants.INTERFACE_ONLY)) {
interfaceOnly = Boolean.parseBoolean(additionalProperties.get(Constants.INTERFACE_ONLY).toString());
if (!interfaceOnly) {
additionalProperties.remove(INTERFACE_ONLY);
additionalProperties.remove(Constants.INTERFACE_ONLY);
}
}
if (additionalProperties.containsKey(USE_COROUTINES)) {
useCoroutines = Boolean.parseBoolean(additionalProperties.get(USE_COROUTINES).toString());
if (additionalProperties.containsKey(Constants.USE_COROUTINES)) {
useCoroutines = Boolean.parseBoolean(additionalProperties.get(Constants.USE_COROUTINES).toString());
if (!useCoroutines) {
additionalProperties.remove(USE_COROUTINES);
additionalProperties.remove(Constants.USE_COROUTINES);
}
}
if (additionalProperties.containsKey(RETURN_RESPONSE)) {
returnResponse = Boolean.parseBoolean(additionalProperties.get(RETURN_RESPONSE).toString());
if (additionalProperties.containsKey(Constants.RETURN_RESPONSE)) {
returnResponse = Boolean.parseBoolean(additionalProperties.get(Constants.RETURN_RESPONSE).toString());
if (!returnResponse) {
additionalProperties.remove(RETURN_RESPONSE);
additionalProperties.remove(Constants.RETURN_RESPONSE);
}
}
@@ -344,6 +340,14 @@ public class KotlinServerCodegen extends AbstractKotlinCodegen implements BeanVa
public final static String RESOURCES_DESC = "Generates routes in a typed way, for both: constructing URLs and reading the parameters.";
public final static String METRICS = "featureMetrics";
public final static String METRICS_DESC = "Enables metrics feature.";
public static final String INTERFACE_ONLY = "interfaceOnly";
public static final String INTERFACE_ONLY_DESC = "Whether to generate only API interface stubs without the server files. This option is currently supported only when using jaxrs-spec library.";
public static final String USE_BEANVALIDATION_DESC = "Use BeanValidation API annotations. This option is currently supported only when using jaxrs-spec library.";
public static final String USE_COROUTINES = "useCoroutines";
public static final String USE_COROUTINES_DESC = "Whether to use the Coroutines. This option is currently supported only when using jaxrs-spec library.";
public static final String RETURN_RESPONSE = "returnResponse";
public static final String RETURN_RESPONSE_DESC = "Whether generate API interface should return javax.ws.rs.core.Response instead of a deserialized entity. Only useful if interfaceOnly is true. This option is currently supported only when using jaxrs-spec library.";
public static final String USE_JAKARTA_EE_DESC = "whether to use Jakarta EE namespace instead of javax";
}
@Override

View File

@@ -4660,4 +4660,36 @@ public class DefaultCodegenTest {
Assert.assertFalse(inlineEnumSchemaProperty.isPrimitiveType);
}
@Test
public void testAddOption() {
final DefaultCodegen codegen = new DefaultCodegen();
codegen.addOption("optionKey", "optionDesc", "defaultValue", Map.of("defaultValue", "defaultDesc"));
CliOption expected = new CliOption("optionKey", "optionDesc");
expected.setDefault("defaultValue");
expected.setEnum(Map.of("defaultValue", "defaultDesc"));
Assert.assertTrue(codegen.cliOptions.contains(expected));
}
@Test
public void testAddOptionDefaultNull() {
final DefaultCodegen codegen = new DefaultCodegen();
codegen.addOption("optionKey", "optionDesc", null);
CliOption expected = new CliOption("optionKey", "optionDesc");
Assert.assertTrue(codegen.cliOptions.contains(expected));
}
@Test
public void testAddOptionEnumValuesNull() {
final DefaultCodegen codegen = new DefaultCodegen();
codegen.addOption("optionKey", "optionDesc", "defaultValue");
CliOption expected = new CliOption("optionKey", "optionDesc");
expected.setDefault("defaultValue");
Assert.assertTrue(codegen.cliOptions.contains(expected));
}
}