Search in sources :

Example 1 with SwaggerConverter

use of io.swagger.v3.parser.converter.SwaggerConverter in project ballerina by ballerina-lang.

the class SwaggerConverterUtils method generateOAS3Definitions.

/**
 * This method will generate open API 3.X specification for given ballerina service. Since we will need to
 * support both swagger 2.0 and OAS 3.0 it was implemented to convert to swagger by default and convert it
 * to OAS on demand.
 *
 * @param ballerinaSource ballerina source to be converted to swagger/OAS definition
 * @param serviceName specific service name within ballerina source that need to map OAS
 * @return Generated OAS3 string output.
 * @throws IOException When error occurs while converting, parsing input source.
 */
public static String generateOAS3Definitions(String ballerinaSource, String serviceName) throws IOException {
    // Get the ballerina model using the ballerina source code.
    BFile balFile = new BFile();
    balFile.setContent(ballerinaSource);
    // Create empty swagger object.
    Swagger swaggerDefinition = new Swagger();
    BLangCompilationUnit topCompilationUnit = SwaggerConverterUtils.getTopLevelNodeFromBallerinaFile(balFile);
    String httpAlias = getAlias(topCompilationUnit, Constants.BALLERINA_HTTP_PACKAGE_NAME);
    String swaggerAlias = getAlias(topCompilationUnit, Constants.SWAGGER_PACKAGE_NAME);
    SwaggerServiceMapper swaggerServiceMapper = new SwaggerServiceMapper(httpAlias, swaggerAlias);
    String swaggerSource = StringUtils.EMPTY;
    for (TopLevelNode topLevelNode : topCompilationUnit.getTopLevelNodes()) {
        if (topLevelNode instanceof BLangService) {
            ServiceNode serviceDefinition = (ServiceNode) topLevelNode;
            // Generate swagger string for the mentioned service name.
            if (StringUtils.isNotBlank(serviceName)) {
                if (serviceDefinition.getName().getValue().equals(serviceName)) {
                    swaggerDefinition = swaggerServiceMapper.convertServiceToSwagger(serviceDefinition);
                    break;
                }
            } else {
                // If no service name mentioned, then generate swagger definition for the first service.
                swaggerDefinition = swaggerServiceMapper.convertServiceToSwagger(serviceDefinition);
                break;
            }
        }
    }
    swaggerSource = swaggerServiceMapper.generateSwaggerString(swaggerDefinition);
    SwaggerConverter converter = new SwaggerConverter();
    return Yaml.pretty(converter.readContents(swaggerSource, null, null).getOpenAPI());
}
Also used : ServiceNode(org.ballerinalang.model.tree.ServiceNode) Swagger(io.swagger.models.Swagger) BLangService(org.wso2.ballerinalang.compiler.tree.BLangService) SwaggerConverter(io.swagger.v3.parser.converter.SwaggerConverter) BFile(org.ballerinalang.composer.service.ballerina.parser.service.model.BFile) BLangCompilationUnit(org.wso2.ballerinalang.compiler.tree.BLangCompilationUnit) TopLevelNode(org.ballerinalang.model.tree.TopLevelNode)

Aggregations

Swagger (io.swagger.models.Swagger)1 SwaggerConverter (io.swagger.v3.parser.converter.SwaggerConverter)1 BFile (org.ballerinalang.composer.service.ballerina.parser.service.model.BFile)1 ServiceNode (org.ballerinalang.model.tree.ServiceNode)1 TopLevelNode (org.ballerinalang.model.tree.TopLevelNode)1 BLangCompilationUnit (org.wso2.ballerinalang.compiler.tree.BLangCompilationUnit)1 BLangService (org.wso2.ballerinalang.compiler.tree.BLangService)1