Search in sources :

Example 86 with SwaggerParser

use of io.swagger.parser.SwaggerParser in project service-proxy by membrane.

the class SwaggerProxy method init.

@Override
public void init() throws Exception {
    super.init();
    // download swaggerUrl
    HttpClient hc = new HttpClient(router.getHttpClientConfig());
    Exchange ex = hc.call(new Request.Builder().get(swaggerUrl).buildExchange());
    if (ex.getResponse().getStatusCode() != 200) {
        log.error("Couldn't fetch Swagger URL!");
        throw new Exception("Couldn't fetch Swagger URL!");
    }
    // parse swaggerUrl
    swagger = new OpenAPIAdapter(new OpenAPIV3Parser().readContents(ex.getResponse().getBodyAsStringDecoded(), null, null).getOpenAPI());
    if (swagger.isNull()) {
        swagger = new SwaggerAdapter(new SwaggerParser().parse(ex.getResponse().getBodyAsStringDecoded()));
        if (swagger.isNull())
            throw new Exception("couldn't parse Swagger definition");
    }
    // pass swagger specification to Swagger Key
    ((SwaggerProxyKey) key).setSwagger(swagger);
    ((SwaggerProxyKey) key).setAllowUI(allowUI);
    // add interceptor to position 0.
    SwaggerRewriterInterceptor sri = new SwaggerRewriterInterceptor(swagger, swaggerUrl);
    interceptors.add(0, sri);
}
Also used : Exchange(com.predic8.membrane.core.exchange.Exchange) SwaggerParser(io.swagger.parser.SwaggerParser) HttpClient(com.predic8.membrane.core.transport.http.HttpClient) Request(com.predic8.membrane.core.http.Request) SwaggerRewriterInterceptor(com.predic8.membrane.core.interceptor.swagger.SwaggerRewriterInterceptor) OpenAPIV3Parser(io.swagger.v3.parser.OpenAPIV3Parser) OpenAPIAdapter(com.predic8.membrane.core.interceptor.swagger.OpenAPIAdapter) SwaggerAdapter(com.predic8.membrane.core.interceptor.swagger.SwaggerAdapter)

Example 87 with SwaggerParser

use of io.swagger.parser.SwaggerParser in project service-proxy by membrane.

the class SwaggerRewriterInterceptor method handleResponse.

@Override
public Outcome handleResponse(Exchange exc) throws Exception {
    // replacement in swagger.json
    if (exc.getRequest().getUri().endsWith(swaggerJson) && exc.getResponseContentType().equalsIgnoreCase(MediaType.APPLICATION_JSON_VALUE)) {
        SwaggerCompatibleOpenAPI swagBody = new OpenAPIAdapter(new OpenAPIV3Parser().readContents(exc.getResponse().getBodyAsStringDecoded()).getOpenAPI());
        if (swagBody == null || swagBody.isNull()) {
            swagBody = new SwaggerAdapter(new SwaggerParser().parse(exc.getResponse().getBodyAsStringDecoded()));
        }
        swagBody.setHost(exc2originalHostPort(exc));
        exc.getResponse().setBodyContent(swagBody.toJSON());
    }
    // replacement in json and javascript (specifically UI)
    if (rewriteUI && (exc.getRequest().getUri().matches("/.*.js(on)?") || exc.getResponse().getHeader().getContentType() != null && exc.getResponse().getHeader().getContentType().equals(MediaType.TEXT_HTML_VALUE))) {
        String from = "(http(s)?://)" + Pattern.quote(((ServiceProxy) exc.getRule()).getTarget().getHost()) + "(/.*\\.js(on)?)";
        String to = "http" + (isTLS(exc) ? "s" : "") + "://" + exc2originalHostPort(exc) + "$3";
        byte[] body = exc.getResponse().getBodyAsStringDecoded().replaceAll(from, to).getBytes(exc.getResponse().getCharset());
        exc.getResponse().setBodyContent(body);
    }
    return super.handleResponse(exc);
}
Also used : SwaggerParser(io.swagger.parser.SwaggerParser) ServiceProxy(com.predic8.membrane.core.rules.ServiceProxy) OpenAPIV3Parser(io.swagger.v3.parser.OpenAPIV3Parser)

Example 88 with SwaggerParser

use of io.swagger.parser.SwaggerParser in project metersphere by metersphere.

the class Swagger2Parser method parse.

