From 2c17a3d3764ea287da9081dff2667f29a5b29afa Mon Sep 17 00:00:00 2001 From: wing328 Date: Sun, 8 Apr 2018 12:22:43 +0800 Subject: [PATCH] add method to skip form parameter's model --- .../codegen/DefaultGenerator.java | 18 +++-- .../codegen/utils/ModelUtils.java | 77 ++++++++++++++++++- 2 files changed, 88 insertions(+), 7 deletions(-) diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultGenerator.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultGenerator.java index 2a89b711c0..f5ae4eb602 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultGenerator.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultGenerator.java @@ -26,6 +26,7 @@ import org.joda.time.DateTime; import org.openapitools.codegen.ignore.CodegenIgnoreProcessor; //import org.openapitools.codegen.languages.AbstractJavaCodegen; import org.openapitools.codegen.utils.ImplementationVersion; +import org.openapitools.codegen.utils.ModelUtils; import org.openapitools.codegen.utils.URLPathUtil; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -271,7 +272,7 @@ public class DefaultGenerator extends AbstractGenerator implements Generator { } } - private void generateModels(List files, List allModels) { + private void generateModels(List files, List allModels, List unusedModels) { if (!generateModels) { return; } @@ -363,6 +364,12 @@ public class DefaultGenerator extends AbstractGenerator implements Generator { continue; } + // don't generate models that are not used as object (e.g. form parameters) + if (unusedModels.contains(name)) { + LOGGER.debug("Model " + name + " not generated since it's marked as unused (due to form parameters)"); + continue; + } + Schema schema = schemas.get(name); Map schemaMap = new HashMap<>(); schemaMap.put(name, schema); @@ -753,16 +760,15 @@ public class DefaultGenerator extends AbstractGenerator implements Generator { configureGeneratorProperties(); configureOpenAPIInfo(); - /* TODO revise inline model logic // resolve inline models - InlineModelResolver inlineModelResolver = new InlineModelResolver(); - inlineModelResolver.flatten(openAPI); - */ + //InlineModelResolver inlineModelResolver = new InlineModelResolver(); + //inlineModelResolver.flatten(openAPI); List files = new ArrayList(); // models + List unusedSchemas = ModelUtils.getUnusedSchemas(openAPI); List allModels = new ArrayList(); - generateModels(files, allModels); + generateModels(files, allModels, unusedSchemas); // apis List allOperations = new ArrayList(); generateApis(files, allOperations, allModels); diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/ModelUtils.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/ModelUtils.java index d114a03e88..cd898b7df7 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/ModelUtils.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/ModelUtils.java @@ -1,15 +1,30 @@ package org.openapitools.codegen.utils; +import io.swagger.v3.oas.models.OpenAPI; +import io.swagger.v3.oas.models.Operation; +import io.swagger.v3.oas.models.PathItem; +import io.swagger.v3.oas.models.media.MediaType; +import io.swagger.v3.oas.models.media.Schema; +import io.swagger.v3.oas.models.parameters.RequestBody; import org.openapitools.codegen.CodegenModel; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import java.util.ArrayList; +import java.util.HashMap; import java.util.List; import java.util.Map; +import org.apache.commons.lang3.StringUtils; + + public class ModelUtils { + static Logger LOGGER = LoggerFactory.getLogger(ModelUtils.class); + /** * Searches for the model by name in the map of models and returns it * - * @param name Name of the model + * @param name Name of the model * @param models Map of models * @return model */ @@ -33,4 +48,64 @@ public class ModelUtils { } return null; } + + public static List getUnusedSchemas(OpenAPI openAPI) { + List unusedSchemas = new ArrayList(); + + // no model defined + if (openAPI.getComponents().getSchemas() == null) { + openAPI.getComponents().setSchemas(new HashMap()); + } + + // operations + Map paths = openAPI.getPaths(); + Map schemas = openAPI.getComponents().getSchemas(); + + if (paths != null) { + for (String pathname : paths.keySet()) { + PathItem path = paths.get(pathname); + Map operationMap = path.readOperationsMap(); + if (operationMap != null) { + for (PathItem.HttpMethod method : operationMap.keySet()) { + Operation operation = operationMap.get(method); + RequestBody requestBody = operation.getRequestBody(); + + if (requestBody == null) { + continue; + } + + //LOGGER.info("debugging resolver: " + requestBody.toString()); + if (requestBody.getContent() == null) { + continue; + } + + // go through "content" + for (String mimeType : requestBody.getContent().keySet()) { + if ("application/x-www-form-urlencoded".equalsIgnoreCase(mimeType) || + "multipart/form-data".equalsIgnoreCase(mimeType)) { + // remove the schema that's automatically created by the parser + MediaType mediaType = requestBody.getContent().get(mimeType); + if (mediaType.getSchema().get$ref() != null) { + LOGGER.debug("mark schema (form parameters) as unused: " + getSimpleRef(mediaType.getSchema().get$ref())); + unusedSchemas.add(getSimpleRef(mediaType.getSchema().get$ref())); + } + } + } + } + } + } + } + + return unusedSchemas; + } + + protected static String getSimpleRef(String ref) { + if (ref.startsWith("#/components/")) { + ref = ref.substring(ref.lastIndexOf("/") + 1); + } else if (ref.startsWith("#/definitions/")) { + ref = ref.substring(ref.lastIndexOf("/") + 1); + } + + return ref; + } }