use of io.swagger.parser.SwaggerParser in project product-microgateway by wso2.
the class OpenAPICodegenUtils method getOpenAPIAsJson.
/**
* Convert the v2 or v3 open API definition in yaml or json format into json format of the respective format.
* v2/YAML -> v2/JSON
* v3/YAML -> v3/JSON
*
* @param openAPIContent open API as a string content
* @return openAPI definition as a JSON String
*/
public static String getOpenAPIAsJson(OpenAPI openAPI, String openAPIContent, Path openAPIPath) {
String jsonOpenAPI = Json.pretty(openAPI);
String openAPIVersion;
Path fileName = openAPIPath.getFileName();
if (fileName == null) {
throw new CLIRuntimeException("Error: Couldn't resolve OpenAPI file name.");
} else if (fileName.toString().endsWith("json")) {
openAPIVersion = findSwaggerVersion(openAPIContent, false);
} else {
openAPIVersion = findSwaggerVersion(jsonOpenAPI, false);
}
switch(openAPIVersion) {
case "2":
Swagger swagger = new SwaggerParser().parse(openAPIContent);
return Json.pretty(swagger);
case "3":
return jsonOpenAPI;
default:
throw new CLIRuntimeException("Error: Swagger version is not identified");
}
}
Aggregations