Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an option to generate the alias (map, array) as model #1729

Merged
merged 4 commits into from
Dec 31, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,9 @@ public class Generate implements Runnable {
@Option(name = {"--enable-post-process-file"}, title = "enable post-process file", description = CodegenConstants.ENABLE_POST_PROCESS_FILE)
private Boolean enablePostProcessFile;

@Option(name = {"--generate-alias-as-model"}, title = "generate alias (array, map) as model", description = CodegenConstants.GENERATE_ALIAS_AS_MODEL_DESC)
private Boolean generateAliasAsModel;

@Override
public void run() {
if (logToStderr != null) {
Expand Down Expand Up @@ -334,6 +337,10 @@ public void run() {
configurator.setEnablePostProcessFile(enablePostProcessFile);
}

if (generateAliasAsModel != null) {
configurator.setGenerateAliasAsModel(generateAliasAsModel);
}

applySystemPropertiesKvpList(systemProperties, configurator);
applyInstantiationTypesKvpList(instantiationTypes, configurator);
applyImportMappingsKvpList(importMappings, configurator);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,11 +262,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);

}
Original file line number Diff line number Diff line change
Expand Up @@ -283,4 +283,7 @@ public static enum ENUM_PROPERTY_NAMING_TYPE {camelCase, PascalCase, snake_case,
public static final String ENABLE_POST_PROCESS_FILE_DESC = "Enable post-processing file using environment variables.";

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";
}
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,11 @@ public void processOpts() {
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ private Model getParent(Model model) {

// check to see if it's a "map" model
if (ModelUtils.isMapSchema(schema)) {
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;
Expand All @@ -441,7 +441,7 @@ private Model getParent(Model model) {

// check to see if it's an "array" model
if (ModelUtils.isArraySchema(schema)) {
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,15 @@ public CodegenConfigurator setEnablePostProcessFile(boolean enablePostProcessFil
return this;
}

public boolean isGenerateAliasAsModel() {
return ModelUtils.isGenerateAliasAsModel();
}

public CodegenConfigurator setGenerateAliasAsModel(boolean generateAliasAsModel) {
ModelUtils.setGenerateAliasAsModel(generateAliasAsModel);
return this;
}

public String getModelNameSuffix() {
return modelNameSuffix;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,16 @@

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
Expand Down Expand Up @@ -769,15 +779,23 @@ public static Schema unaliasSchema(Map<String, Schema> allSchemas, Schema schema
// 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
Expand Down