@Override
public ApiDefinitionImport parse(InputStream source, ApiTestImportRequest request) {
    Swagger swagger;
    String sourceStr = "";
    List<AuthorizationValue> auths = setAuths(request);
    if (StringUtils.isNotBlank(request.getSwaggerUrl())) {
        // 使用 url 导入 swagger
        swagger = new SwaggerParser().read(request.getSwaggerUrl(), auths, true);
    } else {
        // 导入的二进制文件转换为 String
        sourceStr = getApiTestStr(source);
        swagger = new SwaggerParser().readWithInfo(sourceStr, auths, true).getSwagger();
    }
    if (swagger == null || swagger.getSwagger() == null) {
        // 不是 2.0 版本,则尝试转换 3.0
        Swagger3Parser swagger3Parser = new Swagger3Parser();
        return swagger3Parser.parse(sourceStr, request);
    }
    ApiDefinitionImport definitionImport = new ApiDefinitionImport();
    this.projectId = request.getProjectId();
    definitionImport.setData(parseRequests(swagger, request));
    return definitionImport;
}
Also used : SwaggerParser(io.swagger.parser.SwaggerParser) AuthorizationValue(io.swagger.models.auth.AuthorizationValue)

Example 89 with SwaggerParser

use of io.swagger.parser.SwaggerParser in project metersphere by metersphere.

the class Swagger2Parser method parse.

@Override
public ApiImport parse(InputStream source, ApiTestImportRequest request) {
    Swagger swagger;
    if (StringUtils.isNotBlank(request.getSwaggerUrl())) {
        swagger = new SwaggerParser().read(request.getSwaggerUrl());
    } else {
        swagger = new SwaggerParser().readWithInfo(getApiTestStr(source)).getSwagger();
    }
    ApiImport apiImport = new ApiImport();
    apiImport.setScenarios(parseRequests(swagger));
    apiImport.getScenarios().forEach(scenario -> scenario.setEnvironmentId(request.getEnvironmentId()));
    return apiImport;
}
Also used : SwaggerParser(io.swagger.parser.SwaggerParser) ApiImport(io.metersphere.api.dto.parse.ApiImport)

Example 90 with SwaggerParser

use of io.swagger.parser.SwaggerParser in project api-layer by zowe.

the class ApiDocV2Service method transformApiDoc.

public String transformApiDoc(String serviceId, ApiDocInfo apiDocInfo) {
    Swagger swagger = new SwaggerParser().readWithInfo(apiDocInfo.getApiDocContent()).getSwagger();
    if (swagger == null) {
        log.debug("Could not convert response body to a Swagger object.");
        throw new UnexpectedTypeException("Response is not a Swagger type object.");
    }
    boolean hidden = swagger.getTag(HIDDEN_TAG) != null;
    updateSchemeHostAndLink(swagger, serviceId, hidden);
    updatePaths(swagger, serviceId, apiDocInfo, hidden);
    updateExternalDoc(swagger, apiDocInfo);
    try {
        return Json.mapper().writeValueAsString(swagger);
    } catch (JsonProcessingException e) {
        log.debug("Could not convert Swagger to JSON", e);
        throw new ApiDocTransformationException("Could not convert Swagger to JSON");
    }
}
Also used : SwaggerParser(io.swagger.parser.SwaggerParser) Swagger(io.swagger.models.Swagger) UnexpectedTypeException(javax.validation.UnexpectedTypeException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ApiDocTransformationException(org.zowe.apiml.apicatalog.swagger.ApiDocTransformationException)

Aggregations

SwaggerParser (io.swagger.parser.SwaggerParser)95 Swagger (io.swagger.models.Swagger)48 Test (org.testng.annotations.Test)37 HashSet (java.util.HashSet)21 HashMap (java.util.HashMap)15 Path (io.swagger.models.Path)12 ArrayList (java.util.ArrayList)12 IOException (java.io.IOException)11 Operation (io.swagger.models.Operation)10 Map (java.util.Map)10 List (java.util.List)9 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)9 SwaggerDeserializationResult (io.swagger.parser.util.SwaggerDeserializationResult)8 File (java.io.File)8 Test (org.junit.Test)8 HttpMethod (io.swagger.models.HttpMethod)7 OpenAPIV3Parser (io.swagger.v3.parser.OpenAPIV3Parser)7 OAuth2Definition (io.swagger.models.auth.OAuth2Definition)6 SecuritySchemeDefinition (io.swagger.models.auth.SecuritySchemeDefinition)6 Parameter (io.swagger.models.parameters.Parameter)6