mirror of
https://github.com/valitydev/openapi-generator.git
synced 2024-11-08 03:18:53 +00:00
Merge pull request #27 from wing328/inline_support
Mark form parameter's schemas as unused in OAS 3.0 spec
This commit is contained in:
commit
1160dbdb26
@ -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<File> files, List<Object> allModels) {
|
||||
private void generateModels(List<File> files, List<Object> allModels, List<String> 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<String, Schema> 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<File> files = new ArrayList<File>();
|
||||
// models
|
||||
List<String> unusedSchemas = ModelUtils.getUnusedSchemas(openAPI);
|
||||
List<Object> allModels = new ArrayList<Object>();
|
||||
generateModels(files, allModels);
|
||||
generateModels(files, allModels, unusedSchemas);
|
||||
// apis
|
||||
List<Object> allOperations = new ArrayList<Object>();
|
||||
generateApis(files, allOperations, allModels);
|
||||
|
@ -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<String> getUnusedSchemas(OpenAPI openAPI) {
|
||||
List<String> unusedSchemas = new ArrayList<String>();
|
||||
|
||||
// no model defined
|
||||
if (openAPI.getComponents().getSchemas() == null) {
|
||||
openAPI.getComponents().setSchemas(new HashMap<String, Schema>());
|
||||
}
|
||||
|
||||
// operations
|
||||
Map<String, PathItem> paths = openAPI.getPaths();
|
||||
Map<String, Schema> schemas = openAPI.getComponents().getSchemas();
|
||||
|
||||
if (paths != null) {
|
||||
for (String pathname : paths.keySet()) {
|
||||
PathItem path = paths.get(pathname);
|
||||
Map<PathItem.HttpMethod, Operation> 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;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user