use of io.swagger.models.Operation in project camel by apache.
the class SwaggerRestProducerFactory method getSwaggerOperation.
private Operation getSwaggerOperation(Swagger swagger, String verb, String path) {
// path may include base path so skip that
String basePath = swagger.getBasePath();
if (basePath != null && path.startsWith(basePath)) {
path = path.substring(basePath.length());
}
Path modelPath = swagger.getPath(path);
if (modelPath == null) {
return null;
}
// get,put,post,head,delete,patch,options
Operation op = null;
if ("get".equals(verb)) {
op = modelPath.getGet();
} else if ("put".equals(verb)) {
op = modelPath.getPut();
} else if ("post".equals(verb)) {
op = modelPath.getPost();
} else if ("head".equals(verb)) {
op = modelPath.getHead();
} else if ("delete".equals(verb)) {
op = modelPath.getDelete();
} else if ("patch".equals(verb)) {
op = modelPath.getPatch();
} else if ("options".equals(verb)) {
op = modelPath.getOptions();
}
return op;
}
use of io.swagger.models.Operation in project java-chassis by ServiceComb.
the class ClassUtils method createInterface.
private static Class<?> createInterface(Swagger swagger, ClassLoader classLoader, String packageName, String intfName) {
ClassConfig classConfig = new ClassConfig();
classConfig.setClassName(intfName);
classConfig.setIntf(true);
StringBuilder sbMethod = new StringBuilder();
StringBuilder sbMethodGenericSignature = new StringBuilder();
for (Path path : swagger.getPaths().values()) {
for (Operation operation : path.getOperations()) {
boolean hasGenericSignature = false;
sbMethod.setLength(0);
sbMethodGenericSignature.setLength(0);
Response result = operation.getResponses().get(SwaggerConst.SUCCESS_KEY);
JavaType resultJavaType = ConverterMgr.findJavaType(classLoader, packageName, swagger, result.getSchema());
hasGenericSignature = hasGenericSignature || resultJavaType.hasGenericTypes();
sbMethod.append(JavassistUtils.getNameForGenerateCode(resultJavaType)).append(" ").append(operation.getOperationId()).append("(");
sbMethodGenericSignature.append("(");
for (Parameter parameter : operation.getParameters()) {
String paramName = parameter.getName();
paramName = correctMethodParameterName(paramName);
JavaType paramJavaType = ConverterMgr.findJavaType(classLoader, packageName, swagger, parameter);
hasGenericSignature = hasGenericSignature || paramJavaType.hasGenericTypes();
String code = String.format("%s %s,", paramJavaType.getRawClass().getName(), paramName);
sbMethod.append(code);
sbMethodGenericSignature.append(paramJavaType.getGenericSignature());
}
if (!operation.getParameters().isEmpty()) {
sbMethod.setLength(sbMethod.length() - 1);
}
sbMethod.append(");");
sbMethodGenericSignature.append(")");
sbMethodGenericSignature.append(resultJavaType.getGenericSignature());
if (hasGenericSignature) {
classConfig.addMethod(sbMethod.toString(), sbMethodGenericSignature.toString());
} else {
classConfig.addMethod(sbMethod.toString(), null);
}
}
}
return JavassistUtils.createClass(classLoader, classConfig);
}
use of io.swagger.models.Operation in project java-chassis by ServiceComb.
the class TestApiResponse method checkApiResponseHeader.
private void checkApiResponseHeader(SwaggerGenerator generator) {
Swagger swagger = generator.getSwagger();
Path path = swagger.getPaths().get("/testApiResponseHeader");
Operation operation = path.getOperations().get(0);
Assert.assertEquals("testApiResponseHeader", operation.getOperationId());
Response response = operation.getResponses().get("200");
Property property = response.getHeaders().get("k1");
Assert.assertEquals(Integer.class, ConverterMgr.findJavaType(generator, property).getRawClass());
property = response.getHeaders().get("k2");
Assert.assertEquals(String.class, ConverterMgr.findJavaType(generator, property).getRawClass());
}
use of io.swagger.models.Operation in project java-chassis by ServiceComb.
the class RestOperationMeta method init.
public void init(OperationMeta operationMeta) {
this.operationMeta = operationMeta;
Swagger swagger = operationMeta.getSchemaMeta().getSwagger();
Operation operation = operationMeta.getSwaggerOperation();
this.produces = operation.getProduces();
if (produces == null) {
this.produces = swagger.getProduces();
}
setAbsolutePath(concatPath(swagger.getBasePath(), operationMeta.getOperationPath()));
this.createProduceProcessors();
Method method = operationMeta.getMethod();
Type[] genericParamTypes = method.getGenericParameterTypes();
if (genericParamTypes.length != operation.getParameters().size()) {
throw new Error("Param count is not equal between swagger and method, path=" + absolutePath);
}
// 初始化所有rest param
for (int idx = 0; idx < genericParamTypes.length; idx++) {
Parameter parameter = operation.getParameters().get(idx);
Type genericParamType = genericParamTypes[idx];
RestParam param = new RestParam(idx, parameter, genericParamType);
addParam(param);
}
this.pathBuilder = new URLPathBuilder(absolutePath, paramMap);
}
use of io.swagger.models.Operation in project cxf by apache.
the class Swagger2Customizer method customize.
public Swagger customize(Swagger data) {
if (dynamicBasePath) {
MessageContext ctx = createMessageContext();
String currentBasePath = StringUtils.substringBeforeLast(ctx.getHttpServletRequest().getRequestURI(), "/");
data.setBasePath(currentBasePath);
if (data.getHost() == null) {
data.setHost(beanConfig.getHost());
}
if (data.getInfo() == null) {
data.setInfo(beanConfig.getInfo());
}
if (beanConfig.getSwagger() != null && beanConfig.getSwagger().getSecurityDefinitions() != null && data.getSecurityDefinitions() == null) {
data.setSecurityDefinitions(beanConfig.getSwagger().getSecurityDefinitions());
}
}
if (replaceTags || javadocProvider != null) {
Map<String, ClassResourceInfo> operations = new HashMap<>();
Map<Pair<String, String>, OperationResourceInfo> methods = new HashMap<>();
for (ClassResourceInfo cri : cris) {
for (OperationResourceInfo ori : cri.getMethodDispatcher().getOperationResourceInfos()) {
String normalizedPath = getNormalizedPath(cri.getURITemplate().getValue(), ori.getURITemplate().getValue());
operations.put(normalizedPath, cri);
methods.put(ImmutablePair.of(ori.getHttpMethod(), normalizedPath), ori);
}
}
if (replaceTags && data.getTags() != null) {
data.getTags().clear();
}
for (final Map.Entry<String, Path> entry : data.getPaths().entrySet()) {
Tag tag = null;
if (replaceTags && operations.containsKey(entry.getKey())) {
ClassResourceInfo cri = operations.get(entry.getKey());
tag = new Tag();
tag.setName(cri.getURITemplate().getValue().replaceAll("/", "_"));
if (javadocProvider != null) {
tag.setDescription(javadocProvider.getClassDoc(cri));
}
data.addTag(tag);
}
for (Map.Entry<HttpMethod, Operation> subentry : entry.getValue().getOperationMap().entrySet()) {
if (replaceTags && tag != null) {
subentry.getValue().setTags(Collections.singletonList(tag.getName()));
}
Pair<String, String> key = ImmutablePair.of(subentry.getKey().name(), entry.getKey());
if (methods.containsKey(key) && javadocProvider != null) {
OperationResourceInfo ori = methods.get(key);
subentry.getValue().setSummary(javadocProvider.getMethodDoc(ori));
for (int i = 0; i < subentry.getValue().getParameters().size(); i++) {
subentry.getValue().getParameters().get(i).setDescription(javadocProvider.getMethodParameterDoc(ori, i));
}
addParameters(subentry.getValue().getParameters());
if (subentry.getValue().getResponses() != null && !subentry.getValue().getResponses().isEmpty()) {
subentry.getValue().getResponses().entrySet().iterator().next().getValue().setDescription(javadocProvider.getMethodResponseDoc(ori));
}
}
}
}
}
if (replaceTags && data.getTags() != null) {
Collections.sort(data.getTags(), new Comparator<Tag>() {
@Override
public int compare(final Tag tag1, final Tag tag2) {
return tag1.getName().compareTo(tag2.getName());
}
});
}
applyDefaultVersion(data);
return data;
}
Aggregations