@Test(description = "Test OAS definition generation from ballerina service which contains 2 resource which " + "contains only one http method in resource level") public void testOASGenerationWithTwoResource() { String serviceName = "hello"; try { String swaggerDefinition = SwaggerConverterUtils.generateOAS3Definitions( sampleSwaggerWithTwoResources, serviceName); Assert.assertNotNull(swaggerDefinition); } catch (IOException e) { Assert.fail("Error while converting ballerina service to swagger definition with empty service name"); } catch (SwaggerConverterException e) { Assert.fail("Error while converting ballerina service to swagger definition"); } }
public static String generateOAS3Definitions(String ballerinaSource, String serviceName) throws IOException, SwaggerConverterException { Swagger swagger = new Swagger(); String swaggerSource; BallerinaFile ballerinaFile = lsCompiler.compileContent(ballerinaSource, CompilerPhase.DEFINE); BLangCompilationUnit topCompilationUnit = ballerinaFile.getBLangPackage().getCompilationUnits().get(0); String httpAlias = getAlias(topCompilationUnit, Constants.BALLERINA_HTTP_PACKAGE_NAME); String swaggerAlias = getAlias(topCompilationUnit, Constants.SWAGGER_PACKAGE_NAME); SwaggerServiceMapper swaggerServiceMapper = new SwaggerServiceMapper(httpAlias, swaggerAlias); List<EndpointNode> endpoints = new ArrayList<>(); for (TopLevelNode topLevelNode : topCompilationUnit.getTopLevelNodes()) { if (topLevelNode instanceof BLangEndpoint) { endpoints.add((EndpointNode) topLevelNode); } if (topLevelNode instanceof BLangService) { ServiceNode serviceDefinition = (ServiceNode) topLevelNode; swagger = new SwaggerEndpointMapper() .convertBoundEndpointsToSwagger(endpoints, serviceDefinition, swagger); if (StringUtils.isNotBlank(serviceName)) { if (serviceDefinition.getName().getValue().equals(serviceName)) { swagger = swaggerServiceMapper.convertServiceToSwagger(serviceDefinition, swagger); break; } } else { swagger = swaggerServiceMapper.convertServiceToSwagger(serviceDefinition, swagger); break; } } } swaggerSource = swaggerServiceMapper.generateSwaggerString(swagger); SwaggerConverter converter = new SwaggerConverter(); SwaggerDeserializationResult result = new SwaggerParser().readWithInfo(swaggerSource); if (result.getMessages().size() > 0) { throw new SwaggerConverterException("Please check if input source is valid and complete"); } return Yaml.pretty(converter.convert(result).getOpenAPI()); }
SwaggerConverterUtils { public static String generateOAS3Definitions(String ballerinaSource, String serviceName) throws IOException, SwaggerConverterException { Swagger swagger = new Swagger(); String swaggerSource; BallerinaFile ballerinaFile = lsCompiler.compileContent(ballerinaSource, CompilerPhase.DEFINE); BLangCompilationUnit topCompilationUnit = ballerinaFile.getBLangPackage().getCompilationUnits().get(0); String httpAlias = getAlias(topCompilationUnit, Constants.BALLERINA_HTTP_PACKAGE_NAME); String swaggerAlias = getAlias(topCompilationUnit, Constants.SWAGGER_PACKAGE_NAME); SwaggerServiceMapper swaggerServiceMapper = new SwaggerServiceMapper(httpAlias, swaggerAlias); List<EndpointNode> endpoints = new ArrayList<>(); for (TopLevelNode topLevelNode : topCompilationUnit.getTopLevelNodes()) { if (topLevelNode instanceof BLangEndpoint) { endpoints.add((EndpointNode) topLevelNode); } if (topLevelNode instanceof BLangService) { ServiceNode serviceDefinition = (ServiceNode) topLevelNode; swagger = new SwaggerEndpointMapper() .convertBoundEndpointsToSwagger(endpoints, serviceDefinition, swagger); if (StringUtils.isNotBlank(serviceName)) { if (serviceDefinition.getName().getValue().equals(serviceName)) { swagger = swaggerServiceMapper.convertServiceToSwagger(serviceDefinition, swagger); break; } } else { swagger = swaggerServiceMapper.convertServiceToSwagger(serviceDefinition, swagger); break; } } } swaggerSource = swaggerServiceMapper.generateSwaggerString(swagger); SwaggerConverter converter = new SwaggerConverter(); SwaggerDeserializationResult result = new SwaggerParser().readWithInfo(swaggerSource); if (result.getMessages().size() > 0) { throw new SwaggerConverterException("Please check if input source is valid and complete"); } return Yaml.pretty(converter.convert(result).getOpenAPI()); } }
SwaggerConverterUtils { public static String generateOAS3Definitions(String ballerinaSource, String serviceName) throws IOException, SwaggerConverterException { Swagger swagger = new Swagger(); String swaggerSource; BallerinaFile ballerinaFile = lsCompiler.compileContent(ballerinaSource, CompilerPhase.DEFINE); BLangCompilationUnit topCompilationUnit = ballerinaFile.getBLangPackage().getCompilationUnits().get(0); String httpAlias = getAlias(topCompilationUnit, Constants.BALLERINA_HTTP_PACKAGE_NAME); String swaggerAlias = getAlias(topCompilationUnit, Constants.SWAGGER_PACKAGE_NAME); SwaggerServiceMapper swaggerServiceMapper = new SwaggerServiceMapper(httpAlias, swaggerAlias); List<EndpointNode> endpoints = new ArrayList<>(); for (TopLevelNode topLevelNode : topCompilationUnit.getTopLevelNodes()) { if (topLevelNode instanceof BLangEndpoint) { endpoints.add((EndpointNode) topLevelNode); } if (topLevelNode instanceof BLangService) { ServiceNode serviceDefinition = (ServiceNode) topLevelNode; swagger = new SwaggerEndpointMapper() .convertBoundEndpointsToSwagger(endpoints, serviceDefinition, swagger); if (StringUtils.isNotBlank(serviceName)) { if (serviceDefinition.getName().getValue().equals(serviceName)) { swagger = swaggerServiceMapper.convertServiceToSwagger(serviceDefinition, swagger); break; } } else { swagger = swaggerServiceMapper.convertServiceToSwagger(serviceDefinition, swagger); break; } } } swaggerSource = swaggerServiceMapper.generateSwaggerString(swagger); SwaggerConverter converter = new SwaggerConverter(); SwaggerDeserializationResult result = new SwaggerParser().readWithInfo(swaggerSource); if (result.getMessages().size() > 0) { throw new SwaggerConverterException("Please check if input source is valid and complete"); } return Yaml.pretty(converter.convert(result).getOpenAPI()); }  }
SwaggerConverterUtils { public static String generateOAS3Definitions(String ballerinaSource, String serviceName) throws IOException, SwaggerConverterException { Swagger swagger = new Swagger(); String swaggerSource; BallerinaFile ballerinaFile = lsCompiler.compileContent(ballerinaSource, CompilerPhase.DEFINE); BLangCompilationUnit topCompilationUnit = ballerinaFile.getBLangPackage().getCompilationUnits().get(0); String httpAlias = getAlias(topCompilationUnit, Constants.BALLERINA_HTTP_PACKAGE_NAME); String swaggerAlias = getAlias(topCompilationUnit, Constants.SWAGGER_PACKAGE_NAME); SwaggerServiceMapper swaggerServiceMapper = new SwaggerServiceMapper(httpAlias, swaggerAlias); List<EndpointNode> endpoints = new ArrayList<>(); for (TopLevelNode topLevelNode : topCompilationUnit.getTopLevelNodes()) { if (topLevelNode instanceof BLangEndpoint) { endpoints.add((EndpointNode) topLevelNode); } if (topLevelNode instanceof BLangService) { ServiceNode serviceDefinition = (ServiceNode) topLevelNode; swagger = new SwaggerEndpointMapper() .convertBoundEndpointsToSwagger(endpoints, serviceDefinition, swagger); if (StringUtils.isNotBlank(serviceName)) { if (serviceDefinition.getName().getValue().equals(serviceName)) { swagger = swaggerServiceMapper.convertServiceToSwagger(serviceDefinition, swagger); break; } } else { swagger = swaggerServiceMapper.convertServiceToSwagger(serviceDefinition, swagger); break; } } } swaggerSource = swaggerServiceMapper.generateSwaggerString(swagger); SwaggerConverter converter = new SwaggerConverter(); SwaggerDeserializationResult result = new SwaggerParser().readWithInfo(swaggerSource); if (result.getMessages().size() > 0) { throw new SwaggerConverterException("Please check if input source is valid and complete"); } return Yaml.pretty(converter.convert(result).getOpenAPI()); }  static String generateSwaggerDefinitions(String ballerinaSource, String serviceName); static String generateOAS3Definitions(String ballerinaSource, String serviceName); static void generateOAS3Definitions(Path servicePath, Path outPath, String serviceName); static void generateSwaggerDefinitions(Path servicePath, Path outPath, String serviceName); }
SwaggerConverterUtils { public static String generateOAS3Definitions(String ballerinaSource, String serviceName) throws IOException, SwaggerConverterException { Swagger swagger = new Swagger(); String swaggerSource; BallerinaFile ballerinaFile = lsCompiler.compileContent(ballerinaSource, CompilerPhase.DEFINE); BLangCompilationUnit topCompilationUnit = ballerinaFile.getBLangPackage().getCompilationUnits().get(0); String httpAlias = getAlias(topCompilationUnit, Constants.BALLERINA_HTTP_PACKAGE_NAME); String swaggerAlias = getAlias(topCompilationUnit, Constants.SWAGGER_PACKAGE_NAME); SwaggerServiceMapper swaggerServiceMapper = new SwaggerServiceMapper(httpAlias, swaggerAlias); List<EndpointNode> endpoints = new ArrayList<>(); for (TopLevelNode topLevelNode : topCompilationUnit.getTopLevelNodes()) { if (topLevelNode instanceof BLangEndpoint) { endpoints.add((EndpointNode) topLevelNode); } if (topLevelNode instanceof BLangService) { ServiceNode serviceDefinition = (ServiceNode) topLevelNode; swagger = new SwaggerEndpointMapper() .convertBoundEndpointsToSwagger(endpoints, serviceDefinition, swagger); if (StringUtils.isNotBlank(serviceName)) { if (serviceDefinition.getName().getValue().equals(serviceName)) { swagger = swaggerServiceMapper.convertServiceToSwagger(serviceDefinition, swagger); break; } } else { swagger = swaggerServiceMapper.convertServiceToSwagger(serviceDefinition, swagger); break; } } } swaggerSource = swaggerServiceMapper.generateSwaggerString(swagger); SwaggerConverter converter = new SwaggerConverter(); SwaggerDeserializationResult result = new SwaggerParser().readWithInfo(swaggerSource); if (result.getMessages().size() > 0) { throw new SwaggerConverterException("Please check if input source is valid and complete"); } return Yaml.pretty(converter.convert(result).getOpenAPI()); }  static String generateSwaggerDefinitions(String ballerinaSource, String serviceName); static String generateOAS3Definitions(String ballerinaSource, String serviceName); static void generateOAS3Definitions(Path servicePath, Path outPath, String serviceName); static void generateSwaggerDefinitions(Path servicePath, Path outPath, String serviceName);  }