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:
William Cheng
2018-12-31 11:59:58 +08:00
committed by GitHub
parent 2f6381cb19
commit 3ec90a86cb
7 changed files with 50 additions and 8 deletions

View File

@@ -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);
}

View File

@@ -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.";
}

View File

@@ -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

View File

@@ -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;

View File

@@ -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;
}

View File

@@ -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