Search in sources :

Example 6 with Operation

use of io.swagger.models.Operation in project swagger-core by swagger-api.

the class SpecFilter method filterOperation.

public Operation filterOperation(SwaggerSpecFilter filter, Operation op, ApiDescription api, Map<String, List<String>> params, Map<String, String> cookies, Map<String, List<String>> headers) {
    Operation clonedOperation = new Operation().summary(op.getSummary()).description(op.getDescription()).operationId(op.getOperationId()).schemes(op.getSchemes()).consumes(op.getConsumes()).produces(op.getProduces()).tags(op.getTags()).externalDocs(op.getExternalDocs()).vendorExtensions(op.getVendorExtensions()).deprecated(op.isDeprecated());
    List<Parameter> clonedParams = new ArrayList<Parameter>();
    if (op.getParameters() != null) {
        for (Parameter param : op.getParameters()) {
            if (filter.isParamAllowed(param, op, api, params, cookies, headers)) {
                clonedParams.add(param);
            }
        }
    }
    clonedOperation.setParameters(clonedParams);
    clonedOperation.setSecurity(op.getSecurity());
    clonedOperation.setResponses(op.getResponses());
    return clonedOperation;
}
Also used : ArrayList(java.util.ArrayList) BodyParameter(io.swagger.models.parameters.BodyParameter) Parameter(io.swagger.models.parameters.Parameter) Operation(io.swagger.models.Operation)

Example 7 with Operation

use of io.swagger.models.Operation in project swagger-core by swagger-api.

the class ReaderTest method readerTest1.

@Test
public void readerTest1() {
    final Swagger swagger = new Swagger();
    Reader.read(swagger, Collections.<Class<?>>singleton(ResourceWithAnnotations.class));
    Assert.assertNotNull(swagger);
    final Info info = swagger.getInfo();
    Assert.assertNotNull(info);
    Assert.assertEquals(info.getTitle(), "Test title");
    Assert.assertEquals(info.getDescription(), "Test description");
    Assert.assertEquals(info.getVersion(), "1.0.0");
    Assert.assertEquals(info.getTermsOfService(), "link_to_terms");
    Assert.assertEquals(info.getContact().getName(), "Author");
    Assert.assertEquals(info.getContact().getEmail(), "author@mail");
    Assert.assertEquals(info.getContact().getUrl(), "site");
    Assert.assertEquals(info.getLicense().getName(), "License");
    Assert.assertEquals(info.getLicense().getUrl(), "license_url");
    Assert.assertEquals(info.getVendorExtensions().get("x-ext1_prop1"), "ext1_val1");
    Assert.assertEquals(info.getVendorExtensions().get("x-ext1_prop2"), "x-ext1_val2");
    Assert.assertEquals(swagger.getHost(), "host");
    Assert.assertEquals(swagger.getBasePath(), "/api");
    Assert.assertNotNull(swagger.getPath("/resources/testMethod3"));
    Assert.assertNotNull(swagger.getDefinitions().get("SampleData"));
    Assert.assertEquals(swagger.getExternalDocs().getDescription(), "docs");
    Assert.assertEquals(swagger.getExternalDocs().getUrl(), "url_to_docs");
    Path path = swagger.getPath("/resources/testMethod3");
    Assert.assertNotNull(path);
    Operation get = path.getGet();
    Assert.assertNotNull(get);
    Assert.assertEquals("value", get.getVendorExtensions().get("x-name"));
}
Also used : Path(io.swagger.models.Path) ResourceWithAnnotations(io.swagger.servlet.resources.ResourceWithAnnotations) Swagger(io.swagger.models.Swagger) Operation(io.swagger.models.Operation) Info(io.swagger.models.Info) Test(org.testng.annotations.Test)

Example 8 with Operation

use of io.swagger.models.Operation in project swagger-core by swagger-api.

the class Reader method read.

