Search in sources :

Example 11 with Reader

use of io.swagger.jaxrs.Reader in project webcert by sklintyg.

the class ApiScanner method scan.

private synchronized Swagger scan(Application app, ServletConfig sc, String api) {
    Swagger swagger = null;
    ReflectiveJaxrsScanner scanner = new ReflectiveJaxrsScanner();
    scanner.setResourcePackage(BASE_REST_PACKAGE_NAME + api);
    scanner.setPrettyPrint(true);
    SwaggerSerializers.setPrettyPrint(scanner.getPrettyPrint());
    Set<Class<?>> classes = resolveClasses(app, sc, scanner);
    if (classes != null) {
        Reader reader = new Reader(null, ReaderConfigUtils.getReaderConfig(context));
        swagger = reader.read(classes);
        swagger.setBasePath(resolveBasePathForApi(api));
        swagger = scanner.configure(swagger);
        context.setAttribute(SWAGGER_TOKEN + api, swagger);
        apiInitialized.put(SWAGGER_TOKEN + api, Boolean.TRUE);
    }
    return swagger;
}
Also used : Swagger(io.swagger.models.Swagger) Reader(io.swagger.jaxrs.Reader)

Example 12 with Reader

use of io.swagger.jaxrs.Reader in project swagger-core by swagger-api.

the class BaseApiListingResource method scan.

private static synchronized Swagger scan(Application app, ServletContext context, ServletConfig sc, UriInfo uriInfo) {
    Swagger swagger = null;
    SwaggerContextService ctxService = new SwaggerContextService().withServletConfig(sc).withBasePath(getBasePath(uriInfo));
    Scanner scanner = ctxService.getScanner();
    if (scanner != null) {
        SwaggerSerializers.setPrettyPrint(scanner.getPrettyPrint());
        swagger = new SwaggerContextService().withServletConfig(sc).withBasePath(getBasePath(uriInfo)).getSwagger();
        Set<Class<?>> classes;
        if (scanner instanceof JaxrsScanner) {
            JaxrsScanner jaxrsScanner = (JaxrsScanner) scanner;
            classes = jaxrsScanner.classesFromContext(app, sc);
        } else {
            classes = scanner.classes();
        }
        if (classes != null) {
            Reader reader = new Reader(swagger, ReaderConfigUtils.getReaderConfig(context));
            swagger = reader.read(classes);
            if (scanner instanceof SwaggerConfig) {
                swagger = ((SwaggerConfig) scanner).configure(swagger);
            } else {
                SwaggerConfig swaggerConfig = ctxService.getConfig();
                if (swaggerConfig != null) {
                    LOGGER.debug("configuring swagger with " + swaggerConfig);
                    swaggerConfig.configure(swagger);
                } else {
                    LOGGER.debug("no configurator");
                }
            }
            new SwaggerContextService().withServletConfig(sc).withBasePath(getBasePath(uriInfo)).updateSwagger(swagger);
        }
    }
    if (SwaggerContextService.isScannerIdInitParamDefined(sc)) {
        initializedScanner.put(sc.getServletName() + "_" + SwaggerContextService.getScannerIdFromInitParam(sc), true);
    } else if (SwaggerContextService.isConfigIdInitParamDefined(sc)) {
        initializedConfig.put(sc.getServletName() + "_" + SwaggerContextService.getConfigIdFromInitParam(sc), true);
    } else if (SwaggerContextService.isUsePathBasedConfigInitParamDefined(sc)) {
        initializedConfig.put(sc.getServletName() + "_" + ctxService.getBasePath(), true);
    } else {
        initialized = true;
    }
    return swagger;
}
Also used : SwaggerContextService(io.swagger.jaxrs.config.SwaggerContextService) Scanner(io.swagger.config.Scanner) JaxrsScanner(io.swagger.jaxrs.config.JaxrsScanner) SwaggerConfig(io.swagger.config.SwaggerConfig) JaxrsScanner(io.swagger.jaxrs.config.JaxrsScanner) Swagger(io.swagger.models.Swagger) Reader(io.swagger.jaxrs.Reader)

Example 13 with Reader

use of io.swagger.jaxrs.Reader in project swagger-core by swagger-api.

the class SimpleReaderTest method scanSimpleResourceWithoutAnnotations.

@Test(description = "scan a simple resource without annotations")
public void scanSimpleResourceWithoutAnnotations() {
    DefaultReaderConfig config = new DefaultReaderConfig();
    config.setScanAllResources(true);
    Swagger swagger = new Reader(new Swagger(), config).read(SimpleResourceWithoutAnnotations.class);
    assertEquals(swagger.getPaths().size(), 2);
    Operation get = getGet(swagger, "/{id}");
    assertNotNull(get);
    assertEquals(get.getParameters().size(), 2);
    PathParameter param1 = (PathParameter) get.getParameters().get(0);
    assertEquals(param1.getIn(), "path");
    assertEquals(param1.getName(), "id");
    assertTrue(param1.getRequired());
    assertNull(param1.getDescription());
    assertEquals(param1.getDefaultValue(), "5");
    Parameter param2 = get.getParameters().get(1);
    assertEquals(param2.getIn(), "query");
    assertEquals(param2.getName(), "limit");
    assertFalse(param2.getRequired());
    assertNull(param2.getDescription());
}
Also used : Swagger(io.swagger.models.Swagger) Reader(io.swagger.jaxrs.Reader) SerializableParameter(io.swagger.models.parameters.SerializableParameter) PathParameter(io.swagger.models.parameters.PathParameter) Parameter(io.swagger.models.parameters.Parameter) QueryParameter(io.swagger.models.parameters.QueryParameter) BodyParameter(io.swagger.models.parameters.BodyParameter) DefaultReaderConfig(io.swagger.jaxrs.config.DefaultReaderConfig) Operation(io.swagger.models.Operation) NicknamedOperation(io.swagger.resources.NicknamedOperation) PathParameter(io.swagger.models.parameters.PathParameter) Test(org.testng.annotations.Test)

Example 14 with Reader

use of io.swagger.jaxrs.Reader in project swagger-core by swagger-api.

the class ReferenceTest method scanAPI.

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

Example 15 with Reader

use of io.swagger.jaxrs.Reader in project swagger-core by swagger-api.

the class RegexPathParamTest method scanSimpleResource.

@Test(description = "scan a simple resource")
public void scanSimpleResource() {
    Swagger swagger = new Reader(new Swagger()).read(RegexPathParamResource.class);
    Operation get = swagger.getPaths().get("/{report_type}").getGet();
    Parameter param = get.getParameters().get(0);
    assertEquals(param.getName(), "report_type");
    assertEquals(param.getPattern(), "[aA-zZ]+");
}
Also used : Swagger(io.swagger.models.Swagger) Reader(io.swagger.jaxrs.Reader) Parameter(io.swagger.models.parameters.Parameter) Operation(io.swagger.models.Operation) Test(org.testng.annotations.Test)

Aggregations

Reader (io.swagger.jaxrs.Reader)33 Swagger (io.swagger.models.Swagger)31 Test (org.testng.annotations.Test)20 Parameter (io.swagger.models.parameters.Parameter)7 QueryParameter (io.swagger.models.parameters.QueryParameter)5 DefaultReaderConfig (io.swagger.jaxrs.config.DefaultReaderConfig)4 Operation (io.swagger.models.Operation)4 FormParameter (io.swagger.models.parameters.FormParameter)4 HeaderParameter (io.swagger.models.parameters.HeaderParameter)4 Test (org.junit.Test)4 Scanner (io.swagger.config.Scanner)3 SwaggerConfig (io.swagger.config.SwaggerConfig)3 Model (io.swagger.models.Model)3 Info (io.swagger.models.Info)2 NicknamedOperation (io.swagger.resources.NicknamedOperation)2 SwaggerDefinition (io.swagger.annotations.SwaggerDefinition)1 JaxrsScanner (io.swagger.jaxrs.config.JaxrsScanner)1 ReaderListener (io.swagger.jaxrs.config.ReaderListener)1 SwaggerContextService (io.swagger.jaxrs.config.SwaggerContextService)1 ArrayModel (io.swagger.models.ArrayModel)1