Search in sources :

Example 91 with SwaggerParser

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");
    }
}
Also used : Path(java.nio.file.Path) SwaggerParser(io.swagger.parser.SwaggerParser) Swagger(io.swagger.models.Swagger) CLIRuntimeException(org.wso2.apimgt.gateway.cli.exception.CLIRuntimeException)

Aggregations

SwaggerParser (io.swagger.parser.SwaggerParser)91 Swagger (io.swagger.models.Swagger)44 Test (org.testng.annotations.Test)37 HashSet (java.util.HashSet)21 HashMap (java.util.HashMap)14 Path (io.swagger.models.Path)11 IOException (java.io.IOException)11 ArrayList (java.util.ArrayList)11 Map (java.util.Map)10 Operation (io.swagger.models.Operation)9 List (java.util.List)9 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)9 SwaggerDeserializationResult (io.swagger.parser.util.SwaggerDeserializationResult)8 Test (org.junit.Test)8 HttpMethod (io.swagger.models.HttpMethod)7 OpenAPIV3Parser (io.swagger.v3.parser.OpenAPIV3Parser)7 File (java.io.File)7 OAuth2Definition (io.swagger.models.auth.OAuth2Definition)6 SecuritySchemeDefinition (io.swagger.models.auth.SecuritySchemeDefinition)6 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)6