private void read(ReaderContext context) {
    final SwaggerDefinition swaggerDefinition = context.getCls().getAnnotation(SwaggerDefinition.class);
    if (swaggerDefinition != null) {
        readSwaggerConfig(swaggerDefinition);
    }
    for (Method method : context.getCls().getMethods()) {
        if (ReflectionUtils.isOverriddenMethod(method, context.getCls())) {
            continue;
        }
        final Operation operation = new Operation();
        String operationPath = null;
        String httpMethod = null;
        final Type[] genericParameterTypes = method.getGenericParameterTypes();
        final Annotation[][] paramAnnotations = method.getParameterAnnotations();
        for (ReaderExtension extension : ReaderExtensions.getExtensions()) {
            if (operationPath == null) {
                operationPath = extension.getPath(context, method);
            }
            if (httpMethod == null) {
                httpMethod = extension.getHttpMethod(context, method);
            }
            if (operationPath == null || httpMethod == null) {
                continue;
            }
            if (extension.isReadable(context)) {
                extension.setDeprecated(operation, method);
                extension.applyConsumes(context, operation, method);
                extension.applyProduces(context, operation, method);
                extension.applyOperationId(operation, method);
                extension.applySummary(operation, method);
                extension.applyDescription(operation, method);
                extension.applySchemes(context, operation, method);
                extension.applySecurityRequirements(context, operation, method);
                extension.applyTags(context, operation, method);
                extension.applyResponses(context, operation, method);
                extension.applyImplicitParameters(context, operation, method);
                extension.applyExtensions(context, operation, method);
                for (int i = 0; i < genericParameterTypes.length; i++) {
                    extension.applyParameters(context, operation, genericParameterTypes[i], paramAnnotations[i]);
                }
            }
        }
        if (httpMethod != null) {
            if (operation.getResponses() == null) {
                operation.defaultResponse(new Response().description("successful operation"));
            }
            final Map<String, String> regexMap = new HashMap<String, String>();
            final String parsedPath = PathUtils.parsePath(operationPath, regexMap);
            Path path = swagger.getPath(parsedPath);
            if (path == null) {
                path = new Path();
                swagger.path(parsedPath, path);
            }
            path.set(httpMethod.toLowerCase(), operation);
        }
    }
}
Also used : Path(io.swagger.models.Path) HashMap(java.util.HashMap) Method(java.lang.reflect.Method) Operation(io.swagger.models.Operation) ReaderExtension(io.swagger.servlet.extensions.ReaderExtension) Response(io.swagger.models.Response) Type(java.lang.reflect.Type) SwaggerDefinition(io.swagger.annotations.SwaggerDefinition)

Example 9 with Operation

use of io.swagger.models.Operation in project swagger-core by swagger-api.

the class ConsumesProducesTest method applyConsumesProducesTest1.

@Test(dataProvider = "resourceWithAnnotations")
public void applyConsumesProducesTest1(String methodName, List<String> expected) throws NoSuchMethodException {
    final Operation operation = new Operation();
    final ReaderContext context = createDefaultContext();
    final Method method = findMethod(context, methodName);
    extension.applyConsumes(context, operation, method);
    extension.applyProduces(context, operation, method);
    Assert.assertEquals(operation.getConsumes(), expected);
    Assert.assertEquals(operation.getProduces(), expected);
}
Also used : ReaderContext(io.swagger.servlet.ReaderContext) Operation(io.swagger.models.Operation) Method(java.lang.reflect.Method) Test(org.testng.annotations.Test)

Example 10 with Operation

use of io.swagger.models.Operation in project swagger-core by swagger-api.

the class DeprecatedMethodTest method setDeprecatedTest.

@Test(dataProvider = "resources")
public void setDeprecatedTest(String methodName, Boolean expected) throws NoSuchMethodException {
    final Operation operation = new Operation();
    extension.setDeprecated(operation, ResourceWithAnnotations.class.getMethod(methodName));
    Assert.assertEquals(operation.isDeprecated(), expected);
}
Also used : ResourceWithAnnotations(io.swagger.servlet.resources.ResourceWithAnnotations) Operation(io.swagger.models.Operation) Test(org.testng.annotations.Test)

Aggregations

Operation (io.swagger.models.Operation)106 Test (org.testng.annotations.Test)78 Swagger (io.swagger.models.Swagger)39 Path (io.swagger.models.Path)30 BodyParameter (io.swagger.models.parameters.BodyParameter)24 Property (io.swagger.models.properties.Property)24 Response (io.swagger.models.Response)23 Parameter (io.swagger.models.parameters.Parameter)17 RefProperty (io.swagger.models.properties.RefProperty)16 ArrayModel (io.swagger.models.ArrayModel)15 Model (io.swagger.models.Model)14 StringProperty (io.swagger.models.properties.StringProperty)13 ArrayProperty (io.swagger.models.properties.ArrayProperty)12 ReaderContext (io.swagger.servlet.ReaderContext)12 QueryParameter (io.swagger.models.parameters.QueryParameter)10 MapProperty (io.swagger.models.properties.MapProperty)10 NicknamedOperation (io.swagger.resources.NicknamedOperation)10 PathParameter (io.swagger.models.parameters.PathParameter)8 ApiOperation (io.swagger.annotations.ApiOperation)7 Method (java.lang.reflect.Method)7