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);
}
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);
}
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;
}
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;
}
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");
}
}
Aggregations