mirror of
https://github.com/OpenAPITools/openapi-generator.git
synced 2025-07-04 14:40:53 +00:00
codegen should honor path level parameters
This commit is contained in:
parent
e78844cb54
commit
0a8fb1e532
@ -13,6 +13,7 @@ import io.swagger.models.Path;
|
|||||||
import io.swagger.models.Swagger;
|
import io.swagger.models.Swagger;
|
||||||
import io.swagger.models.auth.OAuth2Definition;
|
import io.swagger.models.auth.OAuth2Definition;
|
||||||
import io.swagger.models.auth.SecuritySchemeDefinition;
|
import io.swagger.models.auth.SecuritySchemeDefinition;
|
||||||
|
import io.swagger.models.parameters.Parameter;
|
||||||
import io.swagger.util.Json;
|
import io.swagger.util.Json;
|
||||||
|
|
||||||
import org.apache.commons.io.IOUtils;
|
import org.apache.commons.io.IOUtils;
|
||||||
@ -123,7 +124,7 @@ public class DefaultGenerator extends AbstractGenerator implements Generator {
|
|||||||
Map<String, Model> definitions = swagger.getDefinitions();
|
Map<String, Model> definitions = swagger.getDefinitions();
|
||||||
if (definitions != null) {
|
if (definitions != null) {
|
||||||
List<String> sortedModelKeys = sortModelsByInheritance(definitions);
|
List<String> sortedModelKeys = sortModelsByInheritance(definitions);
|
||||||
|
|
||||||
for (String name : sortedModelKeys) {
|
for (String name : sortedModelKeys) {
|
||||||
Model model = definitions.get(name);
|
Model model = definitions.get(name);
|
||||||
Map<String, Model> modelMap = new HashMap<String, Model>();
|
Map<String, Model> modelMap = new HashMap<String, Model>();
|
||||||
@ -331,7 +332,7 @@ public class DefaultGenerator extends AbstractGenerator implements Generator {
|
|||||||
operation.put(flagFieldName, true);
|
operation.put(flagFieldName, true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private List<String> sortModelsByInheritance(final Map<String, Model> definitions) {
|
private List<String> sortModelsByInheritance(final Map<String, Model> definitions) {
|
||||||
List<String> sortedModelKeys = new ArrayList<String>(definitions.keySet());
|
List<String> sortedModelKeys = new ArrayList<String>(definitions.keySet());
|
||||||
Comparator<String> cmp = new Comparator<String>() {
|
Comparator<String> cmp = new Comparator<String>() {
|
||||||
@ -339,10 +340,10 @@ public class DefaultGenerator extends AbstractGenerator implements Generator {
|
|||||||
public int compare(String o1, String o2) {
|
public int compare(String o1, String o2) {
|
||||||
Model model1 = definitions.get(o1);
|
Model model1 = definitions.get(o1);
|
||||||
Model model2 = definitions.get(o2);
|
Model model2 = definitions.get(o2);
|
||||||
|
|
||||||
int model1InheritanceDepth = getInheritanceDepth(model1);
|
int model1InheritanceDepth = getInheritanceDepth(model1);
|
||||||
int model2InheritanceDepth = getInheritanceDepth(model2);
|
int model2InheritanceDepth = getInheritanceDepth(model2);
|
||||||
|
|
||||||
if (model1InheritanceDepth == model2InheritanceDepth) {
|
if (model1InheritanceDepth == model2InheritanceDepth) {
|
||||||
return 0;
|
return 0;
|
||||||
} else if (model1InheritanceDepth > model2InheritanceDepth) {
|
} else if (model1InheritanceDepth > model2InheritanceDepth) {
|
||||||
@ -351,30 +352,30 @@ public class DefaultGenerator extends AbstractGenerator implements Generator {
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private int getInheritanceDepth(Model model) {
|
private int getInheritanceDepth(Model model) {
|
||||||
int inheritanceDepth = 0;
|
int inheritanceDepth = 0;
|
||||||
Model parent = getParent(model);
|
Model parent = getParent(model);
|
||||||
|
|
||||||
while (parent != null) {
|
while (parent != null) {
|
||||||
inheritanceDepth++;
|
inheritanceDepth++;
|
||||||
parent = getParent(parent);
|
parent = getParent(parent);
|
||||||
}
|
}
|
||||||
|
|
||||||
return inheritanceDepth;
|
return inheritanceDepth;
|
||||||
}
|
}
|
||||||
|
|
||||||
private Model getParent(Model model) {
|
private Model getParent(Model model) {
|
||||||
if (model instanceof ComposedModel) {
|
if (model instanceof ComposedModel) {
|
||||||
return definitions.get(((ComposedModel) model).getParent().getReference());
|
return definitions.get(((ComposedModel) model).getParent().getReference());
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
Collections.sort(sortedModelKeys, cmp);
|
Collections.sort(sortedModelKeys, cmp);
|
||||||
|
|
||||||
return sortedModelKeys;
|
return sortedModelKeys;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -383,12 +384,12 @@ public class DefaultGenerator extends AbstractGenerator implements Generator {
|
|||||||
|
|
||||||
for (String resourcePath : paths.keySet()) {
|
for (String resourcePath : paths.keySet()) {
|
||||||
Path path = paths.get(resourcePath);
|
Path path = paths.get(resourcePath);
|
||||||
processOperation(resourcePath, "get", path.getGet(), ops);
|
processOperation(resourcePath, "get", path.getGet(), ops, path);
|
||||||
processOperation(resourcePath, "put", path.getPut(), ops);
|
processOperation(resourcePath, "put", path.getPut(), ops, path);
|
||||||
processOperation(resourcePath, "post", path.getPost(), ops);
|
processOperation(resourcePath, "post", path.getPost(), ops, path);
|
||||||
processOperation(resourcePath, "delete", path.getDelete(), ops);
|
processOperation(resourcePath, "delete", path.getDelete(), ops, path);
|
||||||
processOperation(resourcePath, "patch", path.getPatch(), ops);
|
processOperation(resourcePath, "patch", path.getPatch(), ops, path);
|
||||||
processOperation(resourcePath, "options", path.getOptions(), ops);
|
processOperation(resourcePath, "options", path.getOptions(), ops, path);
|
||||||
}
|
}
|
||||||
return ops;
|
return ops;
|
||||||
}
|
}
|
||||||
@ -401,14 +402,33 @@ public class DefaultGenerator extends AbstractGenerator implements Generator {
|
|||||||
return map.get(name);
|
return map.get(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void processOperation(String resourcePath, String httpMethod, Operation operation, Map<String, List<CodegenOperation>> operations, Path path) {
|
||||||
public void processOperation(String resourcePath, String httpMethod, Operation operation, Map<String, List<CodegenOperation>> operations) {
|
|
||||||
if (operation != null) {
|
if (operation != null) {
|
||||||
List<String> tags = operation.getTags();
|
List<String> tags = operation.getTags();
|
||||||
if (tags == null) {
|
if (tags == null) {
|
||||||
tags = new ArrayList<String>();
|
tags = new ArrayList<String>();
|
||||||
tags.add("default");
|
tags.add("default");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
build up a set of parameter "ids" defined at the operation level
|
||||||
|
per the swagger 2.0 spec "A unique parameter is defined by a combination of a name and location"
|
||||||
|
i'm assuming "location" == "in"
|
||||||
|
*/
|
||||||
|
Set<String> operationParameters = new HashSet<String>();
|
||||||
|
if (operation.getParameters() != null) {
|
||||||
|
for (Parameter parameter : operation.getParameters()) {
|
||||||
|
operationParameters.add(generateParameterId(parameter));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//need to propagate path level down to the operation
|
||||||
|
for (Parameter parameter : path.getParameters()) {
|
||||||
|
//skip propagation if a parameter with the same name is already defined at the operation level
|
||||||
|
if (!operationParameters.contains(generateParameterId(parameter))) {
|
||||||
|
operation.addParameter(parameter);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
for (String tag : tags) {
|
for (String tag : tags) {
|
||||||
CodegenOperation co = config.fromOperation(resourcePath, httpMethod, operation, swagger.getDefinitions());
|
CodegenOperation co = config.fromOperation(resourcePath, httpMethod, operation, swagger.getDefinitions());
|
||||||
@ -443,7 +463,7 @@ public class DefaultGenerator extends AbstractGenerator implements Generator {
|
|||||||
}
|
}
|
||||||
authMethods.put(securityName, oauth2Operation);
|
authMethods.put(securityName, oauth2Operation);
|
||||||
} else {
|
} else {
|
||||||
authMethods.put(securityName, securityDefinition);
|
authMethods.put(securityName, securityDefinition);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -454,6 +474,10 @@ public class DefaultGenerator extends AbstractGenerator implements Generator {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private String generateParameterId(Parameter parameter) {
|
||||||
|
return parameter.getName() + ":" + parameter.getIn();
|
||||||
|
}
|
||||||
|
|
||||||
protected String sanitizeTag(String tag) {
|
protected String sanitizeTag(String tag) {
|
||||||
// remove spaces and make strong case
|
// remove spaces and make strong case
|
||||||
String[] parts = tag.split(" ");
|
String[] parts = tag.split(" ");
|
||||||
|
Loading…
x
Reference in New Issue
Block a user