mirror of
https://github.com/OpenAPITools/openapi-generator.git
synced 2026-03-18 08:39:19 +00:00
Add an option to generate the alias (map, array) as model (#1729)
* add option to generate alias as model * fix issue due to incorrect merge
This commit is contained in:
@@ -264,11 +264,11 @@ public interface CodegenConfig {
|
||||
|
||||
boolean isEnablePostProcessFile();
|
||||
|
||||
public void setEnablePostProcessFile(boolean isEnablePostProcessFile);
|
||||
void setEnablePostProcessFile(boolean isEnablePostProcessFile);
|
||||
|
||||
// set OpenAPI and schemas
|
||||
public void setGlobalOpenAPI(OpenAPI openAPI);
|
||||
void setGlobalOpenAPI(OpenAPI openAPI);
|
||||
|
||||
public void setGlobalSchemas(OpenAPI openAPI);
|
||||
void setGlobalSchemas(OpenAPI openAPI);
|
||||
|
||||
}
|
||||
|
||||
@@ -284,6 +284,9 @@ public class CodegenConstants {
|
||||
|
||||
public static final String OPEN_API_SPEC_NAME = "openAPISpecName";
|
||||
|
||||
public static final String GENERATE_ALIAS_AS_MODEL = "generateAliasAsModel";
|
||||
public static final String GENERATE_ALIAS_AS_MODEL_DESC = "Generate alias to map, array as models";
|
||||
|
||||
public static final String USE_COMPARE_NET_OBJECTS = "useCompareNetObjects";
|
||||
public static final String USE_COMPARE_NET_OBJECTS_DESC = "Use KellermanSoftware.CompareNetObjects for deep recursive object comparison. WARNING: this option incurs potential performance impact.";
|
||||
}
|
||||
@@ -209,6 +209,11 @@ public class DefaultCodegen implements CodegenConfig {
|
||||
this.setEnablePostProcessFile(Boolean.valueOf(additionalProperties
|
||||
.get(CodegenConstants.ENABLE_POST_PROCESS_FILE).toString()));
|
||||
}
|
||||
|
||||
if (additionalProperties.containsKey(CodegenConstants.GENERATE_ALIAS_AS_MODEL)) {
|
||||
ModelUtils.setGenerateAliasAsModel(Boolean.valueOf(additionalProperties
|
||||
.get(CodegenConstants.GENERATE_ALIAS_AS_MODEL).toString()));
|
||||
}
|
||||
}
|
||||
|
||||
// override with any special post-processing for all models
|
||||
|
||||
@@ -434,13 +434,13 @@ public class DefaultGenerator extends AbstractGenerator implements Generator {
|
||||
LOGGER.info("Model " + name + " not generated since it's a free-form object");
|
||||
continue;
|
||||
} else if (ModelUtils.isMapSchema(schema)) { // check to see if it's a "map" model
|
||||
if (schema.getProperties() == null || schema.getProperties().isEmpty()) {
|
||||
if (!ModelUtils.isGenerateAliasAsModel() && (schema.getProperties() == null || schema.getProperties().isEmpty())) {
|
||||
// schema without property, i.e. alias to map
|
||||
LOGGER.info("Model " + name + " not generated since it's an alias to map (without property)");
|
||||
continue;
|
||||
}
|
||||
} else if (ModelUtils.isArraySchema(schema)) { // check to see if it's an "array" model
|
||||
if (schema.getProperties() == null || schema.getProperties().isEmpty()) {
|
||||
if (!ModelUtils.isGenerateAliasAsModel() && (schema.getProperties() == null || schema.getProperties().isEmpty())) {
|
||||
// schema without property, i.e. alias to array
|
||||
LOGGER.info("Model " + name + " not generated since it's an alias to array (without property)");
|
||||
continue;
|
||||
|
||||
@@ -220,6 +220,15 @@ public class CodegenConfigurator implements Serializable {
|
||||
return this;
|
||||
}
|
||||
|
||||
public boolean isGenerateAliasAsModel() {
|
||||
return ModelUtils.isGenerateAliasAsModel();
|
||||
}
|
||||
|
||||
public CodegenConfigurator setGenerateAliasAsModel(boolean generateAliasAsModel) {
|
||||
ModelUtils.setGenerateAliasAsModel(generateAliasAsModel);
|
||||
return this;
|
||||
}
|
||||
|
||||
public String getModelNameSuffix() {
|
||||
return modelNameSuffix;
|
||||
}
|
||||
|
||||
@@ -61,6 +61,16 @@ import java.util.stream.Collectors;
|
||||
|
||||
public class ModelUtils {
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(ModelUtils.class);
|
||||
private static boolean generateAliasAsModel = false;
|
||||
|
||||
public static void setGenerateAliasAsModel(boolean value) {
|
||||
generateAliasAsModel = value;
|
||||
}
|
||||
|
||||
public static boolean isGenerateAliasAsModel() {
|
||||
return generateAliasAsModel;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Searches for the model by name in the map of models and returns it
|
||||
@@ -769,15 +779,23 @@ public class ModelUtils {
|
||||
// top-level enum class
|
||||
return schema;
|
||||
} else if (isArraySchema(ref)) {
|
||||
return unaliasSchema(allSchemas, allSchemas.get(ModelUtils.getSimpleRef(schema.get$ref())));
|
||||
if (generateAliasAsModel) {
|
||||
return schema; // generate a model extending array
|
||||
} else {
|
||||
return unaliasSchema(allSchemas, allSchemas.get(ModelUtils.getSimpleRef(schema.get$ref())));
|
||||
}
|
||||
} else if (isComposedSchema(ref)) {
|
||||
return schema;
|
||||
} else if (isMapSchema(ref)) {
|
||||
if (ref.getProperties() != null && !ref.getProperties().isEmpty()) // has at least one property
|
||||
return schema; // treat it as model
|
||||
else {
|
||||
// treat it as a typical map
|
||||
return unaliasSchema(allSchemas, allSchemas.get(ModelUtils.getSimpleRef(schema.get$ref())));
|
||||
if (generateAliasAsModel) {
|
||||
return schema; // generate a model extending map
|
||||
} else {
|
||||
// treat it as a typical map
|
||||
return unaliasSchema(allSchemas, allSchemas.get(ModelUtils.getSimpleRef(schema.get$ref())));
|
||||
}
|
||||
}
|
||||
} else if (isObjectSchema(ref)) { // model
|
||||
if (ref.getProperties() != null && !ref.getProperties().isEmpty()) { // has at least one property
|
||||
|
||||
Reference in New Issue
Block a user