private void convertOperations() { Map<String, Path> paths = swagger.getPaths(); if (paths == null || paths.isEmpty()) { return; } appendLine(serviceBuilder, "service MainService {"); for (Path path : paths.values()) { for (Operation operation : path.getOperationMap().values()) { convertOperation(operation); } } serviceBuilder.setLength(serviceBuilder.length() - 1); appendLine(serviceBuilder, "}"); }
protected void addOperationToSwagger() { if (StringUtils.isEmpty(httpMethod)) { return; } Path pathObj = swagger.getPath(path); if (pathObj == null) { pathObj = new Path(); swagger.path(path, pathObj); } HttpMethod hm = HttpMethod.valueOf(httpMethod.toUpperCase(Locale.US)); if (pathObj.getOperationMap().get(hm) != null) { throw new Error(String.format("Only allowed one default path. %s:%s", swaggerGenerator.getCls().getName(), providerMethod.getName())); } pathObj.set(httpMethod, operation); } }
String strPath = entry.getKey(); Path path = entry.getValue(); for (Entry<HttpMethod, Operation> operationEntry : path.getOperationMap().entrySet()) { Operation operation = operationEntry.getValue(); if (operation.getOperationId() == null) {
@Override @JSONField(serialize = false) public Map<HttpMethod, Operation> getOperationMap() { return super.getOperationMap(); } }
private Map<String, Parameter> getParameterMap(Path path) { Map<HttpMethod, Operation> operationMap = path.getOperationMap(); return operationMap.get(HttpMethod.GET).getParameters().stream().collect( Collectors.toMap( Parameter::getName, (Function<Parameter, Parameter>) param -> param)); }
public static OperationDescription operationDescriptionOf(final Swagger swagger, final Operation operation) { final Entry<String, Path> pathEntry = swagger.getPaths().entrySet().stream() .filter(e -> e.getValue().getOperations().contains(operation)).findFirst().get(); final String path = pathEntry.getKey(); final Entry<HttpMethod, Operation> operationEntry = pathEntry.getValue().getOperationMap().entrySet().stream() .filter(e -> e.getValue().equals(operation)).findFirst().get(); final HttpMethod method = operationEntry.getKey(); final String specifiedSummary = trimToNull(operation.getSummary()); final String specifiedDescription = trimToNull(operation.getDescription()); final String name = ofNullable(specifiedSummary).orElseGet(() -> method + " " + path); final String description = ofNullable(specifiedDescription).orElseGet(() -> "Send " + method + " request to " + path); return new OperationDescription(name, description); }
private void manageOperationNames(Path path, String pathname) { String serviceIdTemp; Map<HttpMethod, Operation> operationMap = path.getOperationMap(); if (operationMap != null) { for (Entry<HttpMethod, Operation> entry : operationMap.entrySet()) { serviceIdTemp = computeServiceId(pathname, entry); entry.getValue().setVendorExtension("x-serviceId", serviceIdTemp); entry.getValue().setVendorExtension("x-serviceId-varName", serviceIdTemp.toUpperCase() + "_SERVICE_ID"); } } }
private boolean isPathNotExist(Map<String, UriTemplate> uriTemplateMap, String securityName, Map.Entry<String, Path> entry) { Path path = entry.getValue(); String uri = entry.getKey(); path.getOperationMap().entrySet().forEach(httpMethodOperationEntry -> { if (isOperationNotExist(httpMethodOperationEntry, uri, uriTemplateMap, securityName)) { path.set(httpMethodOperationEntry.getKey().name().toLowerCase(), null); } }); return path.isEmpty(); }
public static OperationDescription operationDescriptionOf(final Swagger swagger, final Operation operation) { final Entry<String, Path> pathEntry = swagger.getPaths().entrySet().stream() .filter(e -> e.getValue().getOperations().contains(operation)).findFirst().get(); final String path = pathEntry.getKey(); final Entry<HttpMethod, Operation> operationEntry = pathEntry.getValue().getOperationMap().entrySet().stream() .filter(e -> e.getValue().equals(operation)).findFirst().get(); final HttpMethod method = operationEntry.getKey(); final String specifiedSummary = trimToNull(operation.getSummary()); final String specifiedDescription = trimToNull(operation.getDescription()); final String name = ofNullable(toLiteralNull(specifiedSummary)).orElseGet(() -> method + " " + path); final String description = ofNullable(toLiteralNull(specifiedDescription)) .orElseGet(() -> "Send " + method + " request to " + path); return new OperationDescription(name, description); }
private boolean isPathNotExist(Map<String, UriTemplate> uriTemplateMap, String securityName, Map.Entry<String, Path> entry) { Path path = entry.getValue(); String uri = entry.getKey(); path.getOperationMap().entrySet().forEach(httpMethodOperationEntry -> { if (isOperationNotExist(httpMethodOperationEntry, uri, uriTemplateMap, securityName)) { path.set(httpMethodOperationEntry.getKey().name().toLowerCase(), null); } }); return path.isEmpty(); }
public static OperationDescription operationDescriptionOf(final Swagger swagger, final Operation operation, final BiFunction<String, String, String> consumer) { final Entry<String, Path> pathEntry = swagger.getPaths().entrySet().stream() .filter(e -> e.getValue().getOperations().contains(operation)).findFirst().get(); final String path = pathEntry.getKey(); final Entry<HttpMethod, Operation> operationEntry = pathEntry.getValue().getOperationMap().entrySet().stream() .filter(e -> e.getValue().equals(operation)).findFirst().get(); final HttpMethod method = operationEntry.getKey(); final String specifiedSummary = trimToNull(operation.getSummary()); final String specifiedDescription = trimToNull(operation.getDescription()); final String name = ofNullable(toLiteralNull(specifiedSummary)).orElseGet(() -> method + " " + path); final String description = ofNullable(toLiteralNull(specifiedDescription)) .orElseGet(() -> consumer.apply(method.name(), path)); return new OperationDescription(name, description); }
private void convertOperations() { Map<String, Path> paths = swagger.getPaths(); if (paths == null || paths.isEmpty()) { return; } appendLine(serviceBuilder, "service MainService {"); for (Path path : paths.values()) { for (Operation operation : path.getOperationMap().values()) { convertOpeation(operation); } } serviceBuilder.setLength(serviceBuilder.length() - 1); appendLine(serviceBuilder, "}"); }
private static List<Endpoint> convert2EndpointList(Map<String, Path> map) { List<Endpoint> endpoints = new ArrayList<Endpoint>(); if (null == map) return endpoints; map.forEach((url, path) -> { Map<HttpMethod, Operation> operationMap = path.getOperationMap(); operationMap.forEach((httpMethod, operation) -> { Endpoint endpoint = new Endpoint(); endpoint.setPathUrl(url); endpoint.setMethod(httpMethod); endpoint.setSummary(operation.getSummary()); endpoint.setPath(path); endpoint.setOperation(operation); endpoints.add(endpoint); }); }); return endpoints; }
/** * Bind a handler to each operation. * * @param router the Vert.x router * @param config the plugin configuration * @param swagger the OpenAPI specification * @param path the mock path * @param pathConfig the path configuration */ private void handlePathOperations(Router router, OpenApiPluginConfig config, Swagger swagger, String path, Path pathConfig) { pathConfig.getOperationMap().forEach((httpMethod, operation) -> { final String fullPath = ofNullable(swagger.getBasePath()).orElse("") + convertPath(path); LOGGER.debug("Adding mock endpoint: {} -> {}", httpMethod, fullPath); // convert an {@link io.swagger.models.HttpMethod} to an {@link io.vertx.core.http.HttpMethod} final HttpMethod method = HttpMethod.valueOf(httpMethod.name()); router.route(method, fullPath).handler(buildHandler(config, swagger, operation)); }); }
static SwaggerModelInfo validateNoMissingOperationIds(final SwaggerModelInfo info) { final Swagger swagger = info.getModel(); if (swagger == null) { return info; } long countNoOpId = swagger.getPaths().values().stream() .flatMap(p -> p.getOperationMap().values().stream()) .filter(o -> o.getOperationId() == null) .count(); if (countNoOpId == 0) { return info; } final SwaggerModelInfo.Builder withWarnings = new SwaggerModelInfo.Builder().createFrom(info); withWarnings.addWarning(new Violation.Builder()// .error("missing-operation-ids") .message("Some operations (" + countNoOpId + ") have no operationId").build()); return withWarnings.build(); }
public DefaultBindingBuilder(BindingBuilderFactory bindingBuilderFactory, SwaggerRouter swaggerRouter, String path) { this.bindingBuilderFactory = bindingBuilderFactory; this.swaggerRouter = swaggerRouter; this.routeConfiguration = new RouteConfiguration(path); Path swaggerPath = swaggerRouter.getSwagger().getPath(routeConfiguration.getSwaggerPath()); Preconditions.checkNotNull(swaggerPath, String.format("Could not find swagger path: [%s] in swagger spec: [%s]", routeConfiguration.getSwaggerPath(), swaggerRouter.getSwagger().getPaths().keySet())); this.operationMap = swaggerPath.getOperationMap(); }
public static Router swaggerRouter(Router baseRouter, Swagger swagger, EventBus eventBus, ServiceIdResolver serviceIdResolver, Function<RoutingContext, DeliveryOptions> configureMessage) { baseRouter.route().handler(BodyHandler.create()); final String basePath = getBasePath(swagger); SwaggerAuthHandlerFactory authHandlerFactory = getSwaggerAuthHandlerFactory(swagger); swagger.getPaths().forEach((path, pathDescription) -> pathDescription.getOperationMap().forEach((method, operation) -> { String convertedPath = convertParametersToVertx(basePath + path); configureAuthRoute(baseRouter, method, convertedPath, swagger, operation, authHandlerFactory); Route route = ROUTE_BUILDERS.get(method).buildRoute(baseRouter, convertedPath); String serviceId = serviceIdResolver.resolve(method, path, operation); configureRoute(route, serviceId, operation, eventBus, configureMessage); })); return baseRouter; }
@Test public void shouldGenerateAtlasmapSchemaSetForUpdatePetRequest() throws IOException { final Operation swaggerOperation = swagger.getPaths().get(path).getOperationMap().get(operation); final DataShape shape = generator.createShapeFromRequest(json, swagger, swaggerOperation); final SoftAssertions softly = new SoftAssertions(); softly.assertThat(shape.getKind()).isEqualTo(DataShapeKinds.XML_SCHEMA); softly.assertThat(shape.getName()).isEqualTo("Request"); softly.assertThat(shape.getDescription()).isEqualTo("API request payload"); softly.assertThat(shape.getExemplar()).isNotPresent(); softly.assertAll(); final String expectedSpecification; try (InputStream in = UnifiedXmlDataShapeGenerator.class.getResourceAsStream("/swagger/" + schemaset)) { expectedSpecification = IOUtils.toString(in, StandardCharsets.UTF_8); } final String specification = shape.getSpecification(); assertThat(specification).isXmlEqualTo(expectedSpecification); }
@Test public void shouldGenerateAtlasmapSchemaSetForUpdatePetRequest() throws IOException { final Operation swaggerOperation = swagger.getPaths().get(path).getOperationMap().get(operation); final DataShape shape = generator.createShapeFromRequest(swaggerSpecification, swagger, swaggerOperation); final SoftAssertions softly = new SoftAssertions(); softly.assertThat(shape.getKind()).isEqualTo(DataShapeKinds.XML_SCHEMA); softly.assertThat(shape.getName()).isEqualTo("Request"); softly.assertThat(shape.getDescription()).isEqualTo("API request payload"); softly.assertThat(shape.getExemplar()).isNotPresent(); softly.assertAll(); final String expectedSpecification; try (InputStream in = UnifiedXmlDataShapeGenerator.class.getResourceAsStream("/swagger/" + schemaset)) { expectedSpecification = IOUtils.toString(in, StandardCharsets.UTF_8); } final String specification = shape.getSpecification(); assertThat(specification).isXmlEqualTo(expectedSpecification); }
@Test public void shouldGenerateAtlasmapSchemaSetForUpdatePetRequest() throws IOException { final Operation swaggerOperation = swagger.getPaths().get(path).getOperationMap().get(operation); final DataShape shape = generator.createShapeFromRequest(json, swagger, swaggerOperation); final SoftAssertions softly = new SoftAssertions(); softly.assertThat(shape.getKind()).isEqualTo(DataShapeKinds.XML_SCHEMA); softly.assertThat(shape.getName()).isEqualTo("Request"); softly.assertThat(shape.getDescription()).isEqualTo("API request payload"); softly.assertThat(shape.getExemplar()).isNotPresent(); softly.assertAll(); final String expectedSpecification; try (InputStream in = UnifiedXmlDataShapeGenerator.class.getResourceAsStream("/swagger/" + schemaset)) { expectedSpecification = IOUtils.toString(in, StandardCharsets.UTF_8); } final String specification = shape.getSpecification(); assertThat(specification).isXmlEqualTo(expectedSpecification); }