forked from loafle/openapi-generator-original
move option to kotlin client (#16627)
This commit is contained in:
parent
16c6cff28f
commit
9b39887eba
@ -39,7 +39,6 @@ These options may be applied as additional-properties (cli) or configOptions (pl
|
||||
|parcelizeModels|toggle "@Parcelize" for generated models| |null|
|
||||
|returnResponse|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.| |false|
|
||||
|serializableModel|boolean - toggle "implements Serializable" for generated models| |null|
|
||||
|serializationLibrary|What serialization library to use: 'moshi' (default), or 'gson' or 'jackson' or 'kotlinx_serialization'| |moshi|
|
||||
|sortModelPropertiesByRequiredFlag|Sort model properties to place required parameters before optional parameters.| |null|
|
||||
|sortParamsByRequiredFlag|Sort method arguments to place required parameters before optional parameters.| |null|
|
||||
|sourceFolder|source folder for generated code| |src/main/kotlin|
|
||||
|
@ -41,7 +41,6 @@ These options may be applied as additional-properties (cli) or configOptions (pl
|
||||
|parcelizeModels|toggle "@Parcelize" for generated models| |null|
|
||||
|reactive|use coroutines for reactive behavior| |false|
|
||||
|serializableModel|boolean - toggle "implements Serializable" for generated models| |null|
|
||||
|serializationLibrary|What serialization library to use: 'moshi' (default), or 'gson' or 'jackson' or 'kotlinx_serialization'| |moshi|
|
||||
|serverPort|configuration the port in which the sever is to run on| |8080|
|
||||
|serviceImplementation|generate stub service implementations that extends service interfaces. If this is set to true service interfaces will also be generated| |false|
|
||||
|serviceInterface|generate service interfaces to go alongside controllers. In most cases this option would be used to update an existing project, so not to override implementations. Useful to help facilitate the generation gap pattern| |false|
|
||||
|
@ -28,7 +28,6 @@ These options may be applied as additional-properties (cli) or configOptions (pl
|
||||
|packageName|Generated artifact package name.| |org.openapitools|
|
||||
|parcelizeModels|toggle "@Parcelize" for generated models| |null|
|
||||
|serializableModel|boolean - toggle "implements Serializable" for generated models| |null|
|
||||
|serializationLibrary|What serialization library to use: 'moshi' (default), or 'gson' or 'jackson' or 'kotlinx_serialization'| |moshi|
|
||||
|sortModelPropertiesByRequiredFlag|Sort model properties to place required parameters before optional parameters.| |null|
|
||||
|sortParamsByRequiredFlag|Sort method arguments to place required parameters before optional parameters.| |null|
|
||||
|sourceFolder|source folder for generated code| |src/main/kotlin|
|
||||
|
@ -48,10 +48,6 @@ import static org.openapitools.codegen.utils.StringUtils.*;
|
||||
|
||||
public abstract class AbstractKotlinCodegen extends DefaultCodegen implements CodegenConfig {
|
||||
|
||||
public static final String SERIALIZATION_LIBRARY_DESC = "What serialization library to use: 'moshi' (default), or 'gson' or 'jackson' or 'kotlinx_serialization'";
|
||||
|
||||
public enum SERIALIZATION_LIBRARY_TYPE {moshi, gson, jackson, kotlinx_serialization}
|
||||
|
||||
public static final String MODEL_MUTABLE = "modelMutable";
|
||||
public static final String MODEL_MUTABLE_DESC = "Create mutable models";
|
||||
public static final String ADDITIONAL_MODEL_TYPE_ANNOTATIONS = "additionalModelTypeAnnotations";
|
||||
@ -81,7 +77,6 @@ public abstract class AbstractKotlinCodegen extends DefaultCodegen implements Co
|
||||
protected boolean nonPublicApi = false;
|
||||
|
||||
protected CodegenConstants.ENUM_PROPERTY_NAMING_TYPE enumPropertyNaming = CodegenConstants.ENUM_PROPERTY_NAMING_TYPE.camelCase;
|
||||
protected SERIALIZATION_LIBRARY_TYPE serializationLibrary = SERIALIZATION_LIBRARY_TYPE.moshi;
|
||||
|
||||
// model classes cannot use the same property names defined in HashMap
|
||||
// ref: https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-hash-map/
|
||||
@ -265,9 +260,6 @@ public abstract class AbstractKotlinCodegen extends DefaultCodegen implements Co
|
||||
CliOption enumPropertyNamingOpt = new CliOption(CodegenConstants.ENUM_PROPERTY_NAMING, CodegenConstants.ENUM_PROPERTY_NAMING_DESC);
|
||||
cliOptions.add(enumPropertyNamingOpt.defaultValue(enumPropertyNaming.name()));
|
||||
|
||||
CliOption serializationLibraryOpt = new CliOption(CodegenConstants.SERIALIZATION_LIBRARY, SERIALIZATION_LIBRARY_DESC);
|
||||
cliOptions.add(serializationLibraryOpt.defaultValue(serializationLibrary.name()));
|
||||
|
||||
cliOptions.add(new CliOption(CodegenConstants.PARCELIZE_MODELS, CodegenConstants.PARCELIZE_MODELS_DESC));
|
||||
cliOptions.add(new CliOption(CodegenConstants.SERIALIZABLE_MODEL, CodegenConstants.SERIALIZABLE_MODEL_DESC));
|
||||
cliOptions.add(new CliOption(CodegenConstants.SORT_PARAMS_BY_REQUIRED_FLAG, CodegenConstants.SORT_PARAMS_BY_REQUIRED_FLAG_DESC));
|
||||
@ -313,9 +305,6 @@ public abstract class AbstractKotlinCodegen extends DefaultCodegen implements Co
|
||||
return this.enumPropertyNaming;
|
||||
}
|
||||
|
||||
public SERIALIZATION_LIBRARY_TYPE getSerializationLibrary() {
|
||||
return this.serializationLibrary;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the naming convention for Kotlin enum properties
|
||||
@ -334,24 +323,6 @@ public abstract class AbstractKotlinCodegen extends DefaultCodegen implements Co
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the serialization engine for Kotlin
|
||||
*
|
||||
* @param enumSerializationLibrary The string representation of the serialization library as defined by
|
||||
* {@link org.openapitools.codegen.languages.AbstractKotlinCodegen.SERIALIZATION_LIBRARY_TYPE}
|
||||
*/
|
||||
public void setSerializationLibrary(final String enumSerializationLibrary) {
|
||||
try {
|
||||
this.serializationLibrary = SERIALIZATION_LIBRARY_TYPE.valueOf(enumSerializationLibrary);
|
||||
} catch (IllegalArgumentException ex) {
|
||||
StringBuilder sb = new StringBuilder(enumSerializationLibrary + " is an invalid enum property naming option. Please choose from:");
|
||||
for (SERIALIZATION_LIBRARY_TYPE t : SERIALIZATION_LIBRARY_TYPE.values()) {
|
||||
sb.append("\n ").append(t.name());
|
||||
}
|
||||
throw new RuntimeException(sb.toString());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* returns the OpenAPI type for the property
|
||||
*
|
||||
@ -463,13 +434,6 @@ public abstract class AbstractKotlinCodegen extends DefaultCodegen implements Co
|
||||
setEnumPropertyNaming((String) additionalProperties.get(CodegenConstants.ENUM_PROPERTY_NAMING));
|
||||
}
|
||||
|
||||
if (additionalProperties.containsKey(CodegenConstants.SERIALIZATION_LIBRARY)) {
|
||||
setSerializationLibrary((String) additionalProperties.get(CodegenConstants.SERIALIZATION_LIBRARY));
|
||||
additionalProperties.put(this.serializationLibrary.name(), true);
|
||||
} else {
|
||||
additionalProperties.put(this.serializationLibrary.name(), true);
|
||||
}
|
||||
|
||||
if (additionalProperties.containsKey(CodegenConstants.SOURCE_FOLDER)) {
|
||||
this.setSourceFolder((String) additionalProperties.get(CodegenConstants.SOURCE_FOLDER));
|
||||
} else {
|
||||
|
@ -104,6 +104,9 @@ public class KotlinClientCodegen extends AbstractKotlinCodegen {
|
||||
|
||||
protected String authFolder;
|
||||
|
||||
protected SERIALIZATION_LIBRARY_TYPE serializationLibrary = SERIALIZATION_LIBRARY_TYPE.moshi;
|
||||
public static final String SERIALIZATION_LIBRARY_DESC = "What serialization library to use: 'moshi' (default), or 'gson' or 'jackson' or 'kotlinx_serialization'";
|
||||
public enum SERIALIZATION_LIBRARY_TYPE {moshi, gson, jackson, kotlinx_serialization}
|
||||
|
||||
public enum DateLibrary {
|
||||
STRING("string"),
|
||||
@ -250,6 +253,9 @@ public class KotlinClientCodegen extends AbstractKotlinCodegen {
|
||||
cliOptions.add(CliOption.newBoolean(GENERATE_ROOM_MODELS, "Generate Android Room database models in addition to API models (JVM Volley library only)", false));
|
||||
|
||||
cliOptions.add(CliOption.newBoolean(SUPPORT_ANDROID_API_LEVEL_25_AND_BELLOW, "[WARNING] This flag will generate code that has a known security vulnerability. It uses `kotlin.io.createTempFile` instead of `java.nio.file.Files.createTempFile` in order to support Android API level 25 and bellow. For more info, please check the following links https://github.com/OpenAPITools/openapi-generator/security/advisories/GHSA-23x4-m842-fmwf, https://github.com/OpenAPITools/openapi-generator/pull/9284"));
|
||||
|
||||
CliOption serializationLibraryOpt = new CliOption(CodegenConstants.SERIALIZATION_LIBRARY, SERIALIZATION_LIBRARY_DESC);
|
||||
cliOptions.add(serializationLibraryOpt.defaultValue(serializationLibrary.name()));
|
||||
}
|
||||
|
||||
public CodegenType getTag() {
|
||||
@ -321,6 +327,28 @@ public class KotlinClientCodegen extends AbstractKotlinCodegen {
|
||||
this.omitGradleWrapper = omitGradleWrapper;
|
||||
}
|
||||
|
||||
public SERIALIZATION_LIBRARY_TYPE getSerializationLibrary() {
|
||||
return this.serializationLibrary;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the serialization engine for Kotlin
|
||||
*
|
||||
* @param enumSerializationLibrary The string representation of the serialization library as defined by
|
||||
* {@link org.openapitools.codegen.languages.AbstractKotlinCodegen.SERIALIZATION_LIBRARY_TYPE}
|
||||
*/
|
||||
public void setSerializationLibrary(final String enumSerializationLibrary) {
|
||||
try {
|
||||
this.serializationLibrary = SERIALIZATION_LIBRARY_TYPE.valueOf(enumSerializationLibrary);
|
||||
} catch (IllegalArgumentException ex) {
|
||||
StringBuilder sb = new StringBuilder(enumSerializationLibrary + " is an invalid enum property naming option. Please choose from:");
|
||||
for (SERIALIZATION_LIBRARY_TYPE t : SERIALIZATION_LIBRARY_TYPE.values()) {
|
||||
sb.append("\n ").append(t.name());
|
||||
}
|
||||
throw new RuntimeException(sb.toString());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String modelFilename(String templateName, String modelName) {
|
||||
String suffix = modelTemplateFiles().get(templateName);
|
||||
@ -403,6 +431,13 @@ public class KotlinClientCodegen extends AbstractKotlinCodegen {
|
||||
setOmitGradleWrapper(Boolean.parseBoolean(additionalProperties.get(OMIT_GRADLE_WRAPPER).toString()));
|
||||
}
|
||||
|
||||
if (additionalProperties.containsKey(CodegenConstants.SERIALIZATION_LIBRARY)) {
|
||||
setSerializationLibrary((String) additionalProperties.get(CodegenConstants.SERIALIZATION_LIBRARY));
|
||||
additionalProperties.put(this.serializationLibrary.name(), true);
|
||||
} else {
|
||||
additionalProperties.put(this.serializationLibrary.name(), true);
|
||||
}
|
||||
|
||||
commonSupportingFiles();
|
||||
|
||||
switch (getLibrary()) {
|
||||
|
@ -24,7 +24,7 @@ public class KotlinJvmVolleyModelCodegenTest {
|
||||
KotlinClientCodegen codegen = new KotlinClientCodegen();
|
||||
codegen.additionalProperties().put(KotlinClientCodegen.GENERATE_ROOM_MODELS, true);
|
||||
codegen.additionalProperties().put(KotlinClientCodegen.ROOM_MODEL_PACKAGE, "models.room");
|
||||
codegen.additionalProperties().put(CodegenConstants.SERIALIZATION_LIBRARY, AbstractKotlinCodegen.SERIALIZATION_LIBRARY_TYPE.gson.name());
|
||||
codegen.additionalProperties().put(CodegenConstants.SERIALIZATION_LIBRARY, KotlinClientCodegen.SERIALIZATION_LIBRARY_TYPE.gson.name());
|
||||
|
||||
String outputPath = checkModel(codegen, false);
|
||||
|
||||
@ -37,7 +37,7 @@ public class KotlinJvmVolleyModelCodegenTest {
|
||||
public void modelsWithoutRoomModels() throws IOException {
|
||||
KotlinClientCodegen codegen = new KotlinClientCodegen();
|
||||
codegen.additionalProperties().put(KotlinClientCodegen.GENERATE_ROOM_MODELS, false);
|
||||
codegen.additionalProperties().put(CodegenConstants.SERIALIZATION_LIBRARY, AbstractKotlinCodegen.SERIALIZATION_LIBRARY_TYPE.gson.name());
|
||||
codegen.additionalProperties().put(CodegenConstants.SERIALIZATION_LIBRARY, KotlinClientCodegen.SERIALIZATION_LIBRARY_TYPE.gson.name());
|
||||
|
||||
String outputPath = checkModel(codegen, false);
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user