Search in sources :

Example 11 with Swagger

use of io.swagger.models.Swagger 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 12 with Swagger

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

the class ApiListingJSONTest method initializeTest.

@Test(description = "test Swagger initialization from BeanConfig")
public void initializeTest() {
    final BeanConfig bc = new BeanConfig();
    bc.setTitle("Petstore Sample API");
    bc.setHost("petstore.swagger.io");
    bc.setBasePath("/api");
    bc.setScan(true);
    final ApiListingJSON listing = new ApiListingJSON();
    // Initializing by Swagger object
    ApiListingJSON.init(new Swagger());
    // Reading configuration and scanning resources
    listing.scan(null);
    final Swagger sw = ApiListingJSON.swagger;
    Assert.assertNotNull(sw);
    Assert.assertEquals(sw.getInfo().getTitle(), "Petstore Sample API");
    Assert.assertEquals(sw.getHost(), "petstore.swagger.io");
    Assert.assertEquals(sw.getBasePath(), "/api");
}
Also used : BeanConfig(io.swagger.jaxrs.config.BeanConfig) Swagger(io.swagger.models.Swagger) Test(org.testng.annotations.Test)

Example 13 with Swagger

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

the class BeanConfig method scanAndRead.

public void scanAndRead() {
    Set<Class<?>> classes = classes();
    if (classes != null) {
        Swagger swagger = reader.read(classes);
        if (StringUtils.isNotBlank(host)) {
            swagger.setHost(host);
        }
        if (StringUtils.isNotBlank(basePath)) {
            swagger.setBasePath(basePath);
        }
        updateInfoFromConfig();
    }
}
Also used : Swagger(io.swagger.models.Swagger)

Example 14 with Swagger

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

the class BaseApiListingResource method process.

protected Swagger process(Application app, ServletContext servletContext, ServletConfig sc, HttpHeaders headers, UriInfo uriInfo) {
    SwaggerContextService ctxService = new SwaggerContextService().withServletConfig(sc).withBasePath(getBasePath(uriInfo));
    Swagger swagger = ctxService.getSwagger();
    synchronized (ApiListingResource.class) {
        if (SwaggerContextService.isScannerIdInitParamDefined(sc)) {
            if (!initializedScanner.containsKey(sc.getServletName() + "_" + SwaggerContextService.getScannerIdFromInitParam(sc))) {
                swagger = scan(app, servletContext, sc, uriInfo);
            }
        } else {
            if (SwaggerContextService.isConfigIdInitParamDefined(sc)) {
                if (!initializedConfig.containsKey(sc.getServletName() + "_" + SwaggerContextService.getConfigIdFromInitParam(sc))) {
                    swagger = scan(app, servletContext, sc, uriInfo);
                }
            } else if (SwaggerContextService.isUsePathBasedConfigInitParamDefined(sc)) {
                if (!initializedConfig.containsKey(sc.getServletName() + "_" + ctxService.getBasePath())) {
                    swagger = scan(app, servletContext, sc, uriInfo);
                }
            } else if (!initialized) {
                swagger = scan(app, servletContext, sc, uriInfo);
            }
        }
    }
    if (swagger != null) {
        SwaggerSpecFilter filterImpl = FilterFactory.getFilter();
        if (filterImpl != null) {
            SpecFilter f = new SpecFilter();
            swagger = f.filter(swagger, filterImpl, getQueryParams(uriInfo.getQueryParameters()), getCookies(headers), getHeaders(headers));
        }
    }
    return swagger;
}
Also used : SwaggerContextService(io.swagger.jaxrs.config.SwaggerContextService) Swagger(io.swagger.models.Swagger) SwaggerSpecFilter(io.swagger.core.filter.SwaggerSpecFilter) SwaggerSpecFilter(io.swagger.core.filter.SwaggerSpecFilter) SpecFilter(io.swagger.core.filter.SpecFilter)

Example 15 with Swagger

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

the class JsonIdentityTest method scan.

@Test(description = "Scan API with operation and response references")
public void scan() throws IOException {
    final Swagger swagger = new Reader(new Swagger()).read(JsonIdentityResource.class);
    final String json = ResourceUtils.loadClassResource(getClass(), "ResourceWithJsonIdentity.json");
    SerializationMatchers.assertEqualsToJson(swagger, json);
}
Also used : Swagger(io.swagger.models.Swagger) Reader(io.swagger.jaxrs.Reader) Test(org.testng.annotations.Test)

Aggregations

Swagger (io.swagger.models.Swagger)164 Test (org.testng.annotations.Test)114 Operation (io.swagger.models.Operation)39 Parameter (io.swagger.models.parameters.Parameter)30 QueryParameter (io.swagger.models.parameters.QueryParameter)30 BodyParameter (io.swagger.models.parameters.BodyParameter)26 Reader (io.swagger.jaxrs.Reader)23 PathParameter (io.swagger.models.parameters.PathParameter)22 SpecFilter (io.swagger.core.filter.SpecFilter)21 Test (org.junit.Test)19 Path (io.swagger.models.Path)18 Response (io.swagger.models.Response)14 SerializableParameter (io.swagger.models.parameters.SerializableParameter)14 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)10 Model (io.swagger.models.Model)10 NicknamedOperation (io.swagger.resources.NicknamedOperation)10 HeaderParameter (io.swagger.models.parameters.HeaderParameter)9 BeanConfig (io.swagger.jaxrs.config.BeanConfig)8 Map (java.util.Map)8 Info (io.swagger.models.Info)7