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 swagger-core by swagger-api.
the class SimpleReaderTest method scanResourceWithCustomHttpMethodAnnotations.
@Test(description = "scan a resource with custom http method annotations")
public void scanResourceWithCustomHttpMethodAnnotations() {
Swagger swagger = getSwagger(ResourceWithCustomHTTPMethodAnnotations.class);
Operation get = getGet(swagger, "/");
assertNotNull(get);
Operation post = getPost(swagger, "/");
assertNotNull(post);
Operation patch = getPatch(swagger, "/");
assertNotNull(patch);
Operation put = getPut(swagger, "/");
assertNotNull(put);
Operation delete = getDelete(swagger, "/");
assertNotNull(delete);
}
use of io.swagger.models.Operation in project swagger-core by swagger-api.
the class SimpleScannerWithDecoratorExtensionTest method scanSimpleResourceWithDecorator.
/**
* Test for method annotated with vendor annotation which could be used for swagger documentation.
*/
@Test(description = "scan a simple resource with custom decorator")
public void scanSimpleResourceWithDecorator() {
final Swagger swagger = getSwagger(SimpleResourceWithVendorAnnotation.class);
assertEquals(swagger.getPaths().size(), 2);
final Operation get = getGet(swagger, "/{id}");
assertNotNull(get);
assertEquals(get.getParameters().size(), 2);
final Response response = get.getResponses().get(RESPONSE_STATUS_401);
assertNotNull(response);
assertEquals(response.getDescription(), RESPONSE_DESCRIPTION);
}
Aggregations